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: support safe paste shortcut on linux and windows

Tw936ce8e627a7b2
diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.js
index 256f250b..d0aa565c 100644
--- a/src-tauri/src/inject/event.js
+++ b/src-tauri/src/inject/event.js
@@ -70,6 +70,26 @@ function hasSelectedText() {
return Boolean(window.getSelection?.()?.toString());
}
+const NON_TEXT_INPUT_TYPES = new Set([
+ "button",
+ "checkbox",
+ "color",
+ "file",
+ "hidden",
+ "image",
+ "radio",
+ "range",
+ "reset",
+ "submit",
+]);
+
+function isTextInputElement(element) {
+ return (
+ element?.tagName === "INPUT" &&
+ !NON_TEXT_INPUT_TYPES.has((element.type || "text").toLowerCase())
+ );
+}
+
function selectEditableElement(element) {
if (typeof element.select === "function") {
element.select();
@@ -90,8 +110,81 @@ function selectEditableElement(element) {
return false;
}
+function canPasteIntoEditableElement(element) {
+ if (!isEditableElement(element)) return false;
+
+ if (element.tagName === "INPUT") {
+ return (
+ isTextInputElement(element) &&
+ element.disabled !== true &&
+ element.readOnly !== true
+ );
+ }
+
+ if (element.tagName === "TEXTAREA") {
+ return element.disabled !== true && element.readOnly !== true;
+ }
+
+ return true;
+}
+
+function insertTextIntoEditableElement(element, text) {
+ if (!text) return false;
+
+ if (document.execCommand("insertText", false, text)) {
+ return true;
+ }
+
+ if (
+ element &&
+ (isTextInputElement(element) || element.tagName === "TEXTAREA") &&
+ typeof element.setRangeText === "function"
+ ) {
+ const valueLength =
+ typeof element.value === "string" ? element.value.length : 0;
+ const start =
+ typeof element.selectionStart === "number"
+ ? element.selectionStart
+ : valueLength;
+ const end =
+ typeof element.selectionEnd === "number" ? element.selectionEnd : start;
+ element.setRangeText(text, start, end, "end");
+ element.dispatchEvent?.(new Event("input", { bubbles: true }));
+ return true;
+ }
+
+ return false;
+}
+
+function runBrowserPasteCommand() {
+ try {
+ return document.execCommand("paste") === true;
+ } catch (error) {
+ return false;
+ }
+}
+
+function pasteClipboardText(activeElement) {
+ if (runBrowserPasteCommand()) {
+ return;
+ }
+
+ const readText = navigator.clipboard?.readText;
+ if (typeof readText !== "function") {
+ return;
+ }
+
+ readText
+ .call(navigator.clipboard)
+ .then((text) => {
+ insertTextIntoEditableElement(activeElement, text);
+ })
+ .catch(() => {});
+}
+
function handleClipboardShortcut(event) {
if (
+ event.isTrusted !== true ||
!isNonMacDesktop() ||
!event.ctrlKey ||
event.metaKey ||
@@ -117,6 +210,12 @@ function handleClipboardShortcut(event) {
return true;
}
+ if (key === "v" && canPasteIntoEditableElement(activeElement)) {
+ event.preventDefault();
+ pasteClipboardText(activeElement);
+ return true;
+ }
+
if (key === "a" && isEditable && selectEditableElement(activeElement)) {
event.preventDefault();
return true;
diff --git a/tests/unit/event-clipboard-shortcuts.test.js b/tests/unit/event-clipboard-shortcuts.test.js
index 1622975d..3187a776 100644
--- a/tests/unit/event-clipboard-shortcuts.test.js
+++ b/tests/unit/event-clipboard-shortcuts.test.js
@@ -29,6 +29,9 @@ function createElement(tagName = "div") {
function loadEventHelpers({
activeElement = createElement("body"),
selectionText = "",
+ clipboardText = "clipboard text",
+ clipboardReadRejects = false,
+ pasteCommandSucceeds = false,
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
} = {}) {
const source = fs.readFileSync(
@@ -40,6 +43,7 @@ function loadEventHelpers({
const elementsById = new Map();
const execCommandCalls = [];
const invokeCalls = [];
+ const clipboardReadCalls = [];
const body = createElement("body");
body.scrollHeight = 0;
body.appendChild = (child) => {
@@ -68,6 +72,15 @@ function loadEventHelpers({
navigator: {
userAgent,
language: "en-US",
+ clipboard: {
+ readText: () => {
+ clipboardReadCalls.push([]);
+ if (clipboardReadRejects) {
+ return Promise.reject(new Error("clipboard denied"));
+ }
+ return Promise.resolve(clipboardText);
+ },
+ },
},
window: {
history: {
@@ -127,6 +140,9 @@ function loadEventHelpers({
activeElement,
execCommand: (command, showUI, value) => {
execCommandCalls.push([command, showUI, value]);
+ if (command === "paste") {
+ return pasteCommandSucceeds;
+ }
return true;
},
},
@@ -136,7 +152,13 @@ function loadEventHelpers({
runInNewContext(source, context);
eventListeners.DOMContentLoaded[0].handler();
- return { ...context, eventListeners, execCommandCalls, invokeCalls };
+ return {
+ ...context,
+ eventListeners,
+ execCommandCalls,
+ invokeCalls,
+ clipboardReadCalls,
+ };
}
function getClipboardShortcutHandler(context) {
@@ -152,6 +174,7 @@ function createKeyEvent(key, overrides = {}) {
metaKey: false,
altKey: false,
shiftKey: false,
+ isTrusted: true,
preventDefault: vi.fn(),
...overrides,
};
@@ -187,7 +210,92 @@ describe("event clipboard shortcuts", () => {
expect(selectAllEvent.preventDefault).toHaveBeenCalled();
});
- it("leaves paste and macOS shortcuts untouched", () => {
+ it("pastes clipboard text into editable elements without Tauri clipboard IPC", async () => {
+ const input = createElement("input");
+ const context = loadEventHelpers({
+ activeElement: input,
+ clipboardText: "pasted text",
+ });
+ const handler = getClipboardShortcutHandler(context);
+ const pasteEvent = createKeyEvent("v");
+
+ handler(pasteEvent);
+ await Promise.resolve();
+
+ expect(context.clipboardReadCalls).toEqual([[]]);
+ expect(context.execCommandCalls).toEqual([
+ ["paste", undefined, undefined],
+ ["insertText", false, "pasted text"],
+ ]);
+ expect(pasteEvent.preventDefault).toHaveBeenCalled();
+ expect(context.invokeCalls).toEqual([]);
+ });
+
+ it("uses the browser paste command first when available", async () => {
+ const input = createElement("input");
+ const context = loadEventHelpers({
+ activeElement: input,
+ pasteCommandSucceeds: true,
+ });
+ const handler = getClipboardShortcutHandler(context);
+ const pasteEvent = createKeyEvent("v");
+
+ handler(pasteEvent);
+ await Promise.resolve();
+
+ expect(context.clipboardReadCalls).toEqual([]);
+ expect(context.execCommandCalls).toEqual([["paste", undefined, undefined]]);
+ expect(pasteEvent.preventDefault).toHaveBeenCalled();
+ });
+
+ it("does not read clipboard for synthetic paste shortcuts", async () => {
+ const input = createElement("input");
+ const context = loadEventHelpers({ activeElement: input });
+ const handler = getClipboardShortcutHandler(context);
+ const pasteEvent = createKeyEvent("v", { isTrusted: false });
+
+ handler(pasteEvent);
+ await Promise.resolve();
+
+ expect(context.clipboardReadCalls).toEqual([]);
+ expect(context.execCommandCalls).toEqual([]);
+ expect(pasteEvent.preventDefault).not.toHaveBeenCalled();
+ });
+
+ it("does not read clipboard for non-text input elements", async () => {
+ const checkbox = createElement("input");
+ checkbox.type = "checkbox";
+ const context = loadEventHelpers({ activeElement: checkbox });
+ const handler = getClipboardShortcutHandler(context);
+ const pasteEvent = createKeyEvent("v");
+
+ handler(pasteEvent);
+ await Promise.resolve();
+
+ expect(context.clipboardReadCalls).toEqual([]);
+ expect(context.execCommandCalls).toEqual([]);
+ expect(pasteEvent.preventDefault).not.toHaveBeenCalled();
+ });
+
+ it("does not add Tauri fallback when async clipboard read is denied", async () => {
+ const input = createElement("input");
+ const context = loadEventHelpers({
+ activeElement: input,
+ clipboardReadRejects: true,
+ });
+ const handler = getClipboardShortcutHandler(context);
+ const pasteEvent = createKeyEvent("v");
+
+ handler(pasteEvent);
+ await Promise.resolve();
+ await Promise.resolve();
+
+ expect(context.clipboardReadCalls).toEqual([[]]);
+ expect(context.execCommandCalls).toEqual([["paste", undefined, undefined]]);
+ expect(pasteEvent.preventDefault).toHaveBeenCalled();
+ });
+
+ it("leaves non-editable paste and macOS shortcuts untouched", () => {
const windowsContext = loadEventHelpers();
const windowsHandler = getClipboardShortcutHandler(windowsContext);
const pasteEvent = createKeyEvent("v");
@@ -205,6 +313,7 @@ describe("event clipboard shortcuts", () => {
expect(pasteEvent.preventDefault).not.toHaveBeenCalled();
expect(copyEvent.preventDefault).not.toHaveBeenCalled();
+ expect(windowsContext.clipboardReadCalls).toEqual([]);
expect(windowsContext.execCommandCalls).toEqual([]);
expect(macContext.execCommandCalls).toEqual([]);
});