Skip to content

Commit

Permalink
Prevent writing the same clipboard too quickly
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs committed Mar 17, 2024
1 parent 08dc836 commit 44de073
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
12 changes: 3 additions & 9 deletions src/main/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,13 @@ export const getNextFileNumber = async (syncFolder: string) => {
};

export const isThereMoreThanOneClipboardFile = async (syncFolder: string) => {
let found = 0;
const files = await fs.readdir(syncFolder);
for (const file of files) {
const filePath = path.join(syncFolder, file);
const parsedFile = parseClipboardFileName(filePath, syncFolder);
if (parsedFile) {
found++;
if (found > 1) {
return true;
}
if (parseClipboardFileName(path.join(syncFolder, file), syncFolder)) {
return true;
}
}
return found > 1;
return false;
};

export const isIsReceivingFile = (file: string) => {
Expand Down
51 changes: 32 additions & 19 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@ let appIcon: Tray = null;

let syncFolder: string = null;

let lastFileNumberWritten: number = null;

let lastTextRead: ClipboardText = null;
let lastImageSha256Read: string = null;
let lastClipboardFilePathsRead: string[] = null;
let lastTimeRead: number = null;

let lastTextWritten: ClipboardText = null;
let lastImageSha256Written: string = null;
let lastTimeWritten: number = null;
let lastFileNumberWritten: number = null;

let clipboardListener: ClipboardEventListener = null;
let clipboardFilesWatcher: StatWatcher = null;
let filesCleanerTask: cron.ScheduledTask = null;
Expand All @@ -99,6 +102,8 @@ let lastTimeClipboardChecked: number = null;
let lastTimeFilesChecked: number = null;

const writeClipboardToFile = async () => {
const currentTime = Date.now();

// Avoids sending the clipboard if there is no other computer receiving
if (
(await fs.readdir(syncFolder)).filter(
Expand All @@ -111,16 +116,6 @@ const writeClipboardToFile = async () => {
return;
}

// Prevents duplicated clipboard events
const currentTime = Date.now();
if (
lastTimeClipboardChecked &&
currentTime - lastTimeClipboardChecked < 1000
) {
return;
}
lastTimeClipboardChecked = currentTime;

let clipboardType: ClipboardType;
let clipboardText: ClipboardText;
let clipboardImage: Buffer;
Expand Down Expand Up @@ -172,13 +167,16 @@ const writeClipboardToFile = async () => {
return;
}

// Prevent sending the clipboard that was just received
// Prevent sending the clipboard that was just received, or resend the same clipboard too quickly
if (
clipboardType === "text" &&
(isClipboardTextEmpty(clipboardText) ||
(lastTimeRead &&
currentTime - lastTimeRead < 5000 &&
isClipboardTextEquals(lastTextRead, clipboardText)))
isClipboardTextEquals(lastTextRead, clipboardText)) ||
(lastTimeWritten &&
currentTime - lastTimeWritten < 10000 &&
isClipboardTextEquals(lastTextWritten, clipboardText)))
) {
return;
}
Expand All @@ -188,7 +186,10 @@ const writeClipboardToFile = async () => {
(!clipboardImage ||
(lastTimeRead &&
currentTime - lastTimeRead < 5000 &&
lastImageSha256Read === clipboardImageSha256))
lastImageSha256Read === clipboardImageSha256) ||
(lastTimeWritten &&
currentTime - lastTimeWritten < 10000 &&
lastImageSha256Written === clipboardImageSha256))
) {
return;
}
Expand Down Expand Up @@ -218,9 +219,11 @@ const writeClipboardToFile = async () => {
encoding: "utf8",
}
);
lastTextWritten = clipboardText;
} else if (clipboardType === "image") {
destinationPath = path.join(syncFolder, `${fileNumber}-${hostName}.png`);
await fs.writeFile(destinationPath, clipboardImage);
lastImageSha256Written = clipboardImageSha256;
} else if (clipboardType === "files") {
filesCount = await getTotalNumberOfFiles(clipboardFilePaths);
destinationPath = path.join(
Expand All @@ -241,6 +244,7 @@ const writeClipboardToFile = async () => {
}
}
console.log(`Clipboard written to ${destinationPath}`);
lastTimeWritten = currentTime;
lastFileNumberWritten = fileNumber;

setIconFor5Seconds("clipboard_sent");
Expand Down Expand Up @@ -454,11 +458,20 @@ const initialize = async () => {
) {
clipboardListener = (await import("clipboard-event")).default;
clipboardListener.startListening();
clipboardListener.on(
"change",
clipboardListener.on("change", () => {
// Prevents duplicated clipboard events
const currentTime = Date.now();
if (
lastTimeClipboardChecked &&
currentTime - lastTimeClipboardChecked < 1000
) {
return;
}
lastTimeClipboardChecked = currentTime;

// Wait 100ms so that clipboard is fully written
() => setTimeout(writeClipboardToFile, 100)
);
setTimeout(writeClipboardToFile, 100);
});
}

if (
Expand Down

0 comments on commit 44de073

Please sign in to comment.