-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyKing.java
65 lines (55 loc) · 1.81 KB
/
CopyKing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.awt.*;
import java.awt.datatransfer.*;
import java.util.regex.*;
public class CopyKing
{
String list = ""; //unused
String prev = ""; //previous copy, stops infinite printing of last url
int num = 1; //printed before each url, increments with each url copied
public void run()
{
System.out.println("=============================================================");
System.out.println("CopyKing by nhampshire");
System.out.println("=============================================================");
System.out.println("Listening...");
while (true) {
try {
Thread.sleep(1000); //interval at which program checks for new URL
getCopy();
} catch (Exception e) {
// TODO: handle exception
}
}
}
public void getCopy()
{
/* get clipboard */
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
/* get clipboard context */
Transferable data = clipboard.getContents(null);
/* is context string */
boolean isText = ( ( data != null ) && ( data.isDataFlavorSupported( DataFlavor.stringFlavor ) ) );
/* if yes print it and/or add to list (depending on version) */
if ( isText ) {
try
{
String URL_REGEX = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
Pattern p = Pattern.compile(URL_REGEX);
String s = (String)data.getTransferData( DataFlavor.stringFlavor );
Matcher m = p.matcher(s);//replace with string to compare
if(!s.equals(prev) && m.find())
{
prev = s;
String thisCopy= num + ". " + s;
System.out.println(thisCopy);
num++;
}
} catch(Exception e){System.out.println(e);}
}
}
public static void main(String[] args)
{
CopyKing king = new CopyKing();
king.run();
}
}