fix: 采集后关闭自动新建商品页

- 新增 close_tab 和 create_tab_info,区分断开 CDP 连接与关闭浏览器 target

- open_product 标记商品页是否本轮自动新建,collect 结束后只关闭自动新建 tab

- 保留用户原本打开的商品 tab,③ 更新流程暂不自动关闭页面

- 补充 editor tab 生命周期测试并同步架构、API、任务和进度文档
This commit is contained in:
chengma
2026-06-27 16:39:12 +08:00
parent 789e82991f
commit e6d7de3d7c
9 changed files with 186 additions and 15 deletions
+22 -2
View File
@@ -5,7 +5,7 @@ import os
import time
from urllib.parse import urlparse
from .cdp import CDP, create_tab, find_product_tab, http_get
from .cdp import CDP, close_tab, create_tab, create_tab_info, find_product_tab, http_get
DEFAULT_REGION_HOST = "seller.shopee.tw"
@@ -272,8 +272,14 @@ def open_product(account, item_id) -> CDP:
item_id = str(item_id)
url = _product_url(account, item_id)
tab = find_product_tab(item_id, host=host)
ws = tab["webSocketDebuggerUrl"] if tab else create_tab(url, host=host)
created_by_app = tab is None
if tab is None:
tab = create_tab_info(url, host=host)
ws = tab["webSocketDebuggerUrl"]
cdp = CDP(ws)
cdp.target_id = tab.get("id")
cdp.created_by_app = created_by_app
cdp.cdp_host = host
_ensure_page_domains(cdp)
try:
cdp.send("Page.bringToFront")
@@ -338,7 +344,21 @@ def collect(account, task) -> dict:
"old_cover_path": old_cover_path,
}
finally:
_close_collected_product(cdp)
def _close_collected_product(cdp):
target_id = getattr(cdp, "target_id", None)
created_by_app = bool(getattr(cdp, "created_by_app", False))
host = getattr(cdp, "cdp_host", None)
try:
cdp.close()
finally:
if created_by_app and target_id:
try:
close_tab(target_id, host=host)
except Exception:
pass
def change_title(cdp, new_title) -> dict: