Skip to content

Commit 5aabeb6

Browse files
committed
fix(browser): relay website Copy buttons to the controlling viewer
1 parent e455200 commit 5aabeb6

6 files changed

Lines changed: 207 additions & 24 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ function makeSession(
133133
if (focusedFrame) focusedFrame.copy = keyCopy || focusedFrame.copy;
134134
else pageCopy = keyCopy || pageCopy;
135135
}
136+
if (
137+
method === "Input.dispatchMouseEvent" &&
138+
(input as { type?: string }).type === "mouseReleased"
139+
) {
140+
if (focusedFrame) focusedFrame.copy = clickCopy || focusedFrame.copy;
141+
else pageCopy = clickCopy || pageCopy;
142+
}
136143
if (method === "Runtime.evaluate" && (input as { contextId?: number }).contextId === 7) {
137144
const value = frame ? frame.copy : pageCopy;
138145
if (frame) frame.copy = "";
@@ -302,6 +309,7 @@ const contextRequests: Array<{ profileId: string; isolated: boolean }> = [];
302309
let contextGate: PromiseWithResolvers<void> | null = null;
303310
let pageCopy = "";
304311
let keyCopy = "";
312+
let clickCopy = "";
305313
let focusedFrame: ReturnType<typeof makeFrame> | null = null;
306314
let contextFailure: Error | null = null;
307315
let pageSetupFailure: Error | null = null;
@@ -456,6 +464,7 @@ beforeEach(() => {
456464
initialNavigation = null;
457465
pageCopy = "";
458466
keyCopy = "";
467+
clickCopy = "";
459468
focusedFrame = null;
460469
desktopTabs.clear();
461470
shownDesktopTabs.clear();
@@ -1287,7 +1296,85 @@ it.effect.each([
12871296
).pipe(Effect.provide(layer)),
12881297
);
12891298

1290-
it.live("only the controlling viewer's copy and cut keys reach its clipboard", () =>
1299+
it.live("clipboard capture expires with the trusted click, even when propagation stops", () =>
1300+
Effect.scoped(
1301+
Effect.gen(function* () {
1302+
yield* ready;
1303+
const session = contexts[0]!.sessions[0]!;
1304+
const scripts = session.send.mock.calls.flatMap(([method, input]) =>
1305+
method === "Page.addScriptToEvaluateOnNewDocument"
1306+
? [input as { source: string; worldName?: string }]
1307+
: [],
1308+
);
1309+
const listeners = new Map<string, Array<(event: object) => void>>();
1310+
const dispatch = (type: string, event: object) => {
1311+
for (const listener of listeners.get(type) ?? []) listener(event);
1312+
};
1313+
const world = (values: object) =>
1314+
NodeVM.createContext({
1315+
Event: { NONE: 0 },
1316+
addEventListener: (type: string, listener: (event: object) => void) => {
1317+
listeners.set(type, [...(listeners.get(type) ?? []), listener]);
1318+
},
1319+
...values,
1320+
});
1321+
const isolated = world({
1322+
document: { activeElement: null, getSelection: () => "selected" },
1323+
HTMLInputElement: String,
1324+
HTMLTextAreaElement: String,
1325+
});
1326+
const main = world({
1327+
Clipboard: class {
1328+
writeText() {
1329+
return Promise.resolve();
1330+
}
1331+
},
1332+
CustomEvent: class {
1333+
readonly type: string;
1334+
readonly detail: string;
1335+
constructor(type: string, options: { detail: string }) {
1336+
this.type = type;
1337+
this.detail = options.detail;
1338+
}
1339+
},
1340+
dispatchEvent: (event: { type: string }) => dispatch(event.type, event),
1341+
});
1342+
for (const script of scripts)
1343+
NodeVM.runInContext(script.source, script.worldName ? isolated : main);
1344+
const take = () => NodeVM.runInContext("takeCopy()", isolated);
1345+
const write = () => NodeVM.runInContext('new Clipboard().writeText("button")', main);
1346+
expect(NodeVM.runInContext("typeof takeCopy", main)).toBe("undefined");
1347+
const click = { isTrusted: true, eventPhase: 1 };
1348+
dispatch("click", click);
1349+
yield* Effect.promise(write);
1350+
expect(take()).toBe("button");
1351+
dispatch("copy", { isTrusted: true });
1352+
expect(take()).toBe("selected");
1353+
dispatch("copy", {
1354+
isTrusted: true,
1355+
defaultPrevented: true,
1356+
clipboardData: { getData: () => "override" },
1357+
});
1358+
expect(take()).toBe("override");
1359+
click.eventPhase = 0;
1360+
yield* Effect.promise(write);
1361+
dispatch("copy", { isTrusted: true });
1362+
expect(take()).toBe("");
1363+
click.isTrusted = false;
1364+
click.eventPhase = 1;
1365+
dispatch("click", click);
1366+
yield* Effect.promise(write);
1367+
dispatch("t3-preview-copy-text", { detail: "redispatched from a timer" });
1368+
expect(take()).toBe("");
1369+
dispatch("click", { isTrusted: false, eventPhase: 1 });
1370+
yield* Effect.promise(write);
1371+
dispatch("t3-preview-copy-text", { detail: "forged outside a click" });
1372+
expect(take()).toBe("");
1373+
}),
1374+
).pipe(Effect.provide(layer)),
1375+
);
1376+
1377+
it.live("only the controlling viewer's copy keys and clicks offer clipboard text", () =>
12911378
Effect.scoped(
12921379
Effect.gen(function* () {
12931380
const { browser, tabId } = yield* ready;
@@ -1319,6 +1406,19 @@ it.live("only the controlling viewer's copy and cut keys reach its clipboard", (
13191406
keyCopy = "";
13201407
yield* viewer.input({ type: "key", action: "down", key: "c", code: "KeyC", modifiers: 4 });
13211408
expect(yield* clipboard(viewer.output)).toEqual([]);
1409+
clickCopy = "button copy";
1410+
yield* watcher.input({ type: "mouse", action: "up", button: "left", x: 10, y: 10 });
1411+
expect(yield* clipboard(watcher.output)).toEqual([]);
1412+
yield* viewer.input({ type: "mouse", action: "down", button: "left", x: 10, y: 10 });
1413+
expect(yield* clipboard(viewer.output)).toEqual([]);
1414+
yield* viewer.input({ type: "mouse", action: "up", button: "left", x: 10, y: 10 });
1415+
expect(yield* clipboard(viewer.output)).toEqual([
1416+
{ _tag: "clipboard", text: "button copy", requireGesture: true },
1417+
]);
1418+
clickCopy = "";
1419+
pageCopy = "stale agent click";
1420+
yield* viewer.input({ type: "mouse", action: "up", button: "left", x: 10, y: 10 });
1421+
expect(yield* clipboard(viewer.output)).toEqual([]);
13221422
expect(yield* clipboard(watcher.output)).toEqual([]);
13231423
}),
13241424
).pipe(Effect.provide(layer)),

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export type ServerBrowserViewerOutput =
189189
readonly y: number;
190190
readonly editable: boolean;
191191
}
192-
| { readonly _tag: "clipboard"; readonly text: string }
192+
| { readonly _tag: "clipboard"; readonly text: string; readonly requireGesture?: boolean }
193193
| {
194194
readonly _tag: "pointer";
195195
readonly phase: "move" | "click";
@@ -423,10 +423,34 @@ const editingCommand = (key: string, modifiers: number) => {
423423

424424
const CLIPBOARD_TEXT_LIMIT = 1024 * 1024;
425425
const CLIPBOARD_WORLD = "t3-preview-clipboard";
426-
// Chromium shares its clipboard across contexts; capture real copy/cut keys in a page-inaccessible world instead.
426+
// This is only a text signal; the isolated world accepts it during a trusted click,
427+
// and only viewer input reads it. There is no binding reply or user activation.
428+
const CLIPBOARD_WRITE_SCRIPT = `(() => {
429+
if (typeof Clipboard === "undefined") return;
430+
const writeText = Clipboard.prototype.writeText;
431+
const emit = dispatchEvent.bind(globalThis);
432+
const Signal = CustomEvent;
433+
const apply = Reflect.apply;
434+
Clipboard.prototype.writeText = function(text) {
435+
const result = apply(writeText, this, [text]);
436+
if (typeof text === "string")
437+
emit(new Signal("t3-preview-copy-text", { detail: text.slice(0, ${CLIPBOARD_TEXT_LIMIT}) }));
438+
return result;
439+
};
440+
})();`;
441+
// Chromium shares its clipboard across contexts; capture event text instead.
427442
const CLIPBOARD_SCRIPT = `(() => {
428443
let keying = false;
444+
let click = null;
429445
let copied = "";
446+
addEventListener("click", (event) => {
447+
if (event.isTrusted) click = event;
448+
}, true);
449+
// eventPhase returns to NONE even when propagation is stopped. Timers cannot reuse a click.
450+
const clicking = () => click !== null && click.isTrusted && click.eventPhase !== Event.NONE;
451+
addEventListener("t3-preview-copy-text", (event) => {
452+
if (clicking() && typeof event.detail === "string") record(event.detail);
453+
});
430454
// The same Ctrl or Cmd + C or X that editingCommand sends as copy or cut.
431455
addEventListener("keydown", (event) => {
432456
if (!event.isTrusted || event.altKey || event.ctrlKey === event.metaKey) return;
@@ -449,11 +473,11 @@ const CLIPBOARD_SCRIPT = `(() => {
449473
// Capturing at window runs before page handlers can stop the event, and
450474
// before cut removes the selection.
451475
const snapshot = (event) => {
452-
if (keying && event.isTrusted) record(selection());
476+
if ((keying || clicking()) && event.isTrusted) record(selection());
453477
};
454478
// Bubbling back to window picks up text a page handler set instead.
455479
const replace = (event) => {
456-
if (keying && event.isTrusted && event.defaultPrevented) record(event.clipboardData?.getData("text/plain"));
480+
if ((keying || clicking()) && event.isTrusted && event.defaultPrevented) record(event.clipboardData?.getData("text/plain"));
457481
};
458482
for (const type of ["copy", "cut"]) {
459483
addEventListener(type, snapshot, true);
@@ -1277,6 +1301,10 @@ const make = Effect.gen(function* () {
12771301
worldName: CLIPBOARD_WORLD,
12781302
runImmediately: true,
12791303
});
1304+
await cdp.send("Page.addScriptToEvaluateOnNewDocument", {
1305+
source: CLIPBOARD_WRITE_SCRIPT,
1306+
runImmediately: true,
1307+
});
12801308
};
12811309

12821310
/** Out-of-process frames need their own copy-recording session until target closure. */
@@ -2399,6 +2427,8 @@ const make = Effect.gen(function* () {
23992427
const button = ["none", "left", "middle", "right"].includes(String(message.button))
24002428
? (message.button as "none" | "left" | "middle" | "right")
24012429
: "none";
2430+
const copying = !tab.desktop && action === "up" && button === "left";
2431+
if (copying) await takeCopiedText(tab);
24022432
await session.send("Input.dispatchMouseEvent", {
24032433
type,
24042434
x: num(message.x),
@@ -2408,6 +2438,10 @@ const make = Effect.gen(function* () {
24082438
clickCount: num(message.clickCount, type === "mouseMoved" ? 0 : 1),
24092439
modifiers,
24102440
});
2441+
if (copying) {
2442+
const copied = await takeCopiedText(tab);
2443+
if (copied) viewer.push({ _tag: "clipboard", text: copied, requireGesture: true });
2444+
}
24112445
if (button !== "none") {
24122446
if (action === "down")
24132447
viewer.pressedButtons.set(button, { x: num(message.x), y: num(message.y) });

‎apps/web/src/browser/ServerBrowserSurface.browser.test.tsx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ vi.mock(import("~/state/previewStream"), async (original) => ({
3131
}));
3232

3333
import { ServerBrowserSurface } from "./ServerBrowserSurface";
34+
import { toastManager } from "~/components/ui/toast";
3435

3536
function setup() {
3637
mocks.sent.length = 0;
@@ -73,6 +74,26 @@ async function renderSurface(controller: PreviewStreamControl["controller"]) {
7374
const keys = () => mocks.sent.filter((input) => input.type === "key");
7475

7576
describe("ServerBrowserSurface clipboard commands", () => {
77+
it("offers pointer copies locally before writing, while keys still write directly", async () => {
78+
setup();
79+
const { screen } = await renderSurface("you");
80+
await using _screen = screen;
81+
using write = vi.spyOn(navigator.clipboard, "writeText").mockResolvedValue();
82+
using toast = vi.spyOn(toastManager, "add").mockReturnValue("page-copy");
83+
using close = vi.spyOn(toastManager, "close").mockImplementation(() => undefined);
84+
const handlers = mocks.createClient.mock.calls.at(-1)![1];
85+
await act(() => handlers.onClipboard?.("button", true));
86+
expect(write).not.toHaveBeenCalled();
87+
const offer = toast.mock.calls.at(-1)![0];
88+
expect(offer.title).toBe("The page copied text");
89+
await act(() => offer.actionProps!.onClick!(new MouseEvent("click") as never));
90+
expect(write).toHaveBeenCalledExactlyOnceWith("button");
91+
expect(close).toHaveBeenCalledWith("page-copy");
92+
await act(() => handlers.onClipboard?.("keys", false));
93+
expect(write).toHaveBeenLastCalledWith("keys");
94+
expect(toast).toHaveBeenCalledTimes(1);
95+
});
96+
7697
it("sends a native Copy or Cut menu command to the page once", async () => {
7798
setup();
7899
const { screen, input, canvas } = await renderSurface("you");

‎apps/web/src/browser/ServerBrowserSurface.tsx‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,27 @@ const pressedButtonOf = (buttons: number): PreviewStreamMouseButton =>
103103
const historyDeltaOf = (button: number): -1 | 1 | null =>
104104
button === 3 ? -1 : button === 4 ? 1 : null;
105105

106-
/** Browsers may refuse a write that arrives after the key press; a toast button is a fresh gesture. */
107-
async function copyPageText(text: string) {
108-
try {
109-
await navigator.clipboard.writeText(text);
110-
} catch {
111-
const id = toastManager.add({
112-
type: "info",
113-
title: "The page copied text",
114-
actionProps: {
115-
children: "Copy",
116-
onClick: () => {
117-
toastManager.close(id);
118-
void navigator.clipboard.writeText(text).catch(() => undefined);
119-
},
120-
},
121-
});
106+
/** A local Copy click also works when the relay arrives without browser activation. */
107+
async function copyPageText(text: string, requireGesture: boolean) {
108+
if (!requireGesture) {
109+
try {
110+
await navigator.clipboard.writeText(text);
111+
return;
112+
} catch {
113+
// Offer a fresh local gesture when the browser refused the relayed key.
114+
}
122115
}
116+
const id = toastManager.add({
117+
type: "info",
118+
title: "The page copied text",
119+
actionProps: {
120+
children: "Copy",
121+
onClick: () => {
122+
toastManager.close(id);
123+
void navigator.clipboard.writeText(text).catch(() => undefined);
124+
},
125+
},
126+
});
123127
}
124128

125129
function offerDownload(download: PreviewStreamDownload) {
@@ -396,7 +400,7 @@ export function ServerBrowserSurface(props: {
396400
const placed = placeAgentCursor(pointer, canvasRef.current, viewportRef.current);
397401
if (placed) setAgentCursor(placed);
398402
},
399-
onClipboard: (text) => void copyPageText(text),
403+
onClipboard: (text, requireGesture) => void copyPageText(text, requireGesture),
400404
onDownload: offerDownload,
401405
onPopup: popupOpened,
402406
onFileChooser: setFileChooser,

‎packages/client-runtime/src/preview/serverBrowserStream.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ describe("preview stream control", () => {
134134
});
135135
});
136136

137+
describe("preview stream clipboard", () => {
138+
beforeEach(() => vi.stubGlobal("WebSocket", FakeSocket));
139+
afterEach(() => vi.unstubAllGlobals());
140+
141+
it("preserves the local gesture requirement for pointer copies", () => {
142+
const onClipboard = vi.fn();
143+
const client = createPreviewStreamClient(target, {
144+
onFrame: vi.fn(),
145+
onClipboard,
146+
onViewport: vi.fn(),
147+
onConnectedChange: vi.fn(),
148+
onUnauthorized: vi.fn(),
149+
});
150+
FakeSocket.current.message(
151+
JSON.stringify({ type: "clipboard", text: "button", requireGesture: true }),
152+
);
153+
expect(onClipboard).toHaveBeenLastCalledWith("button", true);
154+
FakeSocket.current.message(JSON.stringify({ type: "clipboard", text: "keys" }));
155+
expect(onClipboard).toHaveBeenLastCalledWith("keys", false);
156+
client.stop();
157+
});
158+
});
159+
137160
describe("preview stream downloads", () => {
138161
beforeEach(() => vi.stubGlobal("WebSocket", FakeSocket));
139162
afterEach(() => vi.unstubAllGlobals());

‎packages/client-runtime/src/preview/serverBrowserStream.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export interface PreviewStreamEvents {
188188
readonly onProbe?: (probe: PreviewStreamProbe) => void;
189189
readonly onPointer?: (pointer: PreviewStreamPointer) => void;
190190
readonly onControl?: (control: PreviewStreamControl) => void;
191-
readonly onClipboard?: (text: string) => void;
191+
readonly onClipboard?: (text: string, requireGesture: boolean) => void;
192192
readonly onDownload?: (download: PreviewStreamDownload) => void;
193193
readonly onPopup?: (tabId: string) => void;
194194
readonly onFileChooser?: (chooser: PreviewStreamFileChooser | null) => void;
@@ -270,6 +270,7 @@ export function createPreviewStreamClient(
270270
generation,
271271
dialog,
272272
text,
273+
requireGesture,
273274
id,
274275
fileName,
275276
sizeBytes,
@@ -307,7 +308,7 @@ export function createPreviewStreamClient(
307308
} else if (type === "popup" && typeof tabId === "string") {
308309
events.onPopup?.(tabId);
309310
} else if (type === "clipboard" && typeof text === "string") {
310-
events.onClipboard?.(text);
311+
events.onClipboard?.(text, requireGesture === true);
311312
} else if (
312313
type === "download" &&
313314
typeof id === "string" &&

0 commit comments

Comments
 (0)