fix: avoid focusing chrome during collection

This commit is contained in:
chengma
2026-07-08 18:03:59 +08:00
parent 8a8d656a58
commit 521d363ab2
7 changed files with 232 additions and 15 deletions
+10 -2
View File
@@ -160,12 +160,20 @@ def find_product_tab(item_id, host=None):
return None
def create_tab_info(url, host=None):
def create_tab_info(url, host=None, background=False):
"""用 browser 级 Target.createTarget 新建 tab,返回其 page target 信息。"""
ver = http_get("/json/version", host=host)
b = CDP(ver["webSocketDebuggerUrl"])
try:
tid = b.send("Target.createTarget", {"url": url})["targetId"]
params = {"url": url}
if background:
params["background"] = True
try:
tid = b.send("Target.createTarget", params)["targetId"]
except RuntimeError:
if not background:
raise
tid = b.send("Target.createTarget", {"url": url})["targetId"]
finally:
b.close()
end = time.time() + 15
+8 -7
View File
@@ -753,7 +753,7 @@ def _close_open_product_failure(cdp):
pass
def open_product(account, item_id, on_step=None) -> CDP:
def open_product(account, item_id, on_step=None, bring_to_front=True) -> CDP:
"""Open or reuse a product edit tab, navigate to a clean edit URL, and wait ready."""
_notify_collect_step(on_step, "open_product")
@@ -763,7 +763,7 @@ def open_product(account, item_id, on_step=None) -> CDP:
tab = find_product_tab(item_id, host=host)
created_by_app = tab is None
if tab is None:
tab = create_tab_info(url, host=host)
tab = create_tab_info(url, host=host, background=not bring_to_front)
ws = tab["webSocketDebuggerUrl"]
cdp = CDP(ws)
cdp.target_id = tab.get("id")
@@ -772,10 +772,11 @@ def open_product(account, item_id, on_step=None) -> CDP:
try:
_ensure_page_domains(cdp)
install_toast_observer(cdp)
try:
cdp.send("Page.bringToFront")
except Exception:
pass
if bring_to_front:
try:
cdp.send("Page.bringToFront")
except Exception:
pass
cdp.send("Page.navigate", {"url": url})
install_toast_observer(cdp)
_notify_collect_step(on_step, "wait_ready")
@@ -826,7 +827,7 @@ def collect(account, task, on_step=None) -> dict:
"""Collect current title and cover snapshot before any edits."""
item_id = _item_id(task)
cdp = open_product(account, item_id, on_step=on_step)
cdp = open_product(account, item_id, on_step=on_step, bring_to_front=False)
try:
_notify_collect_step(on_step, "read_title")
old_title = read_title(cdp)