Skip to content
edgestoragePublic

About

Codex skill: browser automation via Playwright + raw CDP on the local Chromium. Node CLI with reusable site scripts.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

bcap

一个 Codex skill:通过 Playwright 和原生 CDP 操作本机 Chromium 的浏览器自动化工具,全部用 Node 实现。

  • 自包含:SKILL.md(skill 定义)+ scripts/bcap.mjs(CLI)+ sites/(按站点沉淀的可复用脚本)
  • 自动判断环境:TaskHandoff 实例内驱动受管 Chromium(自动发现 CDP 端点,通常为 http://127.0.0.1:9201);其他环境复用已开调试端口的 Chromium,或直接用本地 Chrome/Chromium 启动 --remote-debugging-port。也可用 --cdp 指向任意已开调试端口的 Chromium
  • 脚本复用:脚本文件写一次,之后用 --input 反复执行;scripts 子命令按域名/匹配 URL 建立脚本索引
  • 执行控制:--timeout 超时、--evidence 执行证据(事件 + 可见元素 DOM diff)、wait 等待条件、history 执行历史
  • 真实输入:click / type / press 走 Playwright 真实事件(isTrusted=true),脚本内可用 cap.click / cap.type / cap.press
  • 省 token:输出默认单行紧凑 JSON(--pretty 美化);evidence 每条列表默认最多 100 条(counts/omitted 始终精确,--evidence-limit all 可全量),DOM 条目自动省略空字段

安装

作为 Codex skill 安装:

bash install.sh                     # 拷贝到 ~/.codex/skills/bcap
bash install.sh --symlink           # 或软链到本目录(会替换已有目标)

作为 OpenCode skill 安装(OpenCode 原生支持 Agent Skills,读取 ~/.config/opencode/skills/<name>/SKILL.md):

bash install.sh --target opencode   # 拷贝到 ~/.config/opencode/skills/bcap
bash install.sh --target all        # Codex 和 OpenCode 同时安装

OpenCode 也会自动读取 ~/.claude/skills/<name>/SKILL.md 与 ~/.agents/skills/<name>/SKILL.md 兼容路径;这里安装到它的原生位置 ~/.config/opencode/skills。 两者都读取 SKILL.md 的 name / description frontmatter,bcap 的 frontmatter 同时满足 Codex 与 OpenCode 的规范。

或直接克隆到 skills 目录(以 Codex 为例):

git clone <repo-url> ~/.codex/skills/bcap
cd ~/.codex/skills/bcap && npm install

依赖

  • Node.js 18+(需要 fetch / AbortSignal.timeout)
  • playwright-core(npm install 安装,不下载浏览器)

运行环境与浏览器生命周期

bcap 自己判断运行环境(status 输出 environment.mode 与判断依据 reason):

  • TaskHandoff 实例:检测到 TASK_HANDOFF_* 环境变量,或 http://127.0.0.1:8080 上的控制面可用。此时浏览器是实例的受管 app(appId=chromium),不需要自己裸起 Chromium。
  • 其他环境(普通本机、其他 agent 运行时):复用已开调试端口的 Chromium,或由 bcap 用本地 Chrome/Chromium 启动 --remote-debugging-port。

默认 auto,可用 --env taskhandoff|local 或 BCAP_ENV 强制指定。

node scripts/bcap.mjs status   # 环境、CDP 端点、浏览器版本、标签页、本地浏览器进程;未启动时返回 running:false
node scripts/bcap.mjs launch   # 幂等:可达就复用,没有才启动并等待 CDP 就绪

完整的检测顺序、会话生命周期和故障恢复见 references/environments.md。

任务实例模式:

  • 等价的手工调用:curl -X POST http://127.0.0.1:8080/api/apps/sessions -H 'Content-Type: application/json' -d '{"appId":"chromium"}'
  • 会话同时提供 CDP(默认 http://127.0.0.1:9201)和实例 Web UI 中的 KasmVNC 可视窗口,用户可实时看到并操作同一个浏览器
  • 支持同时运行多个浏览器会话:status 列出全部(默认目标带 default 标记),用 --session <id> 指定目标,launch --new-session 再开一个,stop <id> 停止指定会话
  • API 地址默认 http://127.0.0.1:8080,用 --api / BCAP_API 覆盖
  • 停止会话:实例 UI 或 POST /api/apps/sessions/<id>/stop

其他环境(本地浏览器):

  • launch 先复用可达的 CDP 端点(--cdp / BCAP_CDP,或默认端口 9201 / 9222),都没有才启动本地浏览器
  • 启动模式:默认有界面(非 headless,用户能看到并接管同一个窗口)。只要任务不需要用户碰浏览器,就优先用 --headless 或 BCAP_HEADLESS=1;自动登录、自动化测试、数据提取和基于截图的视觉检查都属于无人值守流程。只有用户必须亲自交互、观察或接管浏览器(例如手动输入凭据/MFA、确认、支付,或明确要求观看)时才保持有界面,不能用 headless(可用 BCAP_HEADLESS=0 覆盖继承的环境变量)。launch 会复用已有浏览器;需要把已有 headless 本地会话切换为有界面时,先停止该会话再重新 launch。另有 --executable <path>(或 BCAP_BROWSER)、--user-data-dir <dir>(默认 <skill>/.state/browser-profile,登录态可跨次复用)、--port <n>(起始端口,默认 9222,或 BCAP_PORT)、--new-session 再开一个而不是复用
  • 可执行文件查找顺序:--executable / BCAP_BROWSER → Chrome/Chromium/Edge/Brave/Vivaldi(macOS /Applications、Windows Program Files、Linux PATH)→ Playwright 自带 Chromium;都找不到时直接报错并提示设置路径
  • 启动的浏览器记录在 <skill>/.state/local-browser.json(gitignore),status 的 localBrowsers 会列出;stop <id|pid|port> 或 stop all 结束。进程已死或 CDP 不可达的记录自动清理,不会被复用

使用

node scripts/bcap.mjs status     # 环境、端点、浏览器版本、标签页;未启动时返回 running:false
node scripts/bcap.mjs launch     # 确保浏览器在运行(受管 Chromium 或本地 Chrome)
node scripts/bcap.mjs launch --headless --executable '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
node scripts/bcap.mjs run sites/examples/read-page-summary.js --new https://example.com --input '{"linkLimit":5}'
node scripts/bcap.mjs scripts --match https://example.com/    # 脚本索引(--domain 过滤域名)
node scripts/bcap.mjs click 'input[name=q]'                     # 真实点击
node scripts/bcap.mjs type 'input[name=q]' 'hello' --delay 40   # 真实键盘输入
node scripts/bcap.mjs press Enter --selector 'input[name=q]'    # 真实按键
node scripts/bcap.mjs eval "document.title"                     # 单表达式读取
node scripts/bcap.mjs exec --script "return { title: document.title }" --evidence all
node scripts/bcap.mjs wait --until-selector '#ready' --timeout 60000
node scripts/bcap.mjs history --limit 10
node scripts/bcap.mjs cdp Browser.getVersion

完整命令与工作流见 SKILL.md;环境生命周期见 references/environments.md,脚本约定见 references/script-authoring.md,标签页与状态变更安全规则见 references/safety.md。

目录

├── SKILL.md                    # skill 入口(name/description + 工作流)
├── references/                 # 环境生命周期、脚本编写指南、操作安全
├── scripts/bcap.mjs            # CLI 入口(status/launch/stop/scripts/run/exec/eval/click/type/press/wait/history/nav/new/close/shot/text/cdp)
├── sites/                      # 可复用站点脚本库(sites/<domain>/<capability-name>.js)
│   └── examples/               # 示例脚本 + 域名 README 示例
├── agents/openai.yaml          # skill UI 元数据
├── install.sh                  # 安装到 Codex(~/.codex/skills/bcap)或 OpenCode(~/.config/opencode/skills/bcap)
├── .state/                     # 运行时状态(history.jsonl、local-browser.json、本地浏览器 profile;已 gitignore)
└── package.json

About

Codex skill: browser automation via Playwright + raw CDP on the local Chromium. Node CLI with reusable site scripts.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages