Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: guard clipboard paste fallback against key-repeat and stale arms

Tw93783135f8e3b2
diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.js
index 632dc932..d3246154 100644
--- a/src-tauri/src/inject/event.js
+++ b/src-tauri/src/inject/event.js
@@ -207,6 +207,11 @@ function insertTextIntoEditableElement(element, text) {
}
let clipboardPasteFallbackTarget;
+let clipboardPasteFallbackArmedAt = 0;
+// An armed fallback older than this is a leftover from a keyup the window
+// never saw (alt-tab mid-press); firing it on a later plain "v" keyup would
+// paste unexpectedly.
+const CLIPBOARD_PASTE_FALLBACK_TTL_MS = 5000;
function pasteClipboardText(activeElement) {
const readText = navigator.clipboard?.readText;
@@ -253,8 +258,16 @@ function handleClipboardShortcut(event) {
if (key === "v" && canPasteIntoEditableElement(activeElement)) {
// Let the native WebView paste event run first so images, files, and rich
// clipboard formats remain intact. If the platform does not emit paste,
- // keyup applies the existing text-only fallback.
- clipboardPasteFallbackTarget = activeElement;
+ // keyup applies the existing text-only fallback. Key-repeat must not
+ // re-arm: after a native paste already fired and disarmed the fallback,
+ // a repeat keydown re-arming it would make keyup paste text a second
+ // time. Repeats only refresh the TTL of a still-armed target.
+ if (!event.repeat) {
+ clipboardPasteFallbackTarget = activeElement;
+ clipboardPasteFallbackArmedAt = Date.now();
+ } else if (clipboardPasteFallbackTarget === activeElement) {
+ clipboardPasteFallbackArmedAt = Date.now();
+ }
return false;
}
@@ -276,9 +289,11 @@ function handleClipboardPasteFallback(event) {
}
const activeElement = clipboardPasteFallbackTarget;
+ const armedAt = clipboardPasteFallbackArmedAt;
clipboardPasteFallbackTarget = undefined;
if (
!activeElement ||
+ Date.now() - armedAt > CLIPBOARD_PASTE_FALLBACK_TTL_MS ||
document.activeElement !== activeElement ||
!canPasteIntoEditableElement(activeElement)
) {
diff --git a/tests/unit/event-clipboard-shortcuts.test.js b/tests/unit/event-clipboard-shortcuts.test.js
index 905c571b..cc509ccb 100644
--- a/tests/unit/event-clipboard-shortcuts.test.js
+++ b/tests/unit/event-clipboard-shortcuts.test.js
@@ -63,6 +63,8 @@ function loadEventHelpers({
const context = {
console,
URL,
+ // Main-realm Date so tests can shift time seen by the fallback TTL.
+ Date,
Event: class {},
Notification: function Notification() {},
setTimeout,
@@ -150,6 +152,8 @@ function loadEventHelpers({
return {
...context,
+ // Live contextified global: assigning properties here reaches the vm.
+ sandbox: context,
eventListeners,
execCommandCalls,
invokeCalls,
@@ -291,6 +295,49 @@ describe("event clipboard shortcuts", () => {
expect(context.invokeCalls).toEqual([]);
});
+ it("does not re-arm the fallback from key-repeat after native paste fired", async () => {
+ const input = createElement("input");
+ const context = loadEventHelpers({ activeElement: input });
+ const shortcutHandler = getClipboardShortcutHandler(context);
+ const fallbackHandler = getClipboardPasteFallbackHandler(context);
+ const pasteHandler = getPasteHandler(context);
+ const nativePasteEvent = {
+ clipboardData: { types: ["image/png"], getData: () => "" },
+ preventDefault: vi.fn(),
+ stopImmediatePropagation: vi.fn(),
+ };
+
+ shortcutHandler(createKeyEvent("v"));
+ pasteHandler(nativePasteEvent);
+ // Held key: repeated keydown events arrive after the native paste.
+ shortcutHandler(createKeyEvent("v", { repeat: true }));
+ shortcutHandler(createKeyEvent("v", { repeat: true }));
+ fallbackHandler(createKeyEvent("v"));
+ await Promise.resolve();
+
+ // The image already pasted natively; keyup must not paste text on top.
+ expect(context.clipboardReadCalls).toEqual([]);
+ expect(context.execCommandCalls).toEqual([]);
+ });
+
+ it("expires a stale armed fallback instead of pasting on a later keyup", async () => {
+ const input = createElement("input");
+ const context = loadEventHelpers({ activeElement: input });
+ const shortcutHandler = getClipboardShortcutHandler(context);
+ const fallbackHandler = getClipboardPasteFallbackHandler(context);
+
+ shortcutHandler(createKeyEvent("v"));
+ // Keyup was lost (focus left mid-press); much later a plain "v" keyup
+ // arrives on the same element.
+ const realNow = Date.now();
+ context.sandbox.Date = { now: () => realNow + 10_000 };
+ fallbackHandler(createKeyEvent("v"));
+ await Promise.resolve();
+
+ expect(context.clipboardReadCalls).toEqual([]);
+ expect(context.execCommandCalls).toEqual([]);
+ });
+
it("does not read clipboard for synthetic paste shortcuts", async () => {
const input = createElement("input");
const context = loadEventHelpers({ activeElement: input });