Skip to content

Agent Zero AI framework

Enable clipboard shortcuts in Browser visual mode

Alessandroad7925b54300
diff --git a/plugins/_browser/api/ws_browser.py b/plugins/_browser/api/ws_browser.py
index 454f5cbf8..9f4f49acc 100644
--- a/plugins/_browser/api/ws_browser.py
+++ b/plugins/_browser/api/ws_browser.py
@@ -249,6 +249,13 @@ class WsBrowser(WsHandler):
key=str(data.get("key") or ""),
text=str(data.get("text") or ""),
)
+ elif input_type == "clipboard":
+ result = await runtime.call(
+ "clipboard",
+ browser_id,
+ action=str(data.get("action") or ""),
+ text=str(data.get("text") or ""),
+ )
elif input_type == "viewport":
result = await runtime.call(
"set_viewport",
@@ -271,6 +278,15 @@ class WsBrowser(WsHandler):
except Exception as exc:
return self._error("INPUT_FAILED", str(exc), data)
+ if input_type == "clipboard":
+ response = {
+ "state": result.get("state") if isinstance(result, dict) else result,
+ "snapshot": None,
+ }
+ if isinstance(result, dict):
+ response["clipboard"] = result.get("clipboard")
+ return response
+
return {
"state": result,
"snapshot": await self._snapshot_for_result(runtime, result)
diff --git a/plugins/_browser/helpers/runtime.py b/plugins/_browser/helpers/runtime.py
index 00a3ad3b3..ff5fc0691 100644
--- a/plugins/_browser/helpers/runtime.py
+++ b/plugins/_browser/helpers/runtime.py
@@ -37,6 +37,243 @@ SCREENCAST_MAX_WIDTH = 4096
SCREENCAST_MAX_HEIGHT = 4096
VIEWPORT_SIZE_TOLERANCE = 4
VIEWPORT_REMOUNT_PAUSE_SECONDS = 0.05
+CLIPBOARD_BRIDGE_SCRIPT = r"""
+(payload) => {
+ const action = String(payload?.action || "").trim().toLowerCase();
+ const text = String(payload?.text || "");
+ const result = {
+ action,
+ text: "",
+ changed: false,
+ default_prevented: false,
+ handled: false,
+ method: "dom",
+ };
+ const textInputTypes = new Set([
+ "",
+ "email",
+ "number",
+ "password",
+ "search",
+ "tel",
+ "text",
+ "url",
+ ]);
+
+ function deepestActiveElement() {
+ let active = document.activeElement || document.body || document.documentElement;
+ while (active?.shadowRoot?.activeElement) {
+ active = active.shadowRoot.activeElement;
+ }
+ return active || document.body || document.documentElement;
+ }
+
+ function editableTarget(element) {
+ if (!element) return null;
+ if (isTextControl(element) || element.isContentEditable) return element;
+ const closest = element.closest?.("input, textarea, [contenteditable]");
+ if (closest && (isTextControl(closest) || closest.isContentEditable)) return closest;
+ return element;
+ }
+
+ function isTextControl(element) {
+ if (!element) return false;
+ const tagName = String(element.tagName || "").toLowerCase();
+ if (tagName === "textarea") {
+ return !element.disabled && !element.readOnly;
+ }
+ if (tagName !== "input") return false;
+ const type = String(element.type || "text").toLowerCase();
+ return textInputTypes.has(type) && !element.disabled && !element.readOnly;
+ }
+
+ function selectedText(element) {
+ if (isTextControl(element)) {
+ try {
+ const start = Number(element.selectionStart);
+ const end = Number(element.selectionEnd);
+ if (Number.isFinite(start) && Number.isFinite(end) && end > start) {
+ return String(element.value || "").slice(start, end);
+ }
+ } catch {}
+ return "";
+ }
+ const selection = globalThis.getSelection?.();
+ return selection ? String(selection.toString() || "") : "";
+ }
+
+ function makeClipboardData(seedText = "") {
+ let transfer = null;
+ try {
+ transfer = new DataTransfer();
+ } catch {}
+ if (transfer && seedText) {
+ transfer.setData("text/plain", seedText);
+ transfer.setData("text", seedText);
+ }
+ return transfer;
+ }
+
+ function clipboardDataText(transfer) {
+ if (!transfer) return "";
+ return String(transfer.getData("text/plain") || transfer.getData("text") || "");
+ }
+
+ function makeClipboardEvent(type, transfer) {
+ let event = null;
+ try {
+ event = new ClipboardEvent(type, {
+ bubbles: true,
+ cancelable: true,
+ clipboardData: transfer,
+ });
+ } catch {}
+ if (!event) {
+ event = new Event(type, { bubbles: true, cancelable: true });
+ }
+ if (transfer && !event.clipboardData) {
+ try {
+ Object.defineProperty(event, "clipboardData", { value: transfer });
+ } catch {}
+ }
+ return event;
+ }
+
+ function dispatchClipboardEvent(target, type, seedText = "") {
+ const transfer = makeClipboardData(seedText);
+ const event = makeClipboardEvent(type, transfer);
+ (target || document.body || document.documentElement).dispatchEvent(event);
+ return {
+ defaultPrevented: Boolean(event.defaultPrevented),
+ text: clipboardDataText(event.clipboardData || transfer),
+ };
+ }
+
+ function dispatchInputEvent(element, type, inputType, data = null) {
+ let event = null;
+ try {
+ event = new InputEvent(type, {
+ bubbles: true,
+ cancelable: type === "beforeinput",
+ inputType,
+ data,
+ });
+ } catch {}
+ if (!event) {
+ event = new Event(type, {
+ bubbles: true,
+ cancelable: type === "beforeinput",
+ });
+ }
+ return element.dispatchEvent(event);
+ }
+
+ function insertIntoTextControl(element, value) {
+ if (!isTextControl(element)) return false;
+ let start = 0;
+ let end = 0;
+ try {
+ start = Number(element.selectionStart);
+ end = Number(element.selectionEnd);
+ } catch {
+ return false;
+ }
+ if (!Number.isFinite(start) || !Number.isFinite(end)) return false;
+ if (!dispatchInputEvent(element, "beforeinput", "insertFromPaste", value)) {
+ return false;
+ }
+ element.setRangeText(value, start, end, "end");
+ dispatchInputEvent(element, "input", "insertFromPaste", value);
+ return true;
+ }
+
+ function insertIntoContentEditable(element, value) {
+ if (!element?.isContentEditable) return false;
+ if (!dispatchInputEvent(element, "beforeinput", "insertFromPaste", value)) {
+ return false;
+ }
+ const selection = globalThis.getSelection?.();
+ if (!selection || selection.rangeCount === 0) return false;
+ try {
+ if (document.queryCommandSupported?.("insertText") && document.execCommand("insertText", false, value)) {
+ return true;
+ }
+ } catch {}
+ const range = selection.getRangeAt(0);
+ range.deleteContents();
+ const node = document.createTextNode(value);
+ range.insertNode(node);
+ range.setStartAfter(node);
+ range.collapse(true);
+ selection.removeAllRanges();
+ selection.addRange(range);
+ dispatchInputEvent(element, "input", "insertFromPaste", value);
+ return true;
+ }
+
+ function insertText(element, value) {
+ return insertIntoTextControl(element, value) || insertIntoContentEditable(element, value);
+ }
+
+ function removeSelectedText(element) {
+ if (isTextControl(element)) {
+ let start = 0;
+ let end = 0;
+ try {
+ start = Number(element.selectionStart);
+ end = Number(element.selectionEnd);
+ } catch {
+ return false;
+ }
+ if (!Number.isFinite(start) || !Number.isFinite(end) || end <= start) return false;
+ if (!dispatchInputEvent(element, "beforeinput", "deleteByCut", null)) {
+ return false;
+ }
+ element.setRangeText("", start, end, "start");
+ dispatchInputEvent(element, "input", "deleteByCut", null);
+ return true;
+ }
+ if (!element?.isContentEditable) return false;
+ const selection = globalThis.getSelection?.();
+ if (!selection || selection.rangeCount === 0 || !String(selection.toString() || "")) {
+ return false;
+ }
+ if (!dispatchInputEvent(element, "beforeinput", "deleteByCut", null)) {
+ return false;
+ }
+ selection.deleteFromDocument();
+ dispatchInputEvent(element, "input", "deleteByCut", null);
+ return true;
+ }
+
+ const target = editableTarget(deepestActiveElement());
+ if (action === "paste") {
+ const event = dispatchClipboardEvent(target, "paste", text);
+ result.default_prevented = event.defaultPrevented;
+ result.handled = true;
+ result.text = text;
+ if (!event.defaultPrevented) {
+ result.changed = insertText(target, text);
+ }
+ return result;
+ }
+
+ if (action === "copy" || action === "cut") {
+ const selectionText = selectedText(target);
+ const event = dispatchClipboardEvent(target, action, selectionText);
+ result.default_prevented = event.defaultPrevented;
+ result.text = event.text || selectionText;
+ result.handled = Boolean(result.text || event.defaultPrevented);
+ if (action === "cut" && result.text && !event.defaultPrevented) {
+ result.changed = removeSelectedText(target);
+ }
+ return result;
+ }
+
+ result.error = `Unsupported clipboard action: ${action}`;
+ return result;
+}
+"""
_SPECIAL_SCHEME_RE = re.compile(r"^(?:about|blob|data|file|mailto|tel):", re.I)
_URL_SCHEME_RE = re.compile(r"^[a-z][a-z\d+\-.]*://", re.I)
@@ -956,6 +1193,61 @@ class _BrowserRuntimeCore:
) -> dict[str, Any]:
return await self._reference_action("typeSubmit", browser_id, reference_id, text)
+ async def clipboard(
+ self,
+ browser_id: int | str | None,
+ *,
+ action: str,
+ text: str = "",
+ ) -> dict[str, Any]:
+ await self.ensure_started()
+ resolved_id = self._resolve_browser_id(browser_id)
+ page = self._page(resolved_id)
+ normalized_action = str(action or "").strip().lower()
+ if normalized_action not in {"copy", "cut", "paste"}:
+ raise ValueError(f"Unsupported clipboard action: {normalized_action}")
+
+ clipboard_result: dict[str, Any]
+ try:
+ clipboard_result = await page.evaluate(
+ CLIPBOARD_BRIDGE_SCRIPT,
+ {
+ "action": normalized_action,
+ "text": str(text or ""),
+ },
+ ) or {}
+ except Exception as exc:
+ clipboard_result = {
+ "action": normalized_action,
+ "text": "",
+ "changed": False,
+ "default_prevented": False,
+ "handled": False,
+ "error": str(exc),
+ }
+
+ if (
+ normalized_action == "paste"
+ and text
+ and not clipboard_result.get("changed")
+ and not clipboard_result.get("default_prevented")
+ ):
+ if await self._insert_clipboard_text(page, str(text)):
+ clipboard_result["changed"] = True
+ clipboard_result["method"] = "keyboard.insert_text"
+ elif normalized_action in {"copy", "cut"} and not clipboard_result.get("text"):
+ with contextlib.suppress(Exception):
+ shortcut = "Control+C" if normalized_action == "copy" else "Control+X"
+ await page.keyboard.press(shortcut)
+ clipboard_result["keyboard_shortcut"] = True
+
+ await self._settle(page, short=True)
+ self._maybe_promote(resolved_id)
+ return {
+ "state": await self._state(resolved_id),
+ "clipboard": clipboard_result,
+ }
+
async def close_browser(self, browser_id: int | str | None = None) -> dict[str, Any]:
await self.ensure_started()
resolved_id = self._resolve_browser_id(browser_id)
@@ -1175,6 +1467,16 @@ class _BrowserRuntimeCore:
self._maybe_promote(resolved_id)
return await self._state(resolved_id)
+ async def _insert_clipboard_text(self, page: Any, text: str) -> bool:
+ if not text:
+ return False
+ insert_text = getattr(page.keyboard, "insert_text", None)
+ if callable(insert_text):
+ await insert_text(str(text))
+ else:
+ await page.keyboard.type(str(text))
+ return True
+
async def close(self, delete_profile: bool = False) -> None:
self._closing = True
for waiter in self._pending_popups:
diff --git a/plugins/_browser/webui/browser-store.js b/plugins/_browser/webui/browser-store.js
index c20630273..7bf4df547 100644
--- a/plugins/_browser/webui/browser-store.js
+++ b/plugins/_browser/webui/browser-store.js
@@ -2,6 +2,7 @@ import { createStore } from "/js/AlpineStore.js";
import { callJsonApi } from "/js/api.js";
import { getNamespacedClient } from "/js/websocket.js";
import { getContext, setContext } from "/index.js";
+import { copyToClipboard } from "/components/messages/action-buttons/simple-action-buttons.js";
import { store as chatInputStore } from "/components/chat/input/input-store.js";
import { store as pluginSettingsStore } from "/components/plugins/plugin-settings-store.js";
import { store as chatsStore } from "/components/sidebar/chats/chats-store.js";
@@ -24,6 +25,8 @@ const ANNOTATION_DRAG_THRESHOLD = 6;
const ANNOTATION_MAX_COMMENTS = 24;
const ANNOTATION_DOM_LIMIT = 1200;
const ANNOTATION_TRAY_MARGIN = 10;
+const BROWSER_VISUAL_SHORTCUT_KEYS = new Set(["a", "c", "insert", "v", "x", "y", "z"]);
+const LOCAL_EDITABLE_SELECTOR = "input, textarea, select, [contenteditable]";
function makeViewerToken() {
return globalThis.crypto?.randomUUID?.()
@@ -54,6 +57,21 @@ function normalizeBool(value, fallback = true) {
return fallback;
}
+function elementFromTarget(target) {
+ if (!target) return null;
+ if (target.nodeType === 1) return target;
+ return target.parentElement || null;
+}
+
+function isLocalEditableTarget(target) {
+ const element = elementFromTarget(target);
+ const editable = element?.closest?.(LOCAL_EDITABLE_SELECTOR);
+ if (!editable) return false;
+ if (editable.matches?.("input, textarea, select")) return true;
+ const value = String(editable.getAttribute?.("contenteditable") || "").trim().toLowerCase();
+ return ["", "true", "plaintext-only"].includes(value);
+}
+
function nextAnimationFrame() {
return new Promise((resolve) => {
const schedule = globalThis.requestAnimationFrame || ((callback) => globalThis.setTimeout(callback, 16));
@@ -169,6 +187,7 @@ const model = {
_closingBrowserIds: {},
_configLoadedAt: 0,
_configRefreshPromise: null,
+ _clipboardFallbackText: "",
async refreshStatus() {
this.status = await callJsonApi("/plugins/_browser/status", {});
@@ -1546,9 +1565,78 @@ const model = {
return;
}
+ if (this.handleVisualBrowserShortcut(event)) {
+ return;
+ }
+
void this.sendKey(event);
},
+ handleVisualBrowserShortcut(event) {
+ const shortcut = this.visualBrowserShortcut(event);
+ if (!shortcut) return false;
+ event.preventDefault();
+ event.stopPropagation?.();
+
+ if (shortcut.action === "paste") {
+ void this.pasteHostClipboardToBrowser();
+ return true;
+ }
+ if (shortcut.action === "copy" || shortcut.action === "cut") {
+ void this.copyBrowserClipboardToHost(shortcut.action);
+ return true;
+ }
+ if (shortcut.key) {
+ void this.sendShortcut(shortcut.key);
+ return true;
+ }
+ return false;
+ },
+
+ visualBrowserShortcut(event) {
+ if (!this.shouldHandleVisualBrowserShortcut(event)) return null;
+ const key = String(event?.key || "").toLowerCase();
+ const primary = Boolean(event?.ctrlKey || event?.metaKey);
+ const shift = Boolean(event?.shiftKey);
+
+ if (!primary && shift && key === "insert") {
+ return { action: "paste" };
+ }
+ if (!primary || event?.altKey) return null;
+
+ if (key === "v") return { action: "paste" };
+ if (!shift && (key === "c" || key === "insert")) return { action: "copy" };
+ if (!shift && key === "x") return { action: "cut" };
+ if (!shift && key === "a") return { key: "Control+A" };
+ if (key === "z") return { key: shift ? "Control+Shift+Z" : "Control+Z" };
+ if (!shift && key === "y") return { key: "Control+Y" };
+ return null;
+ },
+
+ shouldHandleVisualBrowserShortcut(event) {
+ if (!this._surfaceMounted || !this.activeBrowserId || this.annotating) return false;
+ if (isLocalEditableTarget(event?.target)) return false;
+ const key = String(event?.key || "").toLowerCase();
+ if (!BROWSER_VISUAL_SHORTCUT_KEYS.has(key)) return false;
+ return Boolean(this.visualBrowserStageForEvent(event));
+ },
+
+ visualBrowserStageForEvent(event) {
+ const element = elementFromTarget(event?.target);
+ const blockingUi = element?.closest?.(
+ ".browser-toolbar, .browser-meta, .browser-extension-dropdown, .browser-annotation-popover, .browser-annotation-tray, button, a",
+ );
+ if (blockingUi) return null;
+
+ const stage = element?.closest?.(".browser-stage");
+ if (stage?.closest?.(".browser-panel")) return stage;
+
+ const activeElement = globalThis.document?.activeElement;
+ const activeStage = activeElement?.closest?.(".browser-stage");
+ if (activeStage?.closest?.(".browser-panel")) return activeStage;
+ return null;
+ },
+
handleStageWheel(event) {
if (this.annotating) return;
void this.sendWheel(event);
@@ -2184,6 +2272,83 @@ const model = {
});
},
+ async sendShortcut(key) {
+ const contextId = this.normalizeContextId(this.activeBrowserContextId || this.contextId);
+ if (!contextId || !this.activeBrowserId || !key) return;
+ await websocket.emit("browser_viewer_input", {
+ context_id: contextId,
+ browser_id: this.activeBrowserId,
+ viewer_id: this._viewerToken,
+ input_type: "keyboard",
+ key,
+ text: "",
+ });
+ },
+
+ async pasteHostClipboardToBrowser() {
+ try {
+ const text = await this.readHostClipboardText();
+ if (!text) return;
+ await this.sendClipboard("paste", text);
+ } catch (error) {
+ this.error = "Browser paste needs clipboard permission in this tab.";
+ globalThis.justToast?.(this.error, "warning", 2200, "browser-clipboard");
+ console.warn("Browser clipboard paste failed", error);
+ }
+ },
+
+ async copyBrowserClipboardToHost(action = "copy") {
+ try {
+ const clipboard = await this.sendClipboard(action);
+ const text = String(clipboard?.text || clipboard?.clipboard_text || "");
+ if (!text) return;
+ await copyToClipboard(text);
+ this._clipboardFallbackText = text;
+ const message = action === "cut" ? "Cut from Browser" : "Copied from Browser";
+ globalThis.justToast?.(message, "success", 1200, "browser-clipboard");
+ } catch (error) {
+ this.error = action === "cut"
+ ? "Browser cut failed."
+ : "Browser copy failed.";
+ globalThis.justToast?.(this.error, "warning", 1800, "browser-clipboard");
+ console.warn("Browser clipboard copy failed", error);
+ }
+ },
+
+ async readHostClipboardText() {
+ const clipboard = globalThis.navigator?.clipboard;
+ if (clipboard?.readText && globalThis.isSecureContext) {
+ try {
+ return await clipboard.readText();
+ } catch (error) {
+ if (this._clipboardFallbackText) return this._clipboardFallbackText;
+ throw error;
+ }
+ }
+ return this._clipboardFallbackText || "";
+ },
+
+ async sendClipboard(action = "copy", text = "") {
+ const contextId = this.normalizeContextId(this.activeBrowserContextId || this.contextId);
+ if (!contextId || !this.activeBrowserId) return {};
+ const response = await websocket.request(
+ "browser_viewer_input",
+ {
+ context_id: contextId,
+ browser_id: this.activeBrowserId,
+ viewer_id: this._viewerToken,
+ input_type: "clipboard",
+ action,
+ text,
+ },
+ { timeoutMs: 10000 },
+ );
+ const data = firstOk(response);
+ this.applyActiveFrameState(data.state);
+ this.applySnapshot(data.snapshot);
+ return data.clipboard || {};
+ },
+
async cleanup() {
if (this._surfaceHandoff) {
this.releaseSurfaceBindings();
diff --git a/tests/test_browser_agent_regressions.py b/tests/test_browser_agent_regressions.py
index ec6cbcb80..71a2cbae9 100644
--- a/tests/test_browser_agent_regressions.py
+++ b/tests/test_browser_agent_regressions.py
@@ -869,6 +869,41 @@ def test_browser_annotate_mode_ui_and_prompt_hooks():
assert "value=\\\"[redacted]\\\"" in browser_store
+def test_browser_visual_mode_bridges_clipboard_shortcuts():
+ browser_store = (
+ PROJECT_ROOT / "plugins" / "_browser" / "webui" / "browser-store.js"
+ ).read_text(encoding="utf-8")
+ runtime = (
+ PROJECT_ROOT / "plugins" / "_browser" / "helpers" / "runtime.py"
+ ).read_text(encoding="utf-8")
+ ws_browser = (
+ PROJECT_ROOT / "plugins" / "_browser" / "api" / "ws_browser.py"
+ ).read_text(encoding="utf-8")
+
+ assert 'import { copyToClipboard } from "/components/messages/action-buttons/simple-action-buttons.js";' in browser_store
+ assert "BROWSER_VISUAL_SHORTCUT_KEYS" in browser_store
+ assert "handleVisualBrowserShortcut(event)" in browser_store
+ assert "visualBrowserStageForEvent(event)" in browser_store
+ assert "isLocalEditableTarget(event?.target)" in browser_store
+ assert 'return { action: "paste" };' in browser_store
+ assert 'return { action: "copy" };' in browser_store
+ assert 'return { action: "cut" };' in browser_store
+ assert 'return { key: "Control+A" };' in browser_store
+ assert 'return { key: shift ? "Control+Shift+Z" : "Control+Z" };' in browser_store
+ assert 'input_type: "clipboard"' in browser_store
+ assert "pasteHostClipboardToBrowser()" in browser_store
+ assert "copyBrowserClipboardToHost" in browser_store
+ assert "Browser paste needs clipboard permission in this tab." in browser_store
+
+ assert "CLIPBOARD_BRIDGE_SCRIPT" in runtime
+ assert "async def clipboard" in runtime
+ assert "insertFromPaste" in runtime
+ assert "deleteByCut" in runtime
+ assert "keyboard.insert_text" in runtime
+ assert 'input_type == "clipboard"' in ws_browser
+ assert 'runtime.call(\n "clipboard"' in ws_browser
+
+
def test_browser_runtime_and_content_helper_expose_annotation_target():
runtime = (
PROJECT_ROOT / "plugins" / "_browser" / "helpers" / "runtime.py"
@@ -1458,6 +1493,100 @@ async def test_browser_runtime_remounts_initial_changed_viewport():
assert settled == [True]
+async def test_browser_runtime_clipboard_paste_uses_dom_bridge():
+ eval_payloads = []
+ settled = []
+
+ class FakeKeyboard:
+ def __init__(self):
+ self.inserted = []
+
+ async def insert_text(self, text):
+ self.inserted.append(text)
+
+ class FakePage:
+ url = "about:blank"
+
+ def __init__(self):
+ self.keyboard = FakeKeyboard()
+
+ async def evaluate(self, script, payload=None):
+ if payload is not None:
+ eval_payloads.append((script, payload))
+ return {
+ "action": "paste",
+ "text": payload["text"],
+ "changed": True,
+ "default_prevented": False,
+ }
+ return 1
+
+ async def title(self):
+ return "Blank"
+
+ page = FakePage()
+ core = _BrowserRuntimeCore("ctx")
+ core.context = object()
+ core.pages[7] = browser_runtime_module.BrowserPage(id=7, page=page)
+
+ async def fake_settle(_page, short=False):
+ settled.append(short)
+
+ core._settle = fake_settle
+
+ result = await core.clipboard(7, action="paste", text="hello")
+
+ assert result["state"]["id"] == 7
+ assert result["clipboard"]["changed"] is True
+ assert result["clipboard"]["text"] == "hello"
+ assert eval_payloads[0][1] == {"action": "paste", "text": "hello"}
+ assert "insertFromPaste" in eval_payloads[0][0]
+ assert page.keyboard.inserted == []
+ assert settled == [True]
+
+
+async def test_browser_runtime_clipboard_paste_falls_back_to_keyboard_insert_text():
+ class FakeKeyboard:
+ def __init__(self):
+ self.inserted = []
+
+ async def insert_text(self, text):
+ self.inserted.append(text)
+
+ class FakePage:
+ url = "about:blank"
+
+ def __init__(self):
+ self.keyboard = FakeKeyboard()
+
+ async def evaluate(self, script, payload=None):
+ if payload is not None:
+ return {
+ "action": "paste",
+ "text": payload["text"],
+ "changed": False,
+ "default_prevented": False,
+ }
+ return 1
+
+ async def title(self):
+ return "Blank"
+
+ page = FakePage()
+ core = _BrowserRuntimeCore("ctx")
+ core.context = object()
+ core.pages[7] = browser_runtime_module.BrowserPage(id=7, page=page)
+ core._settle = lambda _page, short=False: asyncio.sleep(0)
+
+ result = await core.clipboard(7, action="paste", text="hello")
+
+ assert result["clipboard"]["changed"] is True
+ assert result["clipboard"]["method"] == "keyboard.insert_text"
+ assert page.keyboard.inserted == ["hello"]
+
+
@pytest.mark.anyio
async def test_browser_viewer_wheel_input_dispatches_scroll(monkeypatch):
calls = []
@@ -1501,6 +1630,54 @@ async def test_browser_viewer_wheel_input_dispatches_scroll(monkeypatch):
assert calls == [("wheel", (3, 320.0, 480.0, 0.0, 640.0), {})]
+async def test_browser_viewer_clipboard_input_dispatches_runtime(monkeypatch):
+ calls = []
+ clipboard = {"action": "paste", "text": "hello", "changed": True}
+
+ class FakeRuntime:
+ async def call(self, method, *args, **kwargs):
+ calls.append((method, args, kwargs))
+ return {
+ "state": {"id": args[0], "currentUrl": "about:blank"},
+ "clipboard": clipboard,
+ }
+
+ async def fake_get_runtime(context_id, create=True):
+ assert context_id == "ctx"
+ assert create is False
+ return FakeRuntime()
+
+ monkeypatch.setattr(ws_browser_module, "get_runtime", fake_get_runtime)
+
+ handler = ws_browser_module.WsBrowser(
+ SimpleNamespace(),
+ threading.RLock(),
+ manager=None,
+ )
+
+ result = await handler.process(
+ "browser_viewer_input",
+ {
+ "context_id": "ctx",
+ "browser_id": 3,
+ "input_type": "clipboard",
+ "action": "paste",
+ "text": "hello",
+ },
+ "sid-1",
+ )
+
+ assert result == {
+ "state": {"id": 3, "currentUrl": "about:blank"},
+ "clipboard": clipboard,
+ "snapshot": None,
+ }
+ assert calls == [
+ ("clipboard", (3,), {"action": "paste", "text": "hello"})
+ ]
+
+
@pytest.mark.anyio
async def test_browser_viewer_annotation_dispatches_runtime(monkeypatch):
calls = []