Skip to content

Commit

Permalink
fix clipboard issue by resetting ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
awidesky committed Mar 3, 2023
1 parent 136ae29 commit 3384189
Showing 1 changed file with 8 additions and 24 deletions.
32 changes: 8 additions & 24 deletions src/com/awidesky/YoutubeClipboardAutoDownloader/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
Expand Down Expand Up @@ -44,7 +45,6 @@
public class Main {

private static String clipboardBefore = "";
private static boolean isSecondtime = false;
private static ClipBoardCheckerThread clipChecker;

public static final Charset NATIVECHARSET = Charset.forName(System.getProperty("native.encoding"));
Expand Down Expand Up @@ -155,7 +155,6 @@ private static void checkClipBoard() {
logger.log("[debug] clipboard ignored due to ClipboardListenOption == \"" + ClipBoardOption.NOLISTEN.getString() + "\"");
return;
}

clipChecker.submit(() -> { // To avoid overhead in EDT, put task in clipCheckerThread.

try {
Expand All @@ -178,11 +177,15 @@ private static void checkClipBoard() {
return;
}
final String data = (String)cb.getData(DataFlavor.stringFlavor);

logger.log("[debug] clipboard : " + data);
cb.setContents(new StringSelection(data), null);

if (isRedundant(data)) return;
logger.log("[debug] clipboard : " + data);

if (clipboardBefore.equals(data)) {
logger.logVerbose("[debug] Duplicate input, ignore this input.");
clipboardBefore = "";
return;
}
clipboardBefore = data;

if (!Config.isLinkAcceptable(data)) {
Expand Down Expand Up @@ -218,25 +221,6 @@ private static void checkClipBoard() {

}

private static boolean isRedundant(String data) throws InterruptedException {

if (clipboardBefore.equals(data)) {

if (isSecondtime) { //second time finding same data
Thread.sleep(50);
clipboardBefore = "";
} else {
isSecondtime = true;
}

return true;

} else {
return false;
}

}

private static void submitDownload(String data) {

int num = taskNum++;
Expand Down

0 comments on commit 3384189

Please sign in to comment.