feat(apply): open product page on left double click
This commit is contained in:
@@ -429,6 +429,27 @@ def _product_url(account, item_id):
|
||||
)
|
||||
|
||||
|
||||
def _find_exact_product_tab(account, item_id, host):
|
||||
"""只返回该账号已打开且商品路径精确匹配的 target。"""
|
||||
|
||||
expected = urlparse(_product_url(account, item_id))
|
||||
expected_host = expected.netloc.lower()
|
||||
expected_path = expected.path.rstrip("/")
|
||||
for target in http_get("/json", host=host):
|
||||
if target.get("type") != "page" or not target.get("webSocketDebuggerUrl"):
|
||||
continue
|
||||
try:
|
||||
current = urlparse(str(target.get("url") or ""))
|
||||
except Exception:
|
||||
continue
|
||||
if (
|
||||
current.netloc.lower() == expected_host
|
||||
and current.path.rstrip("/") == expected_path
|
||||
):
|
||||
return target
|
||||
return None
|
||||
|
||||
|
||||
def _item_id(task):
|
||||
value = _get(task, "item_id", "itemid", "product_id")
|
||||
if not value:
|
||||
@@ -815,6 +836,9 @@ def _wait_ready(cdp, timeout=60):
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
current_url = str(last_state.get("current_url") or _current_url(cdp) or "")
|
||||
if _is_login_url(current_url):
|
||||
raise EditorError("账号未登录,请先到④账号管理人工登录蝦皮")
|
||||
invalid_text = _product_unavailable_toast_text(read_page_toasts(cdp))
|
||||
if invalid_text:
|
||||
raise EditorError(product_unavailable_error_message(invalid_text))
|
||||
@@ -1105,6 +1129,45 @@ def open_product(account, item_id, on_step=None, bring_to_front=True) -> CDP:
|
||||
raise
|
||||
|
||||
|
||||
def open_or_focus_product_tab(account, item_id):
|
||||
"""聚焦现有精确商品页,或新建商品页且不刷新用户已有页面。
|
||||
|
||||
此方法不能复用 ``open_product``:真实更新需要重新导航,人工查看不能
|
||||
刷新用户正在编辑、尚未保存的商品页。
|
||||
"""
|
||||
|
||||
host = _cdp_host(account)
|
||||
item_id = str(item_id)
|
||||
url = _product_url(account, item_id)
|
||||
target = _find_exact_product_tab(account, item_id, host)
|
||||
created_by_app = target is None
|
||||
if target is None:
|
||||
target = create_tab_info(url, host=host, background=False)
|
||||
|
||||
cdp = CDP(target["webSocketDebuggerUrl"])
|
||||
cdp.target_id = target.get("id")
|
||||
cdp.created_by_app = created_by_app
|
||||
cdp.cdp_host = host
|
||||
try:
|
||||
_ensure_page_domains(cdp)
|
||||
if created_by_app:
|
||||
install_toast_observer(cdp)
|
||||
cdp.send("Page.bringToFront")
|
||||
if created_by_app:
|
||||
_wait_ready(cdp)
|
||||
return {
|
||||
"item_id": item_id,
|
||||
"created": created_by_app,
|
||||
"target_id": target.get("id"),
|
||||
}
|
||||
except Exception:
|
||||
if created_by_app:
|
||||
_close_open_product_failure(cdp)
|
||||
raise
|
||||
finally:
|
||||
cdp.close()
|
||||
|
||||
|
||||
def read_title(cdp) -> str:
|
||||
"""Read the current Shopee title input value."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user