Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Paste the local clipboard into the desktop viewer with ⌘V / Ctrl+V

AdityaUmale42d4fd29a862
diff --git a/src/lib/desktop-connection.test.ts b/src/lib/desktop-connection.test.ts
index 12fa09449e..ab62da4fa3 100644
--- a/src/lib/desktop-connection.test.ts
+++ b/src/lib/desktop-connection.test.ts
@@ -7,6 +7,9 @@ const clients = vi.hoisted(() => [] as Array<{
password: string;
listeners: Map<string, Array<(event: { detail: { text: string } }) => void>>;
disconnected: number;
+ viewOnly: boolean;
+ pasted: string[];
+ keys: unknown[][];
emit(type: string, text?: string): void;
}>);
vi.mock("@novnc/novnc", () => ({
@@ -15,6 +18,9 @@ vi.mock("@novnc/novnc", () => ({
password: string;
listeners = new Map<string, Array<(event: { detail: { text: string } }) => void>>();
disconnected = 0;
+ viewOnly = false;
+ pasted: string[] = [];
+ keys: unknown[][] = [];
scaleViewport = false;
background = "";
constructor(_screen: HTMLElement, url: string, options: { credentials: { password: string } }) {
@@ -25,6 +31,12 @@ vi.mock("@novnc/novnc", () => ({
addEventListener(type: string, listener: (event: { detail: { text: string } }) => void) {
this.listeners.set(type, [...(this.listeners.get(type) ?? []), listener]);
}
+ clipboardPasteFrom(text: string) {
+ this.pasted.push(text);
+ }
+ sendKey(...key: unknown[]) {
+ this.keys.push(key);
+ }
disconnect() {
this.disconnected++;
this.emit("disconnect");
@@ -50,6 +62,7 @@ beforeEach(() => {
afterEach(() => {
vi.useRealTimers();
vi.unstubAllGlobals();
+ delete window.ogb;
});
describe("connecting to a desktop", () => {
@@ -109,3 +122,89 @@ describe("connecting to a desktop", () => {
expect(sent).toEqual([0x61, 0xff0d, 0xff09, 0x0100041f]);
});
});
+
+describe("pasting into a desktop", () => {
+ // The canvas noVNC types from sits inside the screen; its own key listener stands in for noVNC's.
+ async function desktop(platform: string, leave = new AbortController()) {
+ window.ogb = { platform } as typeof window.ogb;
+ const screen = document.createElement("div");
+ const canvas = screen.appendChild(document.createElement("canvas"));
+ const forwarded: string[] = [];
+ canvas.addEventListener("keydown", (event) => forwarded.push(event.key));
+ await connectDesktop(screen, "local/shared", { signal: leave.signal, events: events() });
+ const client = clients[0]!;
+ const press = (key: string, init: KeyboardEventInit = {}) =>
+ canvas.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true, ...init }));
+ const paste = (text: string) => {
+ const event = new Event("paste", { bubbles: true, cancelable: true });
+ Object.defineProperty(event, "clipboardData", { value: { getData: (type: string) => type === "text/plain" ? text : "" } });
+ canvas.dispatchEvent(event);
+ return event;
+ };
+ const lift = (code: string) => canvas.dispatchEvent(new KeyboardEvent("keyup", { code, bubbles: true }));
+ return { client, forwarded, press, lift, paste };
+ }
+
+ it("⌘V on a Mac puts the Mac's text on the desktop's clipboard and presses Ctrl+V there, not ⌘", async () => {
+ const { client, forwarded, press, lift, paste } = await desktop("darwin");
+ press("Meta", { code: "MetaLeft", metaKey: true });
+ press("v", { code: "KeyV", metaKey: true });
+ lift("KeyV"); // the app's Edit menu can deliver the paste after V is up
+ const event = paste("from the Mac");
+ expect(forwarded).toEqual(["Meta"]);
+ expect(event.defaultPrevented).toBe(true);
+ expect(client.pasted).toEqual(["from the Mac"]);
+ expect(client.keys).toEqual([[0xffe3, "ControlLeft", true], [0xffe9, "MetaLeft", false], [0x76, "KeyV"], [0xffe3, "ControlLeft", false]]);
+ });
+
+ it("Ctrl+V elsewhere pastes the same way, leaving the held Ctrl to the person's own release", async () => {
+ const { client, forwarded, press, paste } = await desktop("win32");
+ press("Control", { code: "ControlLeft", ctrlKey: true });
+ press("v", { code: "KeyV", ctrlKey: true });
+ paste("from Windows");
+ expect(forwarded).toEqual(["Control"]);
+ expect(client.pasted).toEqual(["from Windows"]);
+ expect(client.keys).toEqual([[0xffe3, "ControlLeft", true], [0x76, "KeyV"]]);
+ });
+
+ it("once ⌘ is let go, a paste from the menu presses the whole Ctrl+V", async () => {
+ const { client, press, lift, paste } = await desktop("darwin");
+ press("Meta", { code: "MetaLeft", metaKey: true });
+ lift("MetaLeft");
+ paste("menu");
+ expect(client.keys).toEqual([[0xffe3, "ControlLeft", true], [0x76, "KeyV"], [0xffe3, "ControlLeft", false]]);
+ });
+
+ it("a clipboard with no text still presses Ctrl+V, so the desktop pastes its own clipboard as before", async () => {
+ const { client, paste } = await desktop("linux");
+ paste("");
+ expect(client.pasted).toEqual([]);
+ expect(client.keys).toEqual([[0xffe3, "ControlLeft", true], [0x76, "KeyV"], [0xffe3, "ControlLeft", false]]);
+ });
+
+ it("other shortcuts still reach the desktop as keys: Ctrl+V on a Mac, ⌘⇧V", async () => {
+ const { client, forwarded, press } = await desktop("darwin");
+ press("v", { code: "KeyV", ctrlKey: true });
+ press("V", { code: "KeyV", metaKey: true, shiftKey: true });
+ expect(forwarded).toEqual(["v", "V"]);
+ expect(client.keys).toEqual([]);
+ });
+
+ it("a view-only desktop gets nothing", async () => {
+ const { client, paste } = await desktop("darwin");
+ client.viewOnly = true;
+ paste("secret");
+ expect(client.pasted).toEqual([]);
+ expect(client.keys).toEqual([]);
+ });
+
+ it("stops once the viewer has left", async () => {
+ const leave = new AbortController();
+ const { client, forwarded, press, paste } = await desktop("darwin", leave);
+ leave.abort();
+ press("v", { code: "KeyV", metaKey: true });
+ paste("late");
+ expect(forwarded).toEqual(["v"]);
+ expect(client.pasted).toEqual([]);
+ });
+});
diff --git a/src/lib/desktop-connection.ts b/src/lib/desktop-connection.ts
index 1114cd0329..aa8cdc18a6 100644
--- a/src/lib/desktop-connection.ts
+++ b/src/lib/desktop-connection.ts
@@ -1,5 +1,6 @@
import RFB from "@novnc/novnc";
import { DESKTOP_VIEWER_TARGET } from "../../shared/desktop-viewer";
+import { isMacPlatform } from "./keyboard-shortcuts";
export interface DesktopConnectionEvents {
connected(): void;
@@ -62,9 +63,50 @@ export async function connectDesktop(screen: HTMLElement, target: string, option
client.addEventListener("clipboard", (event) => {
if (!cancelled.aborted) events.clipboard?.(event.detail.text);
});
+ forwardPaste(screen, client, cancelled);
return client;
}
+const XK_CONTROL_L = 0xffe3;
+const XK_CONTROL_R = 0xffe4;
+const XK_V = 0x76;
+// noVNC sends a Mac's ⌘ keys to the desktop as Alt_L (left) and Super_L (right).
+const MAC_COMMAND_KEYSYMS: Record<string, number> = { MetaLeft: 0xffe9, MetaRight: 0xffeb };
+
+/** The local paste shortcut (⌘V on a Mac, Ctrl+V elsewhere) puts the local clipboard's text on the desktop's
+ * clipboard and presses Ctrl+V there. noVNC would send the shortcut on as raw keys and stop the browser pasting, so
+ * the screen takes it first and lets the browser's paste event bring the text, with no clipboard permission prompt. */
+function forwardPaste(screen: HTMLElement, client: RFB, signal: AbortSignal): void {
+ const mac = isMacPlatform();
+ const pasteModifier = mac ? /^Meta(Left|Right)$/ : /^Control(Left|Right)$/;
+ // The paste modifier the person is holding down, which noVNC has already sent on.
+ let held: string | null = null;
+ screen.addEventListener("keydown", (event) => {
+ if (pasteModifier.test(event.code)) held = event.code;
+ const modifier = mac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey;
+ if (client.viewOnly || !modifier || event.altKey || event.shiftKey || event.key.toLowerCase() !== "v") return;
+ event.stopPropagation();
+ }, { capture: true, signal });
+ screen.addEventListener("keyup", (event) => { if (event.code === held) held = null; }, { capture: true, signal });
+ screen.addEventListener("focusout", () => { held = null; }, { signal });
+ screen.addEventListener("paste", (event) => {
+ event.preventDefault();
+ pasteOnDesktop(client, event.clipboardData?.getData("text/plain") ?? "", held);
+ }, { signal });
+}
+
+/** `held` is the modifier the person holds that noVNC already sent down: Ctrl stays down for their own release; a Mac's
+ * ⌘ is let go once Ctrl is down, so the desktop sees Ctrl+V and not a lone ⌘ tap. */
+function pasteOnDesktop(client: RFB, text: string, held: string | null): void {
+ if (client.viewOnly) return;
+ if (text) client.clipboardPasteFrom(text);
+ const ctrl = held?.startsWith("Control") ? held : "ControlLeft";
+ client.sendKey(ctrl === "ControlRight" ? XK_CONTROL_R : XK_CONTROL_L, ctrl, true);
+ if (held && held in MAC_COMMAND_KEYSYMS) client.sendKey(MAC_COMMAND_KEYSYMS[held]!, held, false);
+ client.sendKey(XK_V, "KeyV");
+ if (ctrl !== held) client.sendKey(XK_CONTROL_L, ctrl, false);
+}
+
/** Explicit text entry works with phone keyboards without reading the clipboard; non-Latin text uses Unicode keysyms. */
export function typeOnDesktop(client: RFB, text: string): void {
for (const char of text) {