diff --git a/src/main/browser/session.ts b/src/main/browser/session.ts index db964ea..55b50e2 100644 --- a/src/main/browser/session.ts +++ b/src/main/browser/session.ts @@ -1165,16 +1165,32 @@ export class BrowserSession implements BrowserLike { } const inject = () => this.evaluate(`(() => { try { delete window.__rpaPickResult; ${PICKER_BOOTSTRAP_SOURCE} } catch {} })()`); + const injectionIds: string[] = []; + const registerInjection = async () => { + const registered = await this.cdp<{ identifier?: string }>("Page.addScriptToEvaluateOnNewDocument", { + source: PICKER_BOOTSTRAP_SOURCE + }).catch(() => null); + if (registered?.identifier) injectionIds.push(registered.identifier); + }; + // 跟随前台标签页:搜索/点按钮常开新标签页,附着目标切过去并重新注入,拾取不中断 + const followForegroundTab = async () => { + if (this.win || !this.httpBase) return; + const pages = await listPageTargets(this.httpBase).catch(() => []); + const page = pages[0]; + if (!page || page.id === this.activeTargetId) return; + await this.activateTab(page.id); + await this.cdp("Page.enable").catch(() => {}); + await registerInjection(); + await inject(); + }; await this.cdp("Page.enable").catch(() => {}); - const registered = await this.cdp<{ identifier?: string }>("Page.addScriptToEvaluateOnNewDocument", { - source: PICKER_BOOTSTRAP_SOURCE - }).catch(() => null); - const injectionId = registered?.identifier; + await registerInjection(); try { await inject(); const deadline = Date.now() + Math.max(5000, timeoutMs); while (Date.now() < deadline) { await new Promise((resolve) => setTimeout(resolve, 400)); + await followForegroundTab().catch(() => {}); const state = await this.evaluate<{ result: { ok: boolean; value?: ElementPickResult; reason?: string } | null; active: boolean }>( "({ result: window.__rpaPickResult ?? null, active: window.__rpaPickerActive?.active === true })" ).catch(() => null); @@ -1187,11 +1203,14 @@ export class BrowserSession implements BrowserLike { } throw new RuntimeError("timeout", "拾取超时,请重新点击拾取"); } finally { - await this.evaluate( - "(() => { try { window.__rpaElementPicker?.cancelHard?.(); window.__rpaPickerActive = undefined; delete window.__rpaPickResult; } catch {} })()" - ).catch(() => {}); - if (injectionId) { - await this.cdp("Page.removeScriptToEvaluateOnNewDocument", { identifier: injectionId }).catch(() => {}); + const cleanupExpr = + "(() => { try { window.__rpaElementPicker?.cancelHard?.(); window.__rpaPickerActive = undefined; delete window.__rpaPickResult; } catch {} })()"; + await this.evaluate(cleanupExpr).catch(() => {}); + for (const tabTransport of this.tabTransports.values()) { + await tabTransport.send("Runtime.evaluate", { expression: cleanupExpr }).catch(() => {}); + } + for (const identifier of injectionIds) { + await this.cdp("Page.removeScriptToEvaluateOnNewDocument", { identifier }).catch(() => {}); } } }