feat: open browser with stable CDP port for scrape attach
All checks were successful
技能自动化发布 / release (push) Successful in 11s
All checks were successful
技能自动化发布 / release (push) Successful in 11s
This commit is contained in:
@@ -91,25 +91,46 @@ HUMAN_CHROME_FLAGS = (
|
||||
#(该项受 Secure Preferences HMAC 保护,裸写会被 Chrome 否掉)。
|
||||
RESTORE_LAST_SESSION_FLAG = "--restore-last-session"
|
||||
|
||||
# 与 scrape-product-colipu 等业务技能约定同一算法,便于 CDP 挂接已打开的账号窗口。
|
||||
_CDP_PORT_BASE = 9330
|
||||
_CDP_PORT_SPAN = 60
|
||||
|
||||
def human_chrome_launch_args(profile_dir, url=None, restore_session=False):
|
||||
|
||||
def cdp_port_for_profile(profile_dir: str) -> int:
|
||||
"""按 profile 绝对路径派生稳定本机调试端口(9330–9389)。"""
|
||||
import hashlib
|
||||
|
||||
key = os.path.abspath(profile_dir).replace("/", "\\").lower().encode("utf-8")
|
||||
digest = hashlib.sha1(key).hexdigest()
|
||||
return _CDP_PORT_BASE + (int(digest[:8], 16) % _CDP_PORT_SPAN)
|
||||
|
||||
|
||||
def human_chrome_launch_args(profile_dir, url=None, restore_session=False, *, enable_cdp=True):
|
||||
"""chrome.exe 之后的参数(Popen 列表形式,路径不额外加引号)。"""
|
||||
profile_abs = os.path.abspath(profile_dir)
|
||||
args = [f"--user-data-dir={profile_abs}", *HUMAN_CHROME_FLAGS]
|
||||
if restore_session:
|
||||
args.append(RESTORE_LAST_SESSION_FLAG)
|
||||
if enable_cdp:
|
||||
port = cdp_port_for_profile(profile_abs)
|
||||
args.append(f"--remote-debugging-port={port}")
|
||||
args.append("--remote-debugging-address=127.0.0.1")
|
||||
cleaned = (url or "").strip()
|
||||
if cleaned:
|
||||
args.append(cleaned)
|
||||
return args
|
||||
|
||||
|
||||
def human_chrome_shortcut_arguments(profile_dir, url=None, restore_session=False):
|
||||
def human_chrome_shortcut_arguments(profile_dir, url=None, restore_session=False, *, enable_cdp=True):
|
||||
"""Windows .lnk Arguments 字符串(user-data-dir 带引号)。"""
|
||||
profile_abs = os.path.abspath(profile_dir)
|
||||
parts = [f'--user-data-dir="{profile_abs}"', *HUMAN_CHROME_FLAGS]
|
||||
if restore_session:
|
||||
parts.append(RESTORE_LAST_SESSION_FLAG)
|
||||
if enable_cdp:
|
||||
port = cdp_port_for_profile(profile_abs)
|
||||
parts.append(f"--remote-debugging-port={port}")
|
||||
parts.append("--remote-debugging-address=127.0.0.1")
|
||||
cleaned = (url or "").strip()
|
||||
if cleaned:
|
||||
parts.append(cleaned)
|
||||
@@ -329,21 +350,23 @@ def cmd_open(account_id):
|
||||
# 首次/空 Profile:仍打开平台首页便于登录。
|
||||
has_session = profile_has_restorable_session(profile_abs)
|
||||
launch_url = None if has_session else url
|
||||
cdp_port = cdp_port_for_profile(profile_abs)
|
||||
cmd = [
|
||||
browser_exe,
|
||||
*human_chrome_launch_args(
|
||||
profile_abs, launch_url, restore_session=has_session
|
||||
profile_abs, launch_url, restore_session=has_session, enable_cdp=True
|
||||
),
|
||||
]
|
||||
|
||||
log.info(
|
||||
"browser_open_launch exe=%s profile_dir=%s launch_url=%s has_session=%s "
|
||||
"restore_last_session=%s platform=%s",
|
||||
"restore_last_session=%s cdp_port=%s platform=%s",
|
||||
browser_exe,
|
||||
profile_abs,
|
||||
launch_url or "(restore)",
|
||||
has_session,
|
||||
has_session,
|
||||
cdp_port,
|
||||
target.get("platform_key"),
|
||||
)
|
||||
|
||||
@@ -396,6 +419,7 @@ def cmd_open(account_id):
|
||||
"pid": child.pid,
|
||||
"has_session": has_session,
|
||||
"restore_last_session": has_session,
|
||||
"cdp_port": cdp_port,
|
||||
"session_restore_on_startup": prefs_result.get("restore_after"),
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user