From 26b4cad2d0d12ca0fd6f0cca331351d020d3ed88 Mon Sep 17 00:00:00 2001 From: Wooseong Kim Date: Fri, 24 Jul 2026 20:28:16 +0900 Subject: [PATCH 1/3] fix(browser): emulate CDP page focus for rich-editor insertText Electron 43+ can leave embedded guest views with document.hasFocus() false even when Orca is frontmost, so Draft.js-style editors drop Input.insertText and execCommand. Enable Emulation.setFocusEmulationEnabled on debugger attach in CdpWsProxy and CdpBridge. --- src/main/browser/cdp-bridge.ts | 9 +++++++++ src/main/browser/cdp-focus-emulation.test.ts | 12 ++++++++++++ src/main/browser/cdp-focus-emulation.ts | 17 +++++++++++++++++ .../browser/cdp-ws-proxy-focus-replay.test.ts | 9 +++++++++ src/main/browser/cdp-ws-proxy.test.ts | 15 +++++++++++++-- src/main/browser/cdp-ws-proxy.ts | 12 ++++++++++++ 6 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/main/browser/cdp-focus-emulation.test.ts create mode 100644 src/main/browser/cdp-focus-emulation.ts diff --git a/src/main/browser/cdp-bridge.ts b/src/main/browser/cdp-bridge.ts index 47d263ac3712..4f3faa389a23 100644 --- a/src/main/browser/cdp-bridge.ts +++ b/src/main/browser/cdp-bridge.ts @@ -48,6 +48,7 @@ import { import { insertTextThroughCdp } from './browser-text-insertion' import type { BrowserManager } from './browser-manager' import { ANTI_DETECTION_SCRIPT } from './anti-detection' +import { enableCdpFocusEmulation } from './cdp-focus-emulation' const CAPTURE_LOG_LIMIT = 1000 @@ -1162,6 +1163,14 @@ export class CdpBridge { source: ANTI_DETECTION_SCRIPT }) + // Why: keep document.hasFocus() true for guest views that are not the OS + // first responder so rich-editor insertText/fill paths commit (#10375). + try { + await enableCdpFocusEmulation(sender) + } catch { + /* best-effort */ + } + // Why: only remove this bridge's listeners; screencast/proxy sessions share the debugger and own their teardown. this.removeDebuggerListeners(guest, state) diff --git a/src/main/browser/cdp-focus-emulation.test.ts b/src/main/browser/cdp-focus-emulation.test.ts new file mode 100644 index 000000000000..356eda656418 --- /dev/null +++ b/src/main/browser/cdp-focus-emulation.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, it, vi } from 'vitest' +import { CDP_FOCUS_EMULATION_METHOD, enableCdpFocusEmulation } from './cdp-focus-emulation' + +describe('enableCdpFocusEmulation', () => { + it('enables Emulation.setFocusEmulationEnabled over CDP', async () => { + const send = vi.fn(async () => ({})) + + await enableCdpFocusEmulation(send) + + expect(send).toHaveBeenCalledWith(CDP_FOCUS_EMULATION_METHOD, { enabled: true }) + }) +}) diff --git a/src/main/browser/cdp-focus-emulation.ts b/src/main/browser/cdp-focus-emulation.ts new file mode 100644 index 000000000000..91a0ef03b553 --- /dev/null +++ b/src/main/browser/cdp-focus-emulation.ts @@ -0,0 +1,17 @@ +/** + * CDP focus emulation so `document.hasFocus()` reports true even when the + * guest WebContents is not the OS first responder (Electron 43+ / multi-view). + * + * Required for rich editors (Draft.js, etc.) that gate Input.insertText and + * execCommand on frame focus — see #10375 / regression of #7035. + */ +export const CDP_FOCUS_EMULATION_METHOD = 'Emulation.setFocusEmulationEnabled' as const + +export type CdpFocusEmulationSender = ( + method: string, + params?: Record +) => Promise + +export async function enableCdpFocusEmulation(send: CdpFocusEmulationSender): Promise { + await send(CDP_FOCUS_EMULATION_METHOD, { enabled: true }) +} diff --git a/src/main/browser/cdp-ws-proxy-focus-replay.test.ts b/src/main/browser/cdp-ws-proxy-focus-replay.test.ts index 71b00cd9ff67..56040198b714 100644 --- a/src/main/browser/cdp-ws-proxy-focus-replay.test.ts +++ b/src/main/browser/cdp-ws-proxy-focus-replay.test.ts @@ -53,6 +53,7 @@ describe('CdpWsProxy DOM.focus replay', () => { expect(getSendCommandCalls(mock)).toEqual([ ['Page.enable', {}], ['Page.addScriptToEvaluateOnNewDocument', expect.any(Object)], + ['Emulation.setFocusEmulationEnabled', { enabled: true }], ['DOM.focus', { backendNodeId: 99 }], ['DOM.focus', { backendNodeId: 99 }], ['Input.insertText', { text: 'hello' }] @@ -81,6 +82,7 @@ describe('CdpWsProxy DOM.focus replay', () => { expect(getSendCommandCalls(mock)).toEqual([ ['Page.enable', {}], ['Page.addScriptToEvaluateOnNewDocument', expect.any(Object)], + ['Emulation.setFocusEmulationEnabled', { enabled: true }], ['DOM.focus', { backendNodeId: 123 }, 'oopif-session-123'], ['DOM.focus', { backendNodeId: 123 }, 'oopif-session-123'], ['Input.insertText', { text: 'frame text' }, 'oopif-session-123'] @@ -113,6 +115,7 @@ describe('CdpWsProxy DOM.focus replay', () => { expect(getSendCommandCalls(mock)).toEqual([ ['Page.enable', {}], ['Page.addScriptToEvaluateOnNewDocument', expect.any(Object)], + ['Emulation.setFocusEmulationEnabled', { enabled: true }], ['DOM.focus', { backendNodeId: 44 }], ['Runtime.callFunctionOn', { functionDeclaration: '() => document.activeElement?.id' }], ['Input.insertText', { text: 'after eval' }] @@ -156,6 +159,7 @@ describe('CdpWsProxy DOM.focus replay', () => { expect(getSendCommandCalls(mock)).toEqual([ ['Page.enable', {}], ['Page.addScriptToEvaluateOnNewDocument', expect.any(Object)], + ['Emulation.setFocusEmulationEnabled', { enabled: true }], ['DOM.focus', { backendNodeId: 55 }], ['Input.insertText', { text: 'fallback' }] ]) @@ -198,6 +202,7 @@ describe('CdpWsProxy DOM.focus replay', () => { expect(getSendCommandCalls(mock)).toEqual([ ['Page.enable', {}], ['Page.addScriptToEvaluateOnNewDocument', expect.any(Object)], + ['Emulation.setFocusEmulationEnabled', { enabled: true }], ['DOM.focus', { backendNodeId: 77 }], ['DOM.focus', { backendNodeId: 77 }] ]) @@ -243,6 +248,7 @@ describe('CdpWsProxy DOM.focus replay', () => { expect(getSendCommandMethods(mock)).toEqual([ 'Page.enable', 'Page.addScriptToEvaluateOnNewDocument', + 'Emulation.setFocusEmulationEnabled', 'DOM.focus', 'DOM.focus', 'Input.insertText' @@ -273,6 +279,7 @@ describe('CdpWsProxy DOM.focus replay', () => { expect(getSendCommandMethods(mock)).toEqual([ 'Page.enable', 'Page.addScriptToEvaluateOnNewDocument', + 'Emulation.setFocusEmulationEnabled', 'DOM.focus', 'Input.insertText' ]) @@ -299,6 +306,7 @@ describe('CdpWsProxy DOM.focus replay', () => { expect(getSendCommandMethods(mock)).toEqual([ 'Page.enable', 'Page.addScriptToEvaluateOnNewDocument', + 'Emulation.setFocusEmulationEnabled', 'DOM.focus', 'Page.captureScreenshot', 'Input.insertText' @@ -325,6 +333,7 @@ describe('CdpWsProxy DOM.focus replay', () => { expect(getSendCommandMethods(mock)).toEqual([ 'Page.enable', 'Page.addScriptToEvaluateOnNewDocument', + 'Emulation.setFocusEmulationEnabled', 'DOM.focus', 'Input.insertText' ]) diff --git a/src/main/browser/cdp-ws-proxy.test.ts b/src/main/browser/cdp-ws-proxy.test.ts index 5c366eda7278..68024b77d5a1 100644 --- a/src/main/browser/cdp-ws-proxy.test.ts +++ b/src/main/browser/cdp-ws-proxy.test.ts @@ -56,6 +56,13 @@ describe('CdpWsProxy', () => { expect(mock.webContents.debugger.attach).toHaveBeenCalledWith('1.3') }) + it('enables CDP focus emulation on attach so document.hasFocus() can be true (#10375)', () => { + expect(mock.webContents.debugger.sendCommand).toHaveBeenCalledWith( + 'Emulation.setFocusEmulationEnabled', + { enabled: true } + ) + }) + // ── CDP message ID correlation ── it('correlates CDP request/response IDs', async () => { @@ -403,6 +410,7 @@ describe('CdpWsProxy', () => { expect(getSendCommandMethods(mock)).toEqual([ 'Page.enable', 'Page.addScriptToEvaluateOnNewDocument', + 'Emulation.setFocusEmulationEnabled', 'Input.insertText' ]) client.close() @@ -422,6 +430,7 @@ describe('CdpWsProxy', () => { expect(getSendCommandMethods(mock)).toEqual([ 'Page.enable', 'Page.addScriptToEvaluateOnNewDocument', + 'Emulation.setFocusEmulationEnabled', 'Network.enable', 'Page.enable', 'Page.setLifecycleEventsEnabled', @@ -443,6 +452,7 @@ describe('CdpWsProxy', () => { expect(getSendCommandMethods(mock)).toEqual([ 'Page.enable', 'Page.addScriptToEvaluateOnNewDocument', + 'Emulation.setFocusEmulationEnabled', 'Network.enable', 'Page.enable', 'Page.setLifecycleEventsEnabled' @@ -462,7 +472,7 @@ describe('CdpWsProxy', () => { sessionId: 'iframe-session-123' }) - expect(getSendCommandCalls(mock).slice(2)).toEqual([ + expect(getSendCommandCalls(mock).slice(3)).toEqual([ ['Network.enable', {}, 'iframe-session-123'], ['Page.enable', {}, 'iframe-session-123'], ['Page.setLifecycleEventsEnabled', { enabled: true }, 'iframe-session-123'], @@ -481,7 +491,7 @@ describe('CdpWsProxy', () => { sessionId: 'iframe-session-123' }) - expect(getSendCommandCalls(mock).slice(2)).toEqual([ + expect(getSendCommandCalls(mock).slice(3)).toEqual([ ['Network.enable', {}, 'iframe-session-123'], ['Page.enable', {}, 'iframe-session-123'], ['Page.setLifecycleEventsEnabled', { enabled: true }, 'iframe-session-123'], @@ -565,6 +575,7 @@ describe('CdpWsProxy', () => { expect(getSendCommandMethods(mock)).toEqual([ 'Page.enable', 'Page.addScriptToEvaluateOnNewDocument', + 'Emulation.setFocusEmulationEnabled', 'Runtime.evaluate' ]) client.close() diff --git a/src/main/browser/cdp-ws-proxy.ts b/src/main/browser/cdp-ws-proxy.ts index 99c1d5546545..a0c4059870dd 100644 --- a/src/main/browser/cdp-ws-proxy.ts +++ b/src/main/browser/cdp-ws-proxy.ts @@ -6,6 +6,7 @@ import { captureScreenshot } from './cdp-screenshot' import { buildPrintToPdfOptions, CdpPdfStreamStore } from './cdp-print-to-pdf' import { ANTI_DETECTION_SCRIPT } from './anti-detection' import { acquireElectronDebugger, type ElectronDebuggerLease } from './electron-debugger-lease' +import { enableCdpFocusEmulation } from './cdp-focus-emulation' const LIFECYCLE_PRIMING_TIMEOUT_MS = 1_000 @@ -233,6 +234,17 @@ export class CdpWsProxy { /* best-effort — page domain may not be ready yet */ } + // Why: Electron 43+ tracks WebContentsView focus correctly, so embedded + // tabs can report document.hasFocus()===false even when Orca is frontmost. + // Rich editors (Draft.js) then drop Input.insertText / execCommand (#10375). + try { + await enableCdpFocusEmulation((method, params) => + this.webContents.debugger.sendCommand(method, params ?? {}) + ) + } catch { + /* best-effort — older CDP stacks may lack the method */ + } + this.debuggerMessageHandler = (_event: unknown, ...rest: unknown[]) => { const [method, params, sessionId] = rest as [ string, From 4c7aade3a4589c2d0c36a324beb45595baaf795e Mon Sep 17 00:00:00 2001 From: Wooseong Kim Date: Sat, 25 Jul 2026 20:03:45 +0900 Subject: [PATCH 2/3] fix(browser): timeout CDP focus-emulation commands during attach CodeRabbit: stalled debugger.sendCommand could hang agent-browser attach. --- src/main/browser/cdp-ws-proxy.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/browser/cdp-ws-proxy.ts b/src/main/browser/cdp-ws-proxy.ts index a0c4059870dd..e60ecef968ea 100644 --- a/src/main/browser/cdp-ws-proxy.ts +++ b/src/main/browser/cdp-ws-proxy.ts @@ -238,9 +238,26 @@ export class CdpWsProxy { // tabs can report document.hasFocus()===false even when Orca is frontmost. // Rich editors (Draft.js) then drop Input.insertText / execCommand (#10375). try { - await enableCdpFocusEmulation((method, params) => - this.webContents.debugger.sendCommand(method, params ?? {}) - ) + // Why: bound stalled debugger commands so attach does not hang agent-browser (30s CDP default). + const FOCUS_EMULATION_COMMAND_TIMEOUT_MS = 5_000 + await enableCdpFocusEmulation(async (method, params) => { + let timer: ReturnType | null = null + try { + return await Promise.race([ + this.webContents.debugger.sendCommand(method, params ?? {}), + new Promise((_, reject) => { + timer = setTimeout( + () => reject(new Error(`CDP focus emulation timed out: ${method}`)), + FOCUS_EMULATION_COMMAND_TIMEOUT_MS + ) + }) + ]) + } finally { + if (timer) { + clearTimeout(timer) + } + } + }) } catch { /* best-effort — older CDP stacks may lack the method */ } From eaf21cbb1fc76953f9c8032fe46583aee6aa29ee Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 27 Jul 2026 20:00:20 -0700 Subject: [PATCH 3/3] fix(browser): bound focus emulation on both CDP paths --- .../browser/cdp-bridge-integration.test.ts | 7 ++++++ src/main/browser/cdp-focus-emulation.test.ts | 16 +++++++++++++ src/main/browser/cdp-focus-emulation.ts | 18 ++++++++++++++- src/main/browser/cdp-ws-proxy.ts | 23 +++---------------- 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/main/browser/cdp-bridge-integration.test.ts b/src/main/browser/cdp-bridge-integration.test.ts index daaae60540e3..b5e6710601dc 100644 --- a/src/main/browser/cdp-bridge-integration.test.ts +++ b/src/main/browser/cdp-bridge-integration.test.ts @@ -352,6 +352,13 @@ describe('Browser automation pipeline (integration)', () => { name: 'More information...' }) expect(activeGuest.debugger.attach).toHaveBeenCalledTimes(1) + expect( + activeGuestHarness.sendCommandMock.mock.calls.some( + ([method, params]) => + method === 'Emulation.setFocusEmulationEnabled' && + JSON.stringify(params) === JSON.stringify({ enabled: true }) + ) + ).toBe(true) }) it('preserves debugger listeners owned by other browser streams', async () => { diff --git a/src/main/browser/cdp-focus-emulation.test.ts b/src/main/browser/cdp-focus-emulation.test.ts index 356eda656418..46b4e573e4c0 100644 --- a/src/main/browser/cdp-focus-emulation.test.ts +++ b/src/main/browser/cdp-focus-emulation.test.ts @@ -9,4 +9,20 @@ describe('enableCdpFocusEmulation', () => { expect(send).toHaveBeenCalledWith(CDP_FOCUS_EMULATION_METHOD, { enabled: true }) }) + + it('bounds a stalled debugger command', async () => { + vi.useFakeTimers() + try { + const send = vi.fn(() => new Promise(() => {})) + const result = enableCdpFocusEmulation(send) + const expectation = expect(result).rejects.toThrow('CDP focus emulation timed out') + + await vi.advanceTimersByTimeAsync(5_000) + + await expectation + expect(vi.getTimerCount()).toBe(0) + } finally { + vi.useRealTimers() + } + }) }) diff --git a/src/main/browser/cdp-focus-emulation.ts b/src/main/browser/cdp-focus-emulation.ts index 91a0ef03b553..e25dceca4b49 100644 --- a/src/main/browser/cdp-focus-emulation.ts +++ b/src/main/browser/cdp-focus-emulation.ts @@ -6,6 +6,7 @@ * execCommand on frame focus — see #10375 / regression of #7035. */ export const CDP_FOCUS_EMULATION_METHOD = 'Emulation.setFocusEmulationEnabled' as const +const CDP_FOCUS_EMULATION_TIMEOUT_MS = 5_000 export type CdpFocusEmulationSender = ( method: string, @@ -13,5 +14,20 @@ export type CdpFocusEmulationSender = ( ) => Promise export async function enableCdpFocusEmulation(send: CdpFocusEmulationSender): Promise { - await send(CDP_FOCUS_EMULATION_METHOD, { enabled: true }) + let timer: ReturnType | null = null + try { + await Promise.race([ + send(CDP_FOCUS_EMULATION_METHOD, { enabled: true }), + new Promise((_, reject) => { + timer = setTimeout( + () => reject(new Error(`CDP focus emulation timed out: ${CDP_FOCUS_EMULATION_METHOD}`)), + CDP_FOCUS_EMULATION_TIMEOUT_MS + ) + }) + ]) + } finally { + if (timer) { + clearTimeout(timer) + } + } } diff --git a/src/main/browser/cdp-ws-proxy.ts b/src/main/browser/cdp-ws-proxy.ts index e60ecef968ea..a0c4059870dd 100644 --- a/src/main/browser/cdp-ws-proxy.ts +++ b/src/main/browser/cdp-ws-proxy.ts @@ -238,26 +238,9 @@ export class CdpWsProxy { // tabs can report document.hasFocus()===false even when Orca is frontmost. // Rich editors (Draft.js) then drop Input.insertText / execCommand (#10375). try { - // Why: bound stalled debugger commands so attach does not hang agent-browser (30s CDP default). - const FOCUS_EMULATION_COMMAND_TIMEOUT_MS = 5_000 - await enableCdpFocusEmulation(async (method, params) => { - let timer: ReturnType | null = null - try { - return await Promise.race([ - this.webContents.debugger.sendCommand(method, params ?? {}), - new Promise((_, reject) => { - timer = setTimeout( - () => reject(new Error(`CDP focus emulation timed out: ${method}`)), - FOCUS_EMULATION_COMMAND_TIMEOUT_MS - ) - }) - ]) - } finally { - if (timer) { - clearTimeout(timer) - } - } - }) + await enableCdpFocusEmulation((method, params) => + this.webContents.debugger.sendCommand(method, params ?? {}) + ) } catch { /* best-effort — older CDP stacks may lack the method */ }