diff --git a/src/main/browser/session.ts b/src/main/browser/session.ts index d32d4a6..8d446f2 100644 --- a/src/main/browser/session.ts +++ b/src/main/browser/session.ts @@ -192,7 +192,22 @@ export class BrowserSession implements BrowserLike { return this.transport.send(method, params); } + private lastFollowCheck = 0; + + /** 运行时跟随前台标签页:点按钮/搜索常开新标签页,元素操作应落在用户眼前看到的页面(节流 400ms) */ + private async followForegroundTab(): Promise { + if (this.win || !this.httpBase) return; + const now = Date.now(); + if (now - this.lastFollowCheck < 400) return; + this.lastFollowCheck = now; + const pages = await listPageTargets(this.httpBase).catch(() => []); + const page = pages[0]; + if (!page || page.id === this.activeTargetId) return; + await this.activateTab(page.id); + } + private async evaluate(expression: string): Promise { + await this.followForegroundTab().catch(() => {}); const result = await this.cdp<{ result?: { value?: T }; exceptionDetails?: { exception?: { description?: string }; text?: string } }>( "Runtime.evaluate", { expression, returnByValue: true, awaitPromise: true }