From 96b084c27397a919aab86f04296b70605ab5ad5e Mon Sep 17 00:00:00 2001 From: wangpi26 Date: Thu, 9 Jul 2026 11:27:37 +0800 Subject: [PATCH] fix: create render pages in background to avoid focus stealing In headful mode (e.g. --control-url attaching to a logged-in browser), every new page created via stealth.Page() was opened in the foreground, which activated the Chrome window and stole focus from the user's active app on each page render. Replace stealth.Page(b) with an explicit b.Page() using TargetCreateTarget{Background: true}, then inject the same stealth.JS anti-detection script via EvalOnNewDocument. Behaviour is identical except pages now open in the background, so focus is no longer disrupted during a crawl. --- browser/pool.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/browser/pool.go b/browser/pool.go index fc43566..cbff549 100644 --- a/browser/pool.go +++ b/browser/pool.go @@ -97,11 +97,16 @@ func (p *Pool) Render(ctx context.Context, rawURL string) (RenderResult, error) return RenderResult{}, err } - page, err := stealth.Page(b) + // 在后台创建页面,避免 headful 模式下每次新建 tab 都把 Chrome 窗口拉到前台抢夺焦点。 + // 与 stealth.Page 的唯一区别是设置了 Background:true;反检测脚本仍由 stealth.JS 注入。 + page, err := b.Page(proto.TargetCreateTarget{Background: true}) if err != nil { return RenderResult{}, fmt.Errorf("new page: %w", err) } defer func() { _ = page.Close() }() + if _, err := page.EvalOnNewDocument(stealth.JS); err != nil { + return RenderResult{}, fmt.Errorf("inject stealth: %w", err) + } page = page.Context(ctx).Timeout(p.opts.RenderTimeout)