Files
cmshoppe/docs/tasks/T-562.md
T

3.6 KiB

id, title, phase, deps, status, created
id title phase deps status created
T-562 ①采集打开商品页不主动切换 Chrome 前台 2
T-203
T-205b
T-560
DONE 2026-07-08

问题 / 背景

运行①「导入采集」时,每采集一个商品都会打开或复用对应账号 Chrome 的商品详情页,并把该 Chrome/标签页切到前台。批量采集多店铺、多商品时,用户正在操作其它窗口会被频繁抢焦点,体验很差。

代码原因:

  • app/editor.py::open_product() 在打开/复用商品页后固定执行 Page.bringToFront,这是 Chrome DevTools Protocol 明确的前台激活命令。
  • open_product() 同时服务①采集和③更新。早期强制前台便于调试和观察更新动作,但①采集是只读流程,不需要每条都切前台。
  • 新建商品 tab 使用 app/cdp.py::create_tab_info() 的 Target.createTarget。Chrome 新建 tab 本身也可能激活标签页;可尝试传 background=true 降低新建 tab 抢焦点概率。

方案

  1. 给打开商品页增加前台控制参数

    • editor.open_product(account, item_id, on_step=None, bring_to_front=True) 增加参数,默认保持旧行为。
    • 仅当 bring_to_front=True 时执行 Page.bringToFront。
    • ③ apply_task() 继续使用默认 True,避免影响改标题、换封面、上传、拖拽和 Shopee 确认框稳定性。
    • ① collect() 调用 open_product(..., bring_to_front=False),采集不主动把页面带到前台。
  2. 新建 tab 尽量后台创建

    • cdp.create_tab_info(url, host=None, background=False) 增加 background 参数。
    • 当 background=True 时,Target.createTarget 参数中带 background: true。
    • open_product(..., bring_to_front=False) 新建商品 tab 时传 background=True;复用已有 tab 时不激活。
    • 如果 Chrome 版本忽略或拒绝 background,则退回普通新建 tab,仍至少不再执行 Page.bringToFront。
  3. 测试

    • editor 单测覆盖:采集调用不发送 Page.bringToFront,更新调用仍发送。
    • cdp 单测覆盖:create_tab_info(background=True) 会把 background: true 传给 Target.createTarget,并覆盖 background 被拒绝后的普通新建回退。
    • 保持失败时自动关闭本轮新建 tab、复用用户已有 tab 不关闭的原语义。

验收要点

  • ①采集每个商品详情页时不再主动调用 Page.bringToFront。
  • ①采集新建商品 tab 时尽量后台创建,降低 Chrome 抢焦点概率。
  • ③更新蝦皮仍保持默认前台行为,不改变上传、拖拽、点击更新和确认框处理。
  • 商品失效/打开失败时,本轮自动新建 tab 仍会关闭;复用用户已有 tab 不关闭。
  • python -m ruff check app tests main.py、py -3.10 -m compileall app main.py、py -3.10 -m unittest discover -s tests、git diff --check 通过。

边界(不改什么)

只改①采集打开商品页的前台激活策略和 CDP 新建 tab 参数;不改标题/封面采集逻辑、不改商品详情页选择器、不改③更新流程、不改 DB schema、不改 Excel、AI、cmhub、账号登录检测或 Shopee 提交逻辑。

执行记录

  • 2026-07-08:完成。editor.open_product() 增加 bring_to_front 参数;①采集调用 bring_to_front=False,不主动 Page.bringToFront;③更新继续默认前台激活。cdp.create_tab_info() 增加 background 参数,后台创建失败时自动回退普通新建。已同步 docs/04-architecture.md 和 docs/routes.md。
  • 2026-07-08:验证通过:python -m ruff check app tests main.py、py -3.10 -m compileall app main.py、py -3.10 -m unittest discover -s tests、git diff --check。