Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| beb95c1415 | |||
| e2eafb7407 |
7
SKILL.md
7
SKILL.md
@@ -1,7 +1,7 @@
|
|||||||
---
|
---
|
||||||
name: 账号管理
|
name: 账号管理
|
||||||
description: 多平台多账号管理。管理各平台账号与Chrome Profile的对应关系,供publisher类Skill调用获取账号信息。
|
description: 多平台多账号管理。管理各平台账号与Chrome Profile的对应关系,供publisher类Skill调用获取账号信息。
|
||||||
version: 1.0.5
|
version: 1.0.50
|
||||||
author: 深圳匠厂科技有限公司
|
author: 深圳匠厂科技有限公司
|
||||||
metadata:
|
metadata:
|
||||||
openclaw:
|
openclaw:
|
||||||
@@ -53,14 +53,13 @@ python3 {baseDir}/scripts/main.py list all
|
|||||||
python3 {baseDir}/scripts/main.py list 搜狐号
|
python3 {baseDir}/scripts/main.py list 搜狐号
|
||||||
```
|
```
|
||||||
|
|
||||||
### 跨技能:单行 JSON(get / list-json / pick-web);需会话有效时用 wait-login
|
### 跨技能:单行 JSON(get / list-json / pick-web)
|
||||||
```bash
|
```bash
|
||||||
python3 {baseDir}/scripts/main.py get <id>
|
python3 {baseDir}/scripts/main.py get <id>
|
||||||
python3 {baseDir}/scripts/main.py list-json all --limit 50
|
python3 {baseDir}/scripts/main.py list-json all --limit 50
|
||||||
python3 {baseDir}/scripts/main.py pick-web 搜狐号
|
python3 {baseDir}/scripts/main.py pick-web 搜狐号
|
||||||
python3 {baseDir}/scripts/main.py wait-login <id>
|
|
||||||
```
|
```
|
||||||
约定与字段说明见 `references/INTEGRATION.md`;错误前缀见 `references/ERRORS.md`。
|
约定与字段说明见 `references/INTEGRATION.md`;错误前缀见 `references/ERRORS.md`。是否已登录由调用方网页会话处理,本 skill 不做编排用预检。
|
||||||
|
|
||||||
## 扩展文档(便于 AI 精确调用)
|
## 扩展文档(便于 AI 精确调用)
|
||||||
|
|
||||||
|
|||||||
@@ -81,16 +81,7 @@ python main.py login <id>
|
|||||||
|
|
||||||
- 依赖 Playwright;超时与轮询间隔等见 [RUNTIME.md](RUNTIME.md)。
|
- 依赖 Playwright;超时与轮询间隔等见 [RUNTIME.md](RUNTIME.md)。
|
||||||
- **不**在库中持久化「是否已登录」;仅刷新 `updated_at` 供 `pick-web` 等排序参考。
|
- **不**在库中持久化「是否已登录」;仅刷新 `updated_at` 供 `pick-web` 等排序参考。
|
||||||
|
- 跨技能编排(如 llm-manager 网页生成)**不再**通过本 skill 单独做「登录预检」;由业务侧在同一浏览器会话内处理未登录态。
|
||||||
### `wait-login` — 跨技能:**始终以本次 DOM 检测为准**
|
|
||||||
|
|
||||||
```bash
|
|
||||||
python main.py wait-login <id>
|
|
||||||
```
|
|
||||||
|
|
||||||
- 由**账号管理**打开浏览器并完成 DOM 检测;调用方只需 `subprocess` 等待本进程结束,成功时 stdout 含 `OK:WAIT_LOGIN`。
|
|
||||||
- 会先打印若干行说明(含 `INFO:WAIT_LOGIN_BEGIN`),再进入检测;超时、关窗、启动失败等见 [ERRORS.md](ERRORS.md)。
|
|
||||||
- 时间窗由 `JIANGCHANG_WAIT_LOGIN_TIMEOUT_SECONDS` 控制,见 [RUNTIME.md](RUNTIME.md)。
|
|
||||||
|
|
||||||
### `delete` — 删除库记录并尽量删除 profile 目录
|
### `delete` — 删除库记录并尽量删除 profile 目录
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,6 @@
|
|||||||
| `ERROR:CLI_DELETE_BAD_MODE` | `delete` 第二段不是 `id`/`platform` |
|
| `ERROR:CLI_DELETE_BAD_MODE` | `delete` 第二段不是 `id`/`platform` |
|
||||||
| `ERROR:CLI_LIST_JSON_MISSING_PLATFORM` | `list-json` 缺少平台参数 |
|
| `ERROR:CLI_LIST_JSON_MISSING_PLATFORM` | `list-json` 缺少平台参数 |
|
||||||
| `ERROR:CLI_PICK_WEB_MISSING_ARGS` | `pick-web` 缺少平台 |
|
| `ERROR:CLI_PICK_WEB_MISSING_ARGS` | `pick-web` 缺少平台 |
|
||||||
| `ERROR:CLI_WAIT_LOGIN_MISSING_ID` | `wait-login` 缺少 id |
|
|
||||||
| `ERROR:CLI_UNKNOWN_OR_BAD_ARGS` | 无法识别的子命令或参数组合 |
|
| `ERROR:CLI_UNKNOWN_OR_BAD_ARGS` | 无法识别的子命令或参数组合 |
|
||||||
|
|
||||||
## `ERROR:` — 业务与数据
|
## `ERROR:` — 业务与数据
|
||||||
@@ -39,19 +38,6 @@
|
|||||||
| `ERROR:需要 playwright:pip install playwright && playwright install chromium` | 未安装 Playwright 或浏览器未安装 |
|
| `ERROR:需要 playwright:pip install playwright && playwright install chromium` | 未安装 Playwright 或浏览器未安装 |
|
||||||
| `ERROR:PROFILE_DIR_MISSING` | 账号记录中 `profile_dir` 为空 |
|
| `ERROR:PROFILE_DIR_MISSING` | 账号记录中 `profile_dir` 为空 |
|
||||||
|
|
||||||
## `ERROR:` — `wait-login`(跨技能编排)
|
|
||||||
|
|
||||||
| 前缀 | 含义 |
|
|
||||||
|------|------|
|
|
||||||
| `ERROR:WAIT_LOGIN_ABORTED` | 用户关闭浏览器,放弃登录 |
|
|
||||||
| `ERROR:WAIT_LOGIN_TIMEOUT` | 限时内未完成登录(默认由 `JIANGCHANG_WAIT_LOGIN_TIMEOUT_SECONDS` 控制,约 180s) |
|
|
||||||
| `ERROR:WAIT_LOGIN_LAUNCH_FAILED` | 无法启动浏览器或创建持久化上下文(端口/占用/安装问题等) |
|
|
||||||
| `ERROR:WAIT_LOGIN_PAGE_ERROR` | 打开登录页或轮询 DOM 时异常 |
|
|
||||||
| `ERROR:WAIT_LOGIN_CONFIG_ERROR` | 子进程配置异常 |
|
|
||||||
| `ERROR:WAIT_LOGIN_CHILD_CRASH` | 子进程异常退出且未写入有效结果文件 |
|
|
||||||
|
|
||||||
编排技能(如 llm-manager)另可能打印 `ERROR:WAIT_LOGIN_OUTER_TIMEOUT`:等待本进程整体超过上限(登录窗口 + 子进程缓冲),已中止。
|
|
||||||
|
|
||||||
## 非前缀输出
|
## 非前缀输出
|
||||||
|
|
||||||
- **成功添加账号**、**删除成功**等可能包含 `✅` 等人类可读行,**不宜**作为稳定协议解析。
|
- **成功添加账号**、**删除成功**等可能包含 `✅` 等人类可读行,**不宜**作为稳定协议解析。
|
||||||
|
|||||||
@@ -24,12 +24,12 @@
|
|||||||
|
|
||||||
常见键名包括:`id`, `name`, `platform`, `phone`, `profile_dir`, `url`, `created_at`, `updated_at`;`extra_json` 列中的扁平扩展可能并入同一对象。
|
常见键名包括:`id`, `name`, `platform`, `phone`, `profile_dir`, `url`, `created_at`, `updated_at`;`extra_json` 列中的扁平扩展可能并入同一对象。
|
||||||
|
|
||||||
**库中不再持久化「是否已登录」**;编排侧需登录时请调用 `wait-login <id>`(始终以本次 Playwright DOM 检测为准)或在业务页内自行判断。
|
**库中不再持久化「是否已登录」**;编排侧在网页自动化中自行处理登录(与 `pick-web` / `get` 返回的 `profile_dir` 同一浏览器会话)。
|
||||||
|
|
||||||
## Publisher / 自动化流水线示例
|
## Publisher / 自动化流水线示例
|
||||||
|
|
||||||
1. `pick-web 搜狐号`(或目标平台)→ 解析 JSON → 取 `id`、`profile_dir`、`url`。
|
1. `pick-web 搜狐号`(或目标平台)→ 解析 JSON → 取 `id`、`profile_dir`、`url`。
|
||||||
2. 需要确保会话有效时:调用 `wait-login <id>`,成功则 stdout 首行含 `OK:WAIT_LOGIN`。
|
2. 使用 `profile_dir` 启动持久化浏览器上下文,在业务页面内完成或等待登录。
|
||||||
|
|
||||||
## 退出码
|
## 退出码
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from service.account_service import (
|
|||||||
cmd_list_json,
|
cmd_list_json,
|
||||||
cmd_pick_web,
|
cmd_pick_web,
|
||||||
)
|
)
|
||||||
from service.browser_service import cmd_login, cmd_open, cmd_wait_login
|
from service.browser_service import cmd_login, cmd_open
|
||||||
from util.constants import LOG_LOGGER_NAME, SKILL_SLUG
|
from util.constants import LOG_LOGGER_NAME, SKILL_SLUG
|
||||||
from util.logging_config import get_skill_logger, setup_skill_logging
|
from util.logging_config import get_skill_logger, setup_skill_logging
|
||||||
from util.platforms import _platform_list_cn_for_help, resolve_platform_key
|
from util.platforms import _platform_list_cn_for_help, resolve_platform_key
|
||||||
@@ -28,7 +28,6 @@ def _cli_print_full_usage() -> None:
|
|||||||
print(" python main.py get <id>")
|
print(" python main.py get <id>")
|
||||||
print(" python main.py open <id>")
|
print(" python main.py open <id>")
|
||||||
print(" python main.py login <id>")
|
print(" python main.py login <id>")
|
||||||
print(" python main.py wait-login <id> # 跨技能:限时内 DOM 检测登录(默认 180s)")
|
|
||||||
print(" python main.py delete id <id>")
|
print(" python main.py delete id <id>")
|
||||||
print(" python main.py delete platform <平台>")
|
print(" python main.py delete platform <平台>")
|
||||||
print(" python main.py delete platform <平台> <手机号>")
|
print(" python main.py delete platform <平台> <手机号>")
|
||||||
@@ -126,7 +125,7 @@ def main() -> None:
|
|||||||
if len(sys.argv) < 3:
|
if len(sys.argv) < 3:
|
||||||
print("ERROR:CLI_PICK_WEB_MISSING_ARGS")
|
print("ERROR:CLI_PICK_WEB_MISSING_ARGS")
|
||||||
print("用法:python main.py pick-web <平台中文名或英文键>")
|
print("用法:python main.py pick-web <平台中文名或英文键>")
|
||||||
print("说明:按 updated_at 选一条账号;是否已登录由 wait-login / 页面检测决定。")
|
print("说明:按 updated_at 选一条账号;是否已登录由调用方网页会话自行处理。")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
cmd_pick_web(sys.argv[2])
|
cmd_pick_web(sys.argv[2])
|
||||||
elif cmd == "open":
|
elif cmd == "open":
|
||||||
@@ -134,14 +133,6 @@ def main() -> None:
|
|||||||
_cli_fail_need_account_id("open")
|
_cli_fail_need_account_id("open")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
cmd_open(sys.argv[2])
|
cmd_open(sys.argv[2])
|
||||||
elif cmd == "wait-login":
|
|
||||||
av = [a for a in sys.argv[2:] if a]
|
|
||||||
if len(av) < 1:
|
|
||||||
print("ERROR:CLI_WAIT_LOGIN_MISSING_ID")
|
|
||||||
print("用法:python main.py wait-login <账号 id>")
|
|
||||||
print("说明:始终以本次 DOM 检测为准;供 llm-manager / 搜狐发布等编排。")
|
|
||||||
sys.exit(1)
|
|
||||||
sys.exit(cmd_wait_login(av[0]))
|
|
||||||
elif cmd == "login":
|
elif cmd == "login":
|
||||||
if len(sys.argv) < 3:
|
if len(sys.argv) < 3:
|
||||||
_cli_fail_need_account_id("login")
|
_cli_fail_need_account_id("login")
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ def cmd_list_json(platform_input: str, limit: int = 200) -> None:
|
|||||||
def cmd_pick_web(platform_input: str):
|
def cmd_pick_web(platform_input: str):
|
||||||
"""
|
"""
|
||||||
供 llm-manager 等:取该平台用于网页自动化的账号候选。
|
供 llm-manager 等:取该平台用于网页自动化的账号候选。
|
||||||
按 updated_at、created_at 倒序选一条,与库内「登录态」无关;实际是否已登录由 wait-login / 页面 DOM 判定。
|
按 updated_at、created_at 倒序选一条;是否已登录由调用方(如网页自动化)在同一会话内处理。
|
||||||
成功:stdout 仅一行 JSON(与 get 一致);失败:首行 ERROR:。
|
成功:stdout 仅一行 JSON(与 get 一致);失败:首行 ERROR:。
|
||||||
"""
|
"""
|
||||||
get_skill_logger().info("pick_web platform_input=%r", platform_input)
|
get_skill_logger().info("pick_web platform_input=%r", platform_input)
|
||||||
|
|||||||
@@ -99,14 +99,6 @@ def _login_dom_grace_seconds() -> float:
|
|||||||
return 4.0
|
return 4.0
|
||||||
|
|
||||||
|
|
||||||
def _wait_login_timeout_seconds() -> int:
|
|
||||||
"""供 wait-login / llm 编排:默认 180 秒,JIANGCHANG_WAIT_LOGIN_TIMEOUT_SECONDS。"""
|
|
||||||
try:
|
|
||||||
return max(30, min(600, int((os.getenv("JIANGCHANG_WAIT_LOGIN_TIMEOUT_SECONDS") or "180").strip())))
|
|
||||||
except ValueError:
|
|
||||||
return 180
|
|
||||||
|
|
||||||
|
|
||||||
def _login_out_bundle_for(platform_key: str) -> dict:
|
def _login_out_bundle_for(platform_key: str) -> dict:
|
||||||
"""传给子进程:anchor + 未登录可见的选择器列表。"""
|
"""传给子进程:anchor + 未登录可见的选择器列表。"""
|
||||||
spec = PLATFORMS.get(platform_key, {}) if platform_key else {}
|
spec = PLATFORMS.get(platform_key, {}) if platform_key else {}
|
||||||
@@ -176,6 +168,13 @@ def _run_login_browser_session(
|
|||||||
if verbose_ui:
|
if verbose_ui:
|
||||||
print(f"正在为账号 [{target['name']}] 打开 {browser_name} …")
|
print(f"正在为账号 [{target['name']}] 打开 {browser_name} …")
|
||||||
print(f"访问地址:{url}")
|
print(f"访问地址:{url}")
|
||||||
|
if platform == "gemini":
|
||||||
|
print(
|
||||||
|
"Gemini / Google 登录:若提示「浏览器不安全」或「无法登录」,请关闭本窗口后,"
|
||||||
|
"用普通 Chrome 启动参数 --user-data-dir= 指向下面 profile 目录,在普通浏览器里完成"
|
||||||
|
" Google 登录后完全退出 Chrome,再执行本 login;详见 llm-manager Gemini 引擎说明。"
|
||||||
|
)
|
||||||
|
print(f" (本账号 profile 目录){profile_dir}")
|
||||||
print(
|
print(
|
||||||
"请在窗口内完成登录。系统根据页面是否仍出现「登录」等未登录控件判断;"
|
"请在窗口内完成登录。系统根据页面是否仍出现「登录」等未登录控件判断;"
|
||||||
"成功后自动关闭窗口并写入状态(无需手动关浏览器)。"
|
"成功后自动关闭窗口并写入状态(无需手动关浏览器)。"
|
||||||
@@ -346,110 +345,6 @@ def cmd_login(account_id):
|
|||||||
print(f"ℹ️ 详细轮询日志见:{log_file}(可将 JIANGCHANG_LOG_LEVEL=DEBUG 打开更细粒度)")
|
print(f"ℹ️ 详细轮询日志见:{log_file}(可将 JIANGCHANG_LOG_LEVEL=DEBUG 打开更细粒度)")
|
||||||
|
|
||||||
|
|
||||||
def cmd_wait_login(account_id) -> int:
|
|
||||||
"""
|
|
||||||
跨技能编排:始终以本次 Playwright DOM 检测为准;限时内打开浏览器并等待用户完成登录。
|
|
||||||
返回 0:本次检测判定已登录;1:失败(超时 / 用户关浏览器 / 其它)。
|
|
||||||
stdout 含 OK:WAIT_LOGIN 或 ERROR:WAIT_LOGIN_*。
|
|
||||||
"""
|
|
||||||
log = get_skill_logger()
|
|
||||||
log.info("wait_login_command account_id=%r", account_id)
|
|
||||||
target = get_account_by_id(account_id)
|
|
||||||
if not target:
|
|
||||||
log.warning("wait_login_aborted account_not_found account_id=%r", account_id)
|
|
||||||
print("ERROR:ACCOUNT_NOT_FOUND")
|
|
||||||
return 1
|
|
||||||
|
|
||||||
if not resolve_chromium_channel():
|
|
||||||
_print_browser_install_hint()
|
|
||||||
return 1
|
|
||||||
try:
|
|
||||||
import playwright # noqa: F401
|
|
||||||
except ImportError:
|
|
||||||
print("ERROR:需要 playwright:pip install playwright && playwright install chromium")
|
|
||||||
return 1
|
|
||||||
if not (target.get("profile_dir") or "").strip():
|
|
||||||
print("ERROR:PROFILE_DIR_MISSING 库中 profile_dir 为空。")
|
|
||||||
return 1
|
|
||||||
|
|
||||||
sec = _wait_login_timeout_seconds()
|
|
||||||
log.info(
|
|
||||||
"wait_login_orchestration account_id=%s name=%r timeout_sec=%s",
|
|
||||||
target.get("id"),
|
|
||||||
target.get("name"),
|
|
||||||
sec,
|
|
||||||
)
|
|
||||||
print("INFO:WAIT_LOGIN_BEGIN", flush=True)
|
|
||||||
print(
|
|
||||||
f"账号管理(account-manager)正在为本账号执行登录检测:将打开浏览器,请在 {sec} 秒内在窗口中完成登录。",
|
|
||||||
flush=True,
|
|
||||||
)
|
|
||||||
print(
|
|
||||||
"说明:调用方/编排技能只需等待本进程结束;打开浏览器与 DOM 检测均由账号管理负责。",
|
|
||||||
flush=True,
|
|
||||||
)
|
|
||||||
print(
|
|
||||||
"· 超时未登录成功 → 本进程报错退出,请中止后续业务。",
|
|
||||||
flush=True,
|
|
||||||
)
|
|
||||||
print(
|
|
||||||
"· 提前关闭浏览器 → 视为放弃登录,本进程立即报错退出。",
|
|
||||||
flush=True,
|
|
||||||
)
|
|
||||||
ok, end_reason = _run_login_browser_session(target, sec, verbose_ui=False)
|
|
||||||
if ok:
|
|
||||||
print("OK:WAIT_LOGIN")
|
|
||||||
return 0
|
|
||||||
if end_reason == "user_closed":
|
|
||||||
print("ERROR:WAIT_LOGIN_ABORTED 已关闭浏览器,未完成登录。")
|
|
||||||
return 1
|
|
||||||
if end_reason == "timeout":
|
|
||||||
print("ERROR:WAIT_LOGIN_TIMEOUT 限时内未完成登录。")
|
|
||||||
return 1
|
|
||||||
if end_reason == "playwright_missing":
|
|
||||||
print("ERROR:需要 playwright:pip install playwright && playwright install chromium")
|
|
||||||
return 1
|
|
||||||
if end_reason == "profile_missing":
|
|
||||||
print("ERROR:PROFILE_DIR_MISSING 库中 profile_dir 为空。")
|
|
||||||
return 1
|
|
||||||
if end_reason == "no_channel":
|
|
||||||
_print_browser_install_hint()
|
|
||||||
return 1
|
|
||||||
if end_reason == "launch_failed":
|
|
||||||
print(
|
|
||||||
"ERROR:WAIT_LOGIN_LAUNCH_FAILED 无法启动浏览器或创建用户数据上下文,请确认本机 Chrome/Edge 可用、profile 目录未被占用,详见统一日志。"
|
|
||||||
)
|
|
||||||
return 1
|
|
||||||
if end_reason == "page_error":
|
|
||||||
print(
|
|
||||||
"ERROR:WAIT_LOGIN_PAGE_ERROR 打开登录页或轮询过程异常,请查看上方 stderr 与统一日志。"
|
|
||||||
)
|
|
||||||
return 1
|
|
||||||
if end_reason == "config_error":
|
|
||||||
print("ERROR:WAIT_LOGIN_CONFIG_ERROR 登录子进程配置异常。")
|
|
||||||
return 1
|
|
||||||
if end_reason == "child_crash":
|
|
||||||
print("ERROR:WAIT_LOGIN_CHILD_CRASH 登录检测子进程异常退出且未写入有效结果。")
|
|
||||||
return 1
|
|
||||||
if end_reason == "profile_dir_unusable":
|
|
||||||
print(
|
|
||||||
"ERROR:WAIT_LOGIN_PROFILE_DIR 用户数据目录不可用或无法访问(权限/路径等),详见统一日志中的 login_child_result_detail / stderr。"
|
|
||||||
)
|
|
||||||
return 1
|
|
||||||
if end_reason == "logging_import_failed":
|
|
||||||
print(
|
|
||||||
"ERROR:WAIT_LOGIN_INTERNAL 登录子进程无法加载日志模块,请检查 account-manager 安装是否完整。"
|
|
||||||
)
|
|
||||||
return 1
|
|
||||||
if end_reason == "child_fatal":
|
|
||||||
print(
|
|
||||||
"ERROR:WAIT_LOGIN_CHILD_FATAL 登录子进程异常,详见统一日志 login_child_result_detail 或 login_child_stderr。"
|
|
||||||
)
|
|
||||||
return 1
|
|
||||||
print(f"ERROR:WAIT_LOGIN_FAILED end_reason={end_reason}")
|
|
||||||
return 1
|
|
||||||
|
|
||||||
|
|
||||||
def cmd_open(account_id):
|
def cmd_open(account_id):
|
||||||
"""打开该账号的持久化浏览器,仅用于肉眼确认是否已登录;不写数据库。"""
|
"""打开该账号的持久化浏览器,仅用于肉眼确认是否已登录;不写数据库。"""
|
||||||
get_skill_logger().info("open account_id=%r", account_id)
|
get_skill_logger().info("open account_id=%r", account_id)
|
||||||
|
|||||||
@@ -16,6 +16,12 @@ _scripts_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|||||||
if _scripts_dir not in sys.path:
|
if _scripts_dir not in sys.path:
|
||||||
sys.path.insert(0, _scripts_dir)
|
sys.path.insert(0, _scripts_dir)
|
||||||
|
|
||||||
|
from util.playwright_stealth import (
|
||||||
|
STEALTH_INIT_SCRIPT,
|
||||||
|
persistent_context_launch_parts,
|
||||||
|
stealth_enabled,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _is_context_closed_error(ex: BaseException) -> bool:
|
def _is_context_closed_error(ex: BaseException) -> bool:
|
||||||
s = str(ex).lower()
|
s = str(ex).lower()
|
||||||
@@ -224,13 +230,19 @@ def _run_login_child(c: dict) -> None:
|
|||||||
|
|
||||||
with sync_playwright() as p:
|
with sync_playwright() as p:
|
||||||
try:
|
try:
|
||||||
ctx = p.chromium.launch_persistent_context(
|
args, ignore_automation = persistent_context_launch_parts()
|
||||||
|
lc_kw = dict(
|
||||||
user_data_dir=profile_dir,
|
user_data_dir=profile_dir,
|
||||||
headless=False,
|
headless=False,
|
||||||
channel=c["channel"],
|
channel=c["channel"],
|
||||||
no_viewport=True,
|
no_viewport=True,
|
||||||
args=["--start-maximized"],
|
args=args,
|
||||||
)
|
)
|
||||||
|
if ignore_automation is not None:
|
||||||
|
lc_kw["ignore_default_args"] = ignore_automation
|
||||||
|
ctx = p.chromium.launch_persistent_context(**lc_kw)
|
||||||
|
if stealth_enabled():
|
||||||
|
ctx.add_init_script(STEALTH_INIT_SCRIPT)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if _is_context_closed_error(e):
|
if _is_context_closed_error(e):
|
||||||
end_reason = "user_closed"
|
end_reason = "user_closed"
|
||||||
|
|||||||
@@ -4,18 +4,30 @@ import sys
|
|||||||
|
|
||||||
from playwright.sync_api import sync_playwright
|
from playwright.sync_api import sync_playwright
|
||||||
|
|
||||||
|
from util.playwright_stealth import (
|
||||||
|
STEALTH_INIT_SCRIPT,
|
||||||
|
persistent_context_launch_parts,
|
||||||
|
stealth_enabled,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
with open(sys.argv[1], encoding="utf-8") as f:
|
with open(sys.argv[1], encoding="utf-8") as f:
|
||||||
c = json.load(f)
|
c = json.load(f)
|
||||||
with sync_playwright() as p:
|
with sync_playwright() as p:
|
||||||
ctx = p.chromium.launch_persistent_context(
|
args, ignore_automation = persistent_context_launch_parts()
|
||||||
|
lc_kw = dict(
|
||||||
user_data_dir=c["profile_dir"],
|
user_data_dir=c["profile_dir"],
|
||||||
headless=False,
|
headless=False,
|
||||||
channel=c["channel"],
|
channel=c["channel"],
|
||||||
no_viewport=True,
|
no_viewport=True,
|
||||||
args=["--start-maximized"],
|
args=args,
|
||||||
)
|
)
|
||||||
|
if ignore_automation is not None:
|
||||||
|
lc_kw["ignore_default_args"] = ignore_automation
|
||||||
|
ctx = p.chromium.launch_persistent_context(**lc_kw)
|
||||||
|
if stealth_enabled():
|
||||||
|
ctx.add_init_script(STEALTH_INIT_SCRIPT)
|
||||||
try:
|
try:
|
||||||
page = ctx.pages[0] if ctx.pages else ctx.new_page()
|
page = ctx.pages[0] if ctx.pages else ctx.new_page()
|
||||||
page.goto(c["url"], wait_until="domcontentloaded", timeout=60000)
|
page.goto(c["url"], wait_until="domcontentloaded", timeout=60000)
|
||||||
|
|||||||
@@ -62,6 +62,12 @@ PLATFORMS = {
|
|||||||
"prefix": "元宝",
|
"prefix": "元宝",
|
||||||
"aliases": ["元宝"],
|
"aliases": ["元宝"],
|
||||||
},
|
},
|
||||||
|
"gemini": {
|
||||||
|
"url": "https://gemini.google.com",
|
||||||
|
"label": "Gemini",
|
||||||
|
"prefix": "Gemini",
|
||||||
|
"aliases": ["谷歌Gemini", "Google Gemini", "google gemini", "Bard"],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
# 未登录页共性:主行动点含「登录」——submit 按钮、Element 主按钮、Semi 文案、标题、通栏主按钮等,Playwright :has-text 可覆盖多数站点。
|
# 未登录页共性:主行动点含「登录」——submit 按钮、Element 主按钮、Semi 文案、标题、通栏主按钮等,Playwright :has-text 可覆盖多数站点。
|
||||||
|
|||||||
40
scripts/util/playwright_stealth.py
Normal file
40
scripts/util/playwright_stealth.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
"""
|
||||||
|
淡化 Playwright 启动时的自动化指纹,减轻 Google 账号页「此浏览器或应用可能不安全」类拦截。
|
||||||
|
|
||||||
|
关闭:环境变量 OPENCLAW_PLAYWRIGHT_STEALTH=0(或 false/off/no)。
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
STEALTH_INIT_SCRIPT = """
|
||||||
|
(() => {
|
||||||
|
try {
|
||||||
|
Object.defineProperty(navigator, "webdriver", { get: () => undefined });
|
||||||
|
} catch (e) {}
|
||||||
|
})();
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def stealth_enabled() -> bool:
|
||||||
|
v = (os.environ.get("OPENCLAW_PLAYWRIGHT_STEALTH") or "1").strip().lower()
|
||||||
|
return v not in ("0", "false", "no", "off")
|
||||||
|
|
||||||
|
|
||||||
|
def persistent_context_launch_parts(
|
||||||
|
*,
|
||||||
|
extra_args: list[str] | None = None,
|
||||||
|
) -> tuple[list[str], list[str] | None]:
|
||||||
|
"""
|
||||||
|
返回 (args, ignore_default_args)。
|
||||||
|
ignore_default_args 为 None 时不应传给 launch_persistent_context。
|
||||||
|
"""
|
||||||
|
args = ["--start-maximized"]
|
||||||
|
if extra_args:
|
||||||
|
args = args + list(extra_args)
|
||||||
|
if not stealth_enabled():
|
||||||
|
return args, None
|
||||||
|
if "--disable-blink-features=AutomationControlled" not in args:
|
||||||
|
args.append("--disable-blink-features=AutomationControlled")
|
||||||
|
return args, ["--enable-automation"]
|
||||||
Reference in New Issue
Block a user