fix: use --restore-last-session instead of unsigned Preferences restore_on_startup
All checks were successful
技能自动化发布 / release (push) Successful in 5s

This commit is contained in:
2026-09-16 14:38:03 +08:00
parent 7709292f1c
commit 71b20e6e4a
5 changed files with 93 additions and 24 deletions

View File

@@ -87,21 +87,29 @@ HUMAN_CHROME_FLAGS = (
"--disable-search-engine-choice-screen",
)
# 强制恢复上次会话(含 Session Cookie)。不依赖 Preferences.restore_on_startup
#(该项受 Secure Preferences HMAC 保护,裸写会被 Chrome 否掉)。
RESTORE_LAST_SESSION_FLAG = "--restore-last-session"
def human_chrome_launch_args(profile_dir, url=None):
def human_chrome_launch_args(profile_dir, url=None, restore_session=False):
"""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)
cleaned = (url or "").strip()
if cleaned:
args.append(cleaned)
return args
def human_chrome_shortcut_arguments(profile_dir, url=None):
def human_chrome_shortcut_arguments(profile_dir, url=None, restore_session=False):
"""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)
cleaned = (url or "").strip()
if cleaned:
parts.append(cleaned)
@@ -307,7 +315,7 @@ def cmd_open(account_id):
)
return
# 独立 Profile 默认不会「继续上次会话」,Session Cookie 关窗即丢;打开前写入 Preferences
# 清理 exit_type 等(不写 MAC 保护的 restore_on_startup);有会话时靠 --restore-last-session
prefs_result = ensure_session_restore(profile_abs)
log.info("browser_open_prefs %s", prefs_result)
@@ -317,21 +325,26 @@ def cmd_open(account_id):
finally:
conn.close()
# 有可恢复会话时不带 URL;首次/空 Profile 仍打开平台首页便于登录。
# 须识别现代 Chrome 的 Default/Sessions/Session_*(仅认旧文件名会导致每次都开首页冲掉 Session Cookie)。
# 有可恢复会话:不带 URL + --restore-last-session(恢复标签与 Session Cookie)。
# 首次/空 Profile:仍打开平台首页便于登录。
has_session = profile_has_restorable_session(profile_abs)
launch_url = None if has_session else url
cmd = [browser_exe, *human_chrome_launch_args(profile_abs, launch_url)]
cmd = [
browser_exe,
*human_chrome_launch_args(
profile_abs, launch_url, restore_session=has_session
),
]
log.info(
"browser_open_launch exe=%s profile_dir=%s launch_url=%s has_session=%s "
"platform=%s restore_on_startup=%s",
"restore_last_session=%s platform=%s",
browser_exe,
profile_abs,
launch_url or "(restore)",
has_session,
has_session,
target.get("platform_key"),
prefs_result.get("restore_after"),
)
try:
@@ -381,6 +394,8 @@ def cmd_open(account_id):
"profile_dir": profile_abs,
"url": url,
"pid": child.pid,
"has_session": has_session,
"restore_last_session": has_session,
"session_restore_on_startup": prefs_result.get("restore_after"),
},
},