@jackwener/opencli 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/.github/workflows/build-extension.yml +62 -0
  2. package/.github/workflows/ci.yml +6 -6
  3. package/.github/workflows/e2e-headed.yml +2 -2
  4. package/.github/workflows/pkg-pr-new.yml +2 -2
  5. package/.github/workflows/release.yml +2 -5
  6. package/.github/workflows/security.yml +2 -2
  7. package/CDP.md +1 -1
  8. package/CDP.zh-CN.md +1 -1
  9. package/README.md +15 -7
  10. package/README.zh-CN.md +15 -7
  11. package/SKILL.md +3 -5
  12. package/dist/browser/cdp.d.ts +27 -0
  13. package/dist/browser/cdp.js +295 -0
  14. package/dist/browser/index.d.ts +3 -0
  15. package/dist/browser/index.js +4 -0
  16. package/dist/browser/page.js +2 -23
  17. package/dist/browser/utils.d.ts +10 -0
  18. package/dist/browser/utils.js +27 -0
  19. package/dist/browser.test.js +42 -1
  20. package/dist/chaoxing.d.ts +58 -0
  21. package/dist/chaoxing.js +225 -0
  22. package/dist/chaoxing.test.d.ts +1 -0
  23. package/dist/chaoxing.test.js +38 -0
  24. package/dist/cli-manifest.json +203 -0
  25. package/dist/cli.d.ts +1 -0
  26. package/dist/cli.js +197 -0
  27. package/dist/clis/boss/chatlist.d.ts +1 -0
  28. package/dist/clis/boss/chatlist.js +50 -0
  29. package/dist/clis/boss/chatmsg.d.ts +1 -0
  30. package/dist/clis/boss/chatmsg.js +73 -0
  31. package/dist/clis/boss/send.d.ts +1 -0
  32. package/dist/clis/boss/send.js +176 -0
  33. package/dist/clis/chaoxing/assignments.d.ts +1 -0
  34. package/dist/clis/chaoxing/assignments.js +74 -0
  35. package/dist/clis/chaoxing/exams.d.ts +1 -0
  36. package/dist/clis/chaoxing/exams.js +74 -0
  37. package/dist/clis/chatgpt/ask.js +15 -14
  38. package/dist/clis/chatgpt/ax.d.ts +1 -0
  39. package/dist/clis/chatgpt/ax.js +78 -0
  40. package/dist/clis/chatgpt/read.js +5 -6
  41. package/dist/clis/twitter/post.js +9 -2
  42. package/dist/clis/twitter/search.js +14 -33
  43. package/dist/clis/xiaohongshu/download.d.ts +1 -1
  44. package/dist/clis/xiaohongshu/download.js +1 -1
  45. package/dist/engine.js +24 -13
  46. package/dist/explore.js +46 -101
  47. package/dist/main.js +4 -193
  48. package/dist/output.d.ts +1 -1
  49. package/dist/registry.d.ts +3 -3
  50. package/dist/scripts/framework.d.ts +4 -0
  51. package/dist/scripts/framework.js +21 -0
  52. package/dist/scripts/interact.d.ts +4 -0
  53. package/dist/scripts/interact.js +20 -0
  54. package/dist/scripts/store.d.ts +9 -0
  55. package/dist/scripts/store.js +44 -0
  56. package/dist/synthesize.js +1 -1
  57. package/extension/dist/background.js +338 -430
  58. package/extension/manifest.json +2 -2
  59. package/extension/src/background.ts +2 -2
  60. package/package.json +1 -1
  61. package/src/browser/cdp.ts +295 -0
  62. package/src/browser/index.ts +4 -0
  63. package/src/browser/page.ts +2 -24
  64. package/src/browser/utils.ts +27 -0
  65. package/src/browser.test.ts +46 -0
  66. package/src/chaoxing.test.ts +45 -0
  67. package/src/chaoxing.ts +268 -0
  68. package/src/cli.ts +185 -0
  69. package/src/clis/antigravity/SKILL.md +5 -0
  70. package/src/clis/boss/chatlist.ts +50 -0
  71. package/src/clis/boss/chatmsg.ts +70 -0
  72. package/src/clis/boss/send.ts +193 -0
  73. package/src/clis/chaoxing/README.md +36 -0
  74. package/src/clis/chaoxing/README.zh-CN.md +35 -0
  75. package/src/clis/chaoxing/assignments.ts +88 -0
  76. package/src/clis/chaoxing/exams.ts +88 -0
  77. package/src/clis/chatgpt/ask.ts +14 -15
  78. package/src/clis/chatgpt/ax.ts +81 -0
  79. package/src/clis/chatgpt/read.ts +5 -7
  80. package/src/clis/twitter/post.ts +9 -2
  81. package/src/clis/twitter/search.ts +15 -33
  82. package/src/clis/xiaohongshu/download.ts +1 -1
  83. package/src/engine.ts +20 -13
  84. package/src/explore.ts +51 -100
  85. package/src/main.ts +4 -180
  86. package/src/output.ts +12 -12
  87. package/src/registry.ts +3 -3
  88. package/src/scripts/framework.ts +20 -0
  89. package/src/scripts/interact.ts +22 -0
  90. package/src/scripts/store.ts +40 -0
  91. package/src/synthesize.ts +1 -1
@@ -0,0 +1,62 @@
1
+ name: Build Chrome Extension
2
+
3
+ on:
4
+ push:
5
+ branches: [ "main" ]
6
+ tags: [ "v*.*.*" ]
7
+ pull_request:
8
+ branches: [ "main" ]
9
+
10
+ permissions:
11
+ contents: write
12
+
13
+ jobs:
14
+ build:
15
+ runs-on: ubuntu-latest
16
+ steps:
17
+ - name: Checkout Code
18
+ uses: actions/checkout@v4
19
+
20
+ - name: Setup Node.js
21
+ uses: actions/setup-node@v4
22
+ with:
23
+ node-version: 20
24
+
25
+ - name: Create Extension ZIP
26
+ run: |
27
+ cd extension
28
+ zip -r ../opencli-extension.zip .
29
+
30
+ - name: Create Extension CRX
31
+ run: |
32
+ npm install -g crx3
33
+ if [ -n "${{ secrets.CRX_PRIVATE_KEY }}" ]; then
34
+ echo "Found CRX_PRIVATE_KEY, signing extension..."
35
+ echo "${{ secrets.CRX_PRIVATE_KEY }}" > crx-key.pem
36
+ crx3 pack extension -o opencli-extension.crx -p crx-key.pem
37
+ rm crx-key.pem
38
+ else
39
+ echo "No CRX_PRIVATE_KEY configured. Generating CRX with a temporary random key..."
40
+ crx3 pack extension -o opencli-extension.crx
41
+ fi
42
+
43
+ - name: Upload Artifacts (Action Run)
44
+ uses: actions/upload-artifact@v4
45
+ with:
46
+ name: opencli-extension-build
47
+ path: |
48
+ opencli-extension.zip
49
+ opencli-extension.crx
50
+ retention-days: 7
51
+
52
+ - name: Attach to GitHub Release
53
+ if: startsWith(github.ref, 'refs/tags/')
54
+ uses: softprops/action-gh-release@v2
55
+ with:
56
+ files: |
57
+ opencli-extension.zip
58
+ opencli-extension.crx
59
+ draft: false
60
+ prerelease: false
61
+ env:
62
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -18,9 +18,9 @@ jobs:
18
18
  build:
19
19
  runs-on: ubuntu-latest
20
20
  steps:
21
- - uses: actions/checkout@v6
21
+ - uses: actions/checkout@v4
22
22
 
23
- - uses: actions/setup-node@v6
23
+ - uses: actions/setup-node@v4
24
24
  with:
25
25
  node-version: '22'
26
26
  cache: 'npm'
@@ -43,9 +43,9 @@ jobs:
43
43
  node-version: ['20', '22']
44
44
  shard: [1, 2]
45
45
  steps:
46
- - uses: actions/checkout@v6
46
+ - uses: actions/checkout@v4
47
47
 
48
- - uses: actions/setup-node@v6
48
+ - uses: actions/setup-node@v4
49
49
  with:
50
50
  node-version: ${{ matrix.node-version }}
51
51
  cache: 'npm'
@@ -62,9 +62,9 @@ jobs:
62
62
  needs: build
63
63
  runs-on: ubuntu-latest
64
64
  steps:
65
- - uses: actions/checkout@v6
65
+ - uses: actions/checkout@v4
66
66
 
67
- - uses: actions/setup-node@v6
67
+ - uses: actions/setup-node@v4
68
68
  with:
69
69
  node-version: '22'
70
70
  cache: 'npm'
@@ -16,9 +16,9 @@ jobs:
16
16
  runs-on: ubuntu-latest
17
17
  timeout-minutes: 20
18
18
  steps:
19
- - uses: actions/checkout@v6
19
+ - uses: actions/checkout@v4
20
20
 
21
- - uses: actions/setup-node@v6
21
+ - uses: actions/setup-node@v4
22
22
  with:
23
23
  node-version: '22'
24
24
  cache: 'npm'
@@ -13,9 +13,9 @@ jobs:
13
13
  if: ${{ vars.PKG_PR_NEW_ENABLED == 'true' }}
14
14
  runs-on: ubuntu-latest
15
15
  steps:
16
- - uses: actions/checkout@v6
16
+ - uses: actions/checkout@v4
17
17
 
18
- - uses: actions/setup-node@v6
18
+ - uses: actions/setup-node@v4
19
19
  with:
20
20
  node-version: '22'
21
21
  cache: 'npm'
@@ -13,9 +13,9 @@ jobs:
13
13
  release:
14
14
  runs-on: ubuntu-latest
15
15
  steps:
16
- - uses: actions/checkout@v6
16
+ - uses: actions/checkout@v4
17
17
 
18
- - uses: actions/setup-node@v6
18
+ - uses: actions/setup-node@v4
19
19
  with:
20
20
  node-version: '22'
21
21
  registry-url: 'https://registry.npmjs.org'
@@ -26,9 +26,6 @@ jobs:
26
26
  - name: Type check
27
27
  run: npx tsc --noEmit
28
28
 
29
- - name: Build
30
- run: npm run build
31
-
32
29
  - name: Create GitHub Release
33
30
  uses: softprops/action-gh-release@v2
34
31
  with:
@@ -19,9 +19,9 @@ jobs:
19
19
  audit:
20
20
  runs-on: ubuntu-latest
21
21
  steps:
22
- - uses: actions/checkout@v6
22
+ - uses: actions/checkout@v4
23
23
 
24
- - uses: actions/setup-node@v6
24
+ - uses: actions/setup-node@v4
25
25
  with:
26
26
  node-version: '22'
27
27
  cache: 'npm'
package/CDP.md CHANGED
@@ -98,6 +98,6 @@ opencli doctor # Verify connection
98
98
  opencli bilibili hot --limit 5 # Test a command
99
99
  ```
100
100
 
101
- > *Tip: OpenCLI automatically requests the `/json/version` HTTP endpoint to discover the underlying WebSocket URL if you provide a standard HTTP/HTTPS address.*
101
+ > *Tip: If you provide a standard HTTP/HTTPS CDP endpoint, OpenCLI requests the `/json` target list and picks the most likely inspectable app/page target automatically. If multiple app targets exist, you can further narrow selection with `OPENCLI_CDP_TARGET` (for example `antigravity` or `codex`).*
102
102
 
103
103
  If you plan to use this setup frequently, you can persist the environment variable by adding the `export` line to your `~/.bashrc` or `~/.zshrc` on the server.
package/CDP.zh-CN.md CHANGED
@@ -98,6 +98,6 @@ opencli doctor # 查看并验证连接是否通畅
98
98
  opencli bilibili hot --limit 5 # 执行目标命令
99
99
  ```
100
100
 
101
- > *Tip: 如果你填写的是一个普通 HTTP/HTTPS 的 URL 地址,OpenCLI 会自动尝试抓取该地址下的 `/json/version` 节点,来动态解析并连接真正底层依赖的 WebSocket 地址。*
101
+ > *Tip: 如果你填写的是一个普通 HTTP/HTTPS 的 CDP 地址,OpenCLI 会自动请求 `/json` target 列表,并挑选最可能的 app/page target;如果同一个端口下暴露了多个应用 target,还可以通过 `OPENCLI_CDP_TARGET`(例如 `antigravity`、`codex`)进一步缩小匹配范围。*
102
102
 
103
103
  如果你想在此服务器上永久启用该配置,可以将对应的 `export` 语句追加进入你的 `~/.bashrc` 或 `~/.zshrc` 配置文件中。
package/README.md CHANGED
@@ -39,7 +39,7 @@ Turn ANY Electron application into a CLI tool! Recombine, script, and extend app
39
39
  - **CLI All Electron** — CLI-ify apps like Antigravity Ultra! Now AI can control itself natively using cc/openclaw!
40
40
  - **Account-safe** — Reuses Chrome's logged-in state; your credentials never leave the browser.
41
41
  - **AI Agent ready** — `explore` discovers APIs, `synthesize` generates adapters, `cascade` finds auth strategies.
42
- - **Self-healing setup** — `opencli setup` auto-discovers tokens; `opencli doctor` diagnoses config across 10+ tools; `--fix` repairs them all.
42
+ - **Self-healing setup** — `opencli setup` verifies Browser Bridge connectivity; `opencli doctor` diagnoses daemon, extension, and live browser connectivity.
43
43
  - **Dynamic Loader** — Simply drop `.ts` or `.yaml` adapters into the `clis/` folder for auto-registration.
44
44
  - **Dual-Engine Architecture** — Supports both YAML declarative data pipelines and robust browser runtime TypeScript injections.
45
45
 
@@ -54,10 +54,18 @@ OpenCLI connects to your browser through a lightweight **Browser Bridge** Chrome
54
54
 
55
55
  ### Browser Bridge Extension Setup
56
56
 
57
- 1. Install the **opencli Browser Bridge** extension in Chrome:
58
- - Open `chrome://extensions`, enable **Developer mode** (top-right toggle)
59
- - Click **Load unpacked**, select the `extension/` folder from this repo
60
- 2. That's it! The daemon auto-starts when you run any browser command. No tokens, no manual configuration.
57
+ You can install the extension via either method:
58
+
59
+ **Method 1: Download Pre-built Release (Recommended)**
60
+ 1. Go to the GitHub [Releases page](https://github.com/jackwener/opencli/releases) and download the latest `opencli-extension.zip` or `opencli-extension.crx`.
61
+ 2. Open `chrome://extensions` and enable **Developer mode** (top-right toggle).
62
+ 3. Drag and drop the `.crx` file or the unzipped folder into the extensions page.
63
+
64
+ **Method 2: Load Unpacked Source (For Developers)**
65
+ 1. Open `chrome://extensions` and enable **Developer mode**.
66
+ 2. Click **Load unpacked** and select the `extension/` directory from this repository.
67
+
68
+ That's it! The daemon auto-starts when you run any browser command. No tokens, no manual configuration.
61
69
 
62
70
  > **Tip**: Use `opencli doctor` for ongoing diagnosis:
63
71
  > ```bash
@@ -117,7 +125,7 @@ Run `opencli list` for the live registry.
117
125
  | **discord-app** | `status` `send` `read` `channels` `servers` `search` `members` | 🖥️ Desktop |
118
126
  | **v2ex** | `hot` `latest` `topic` `daily` `me` `notifications` | 🌐 / 🔐 |
119
127
  | **xueqiu** | `feed` `hot-stock` `hot` `search` `stock` `watchlist` | 🔐 Browser |
120
- | **antigravity** | `status` `send` `read` `new` `evaluate` | 🖥️ Desktop |
128
+ | **antigravity** | `status` `send` `read` `new` `dump` `extract-code` `model` `watch` | 🖥️ Desktop |
121
129
  | **chatgpt** | `status` `new` `send` `read` `ask` | 🖥️ Desktop |
122
130
  | **xiaohongshu** | `search` `notifications` `feed` `me` `user` `download` | 🔐 Browser |
123
131
  | **apple-podcasts** | `search` `episodes` `top` | 🌐 Public |
@@ -181,7 +189,7 @@ brew install yt-dlp
181
189
 
182
190
  ```bash
183
191
  # Download images/videos from Xiaohongshu note
184
- opencli xiaohongshu download --note-id abc123 --output ./xhs
192
+ opencli xiaohongshu download --note_id abc123 --output ./xhs
185
193
 
186
194
  # Download Bilibili video (requires yt-dlp)
187
195
  opencli bilibili download --bvid BV1xxx --output ./bilibili
package/README.zh-CN.md CHANGED
@@ -40,7 +40,7 @@ CLI all electron!现在支持把所有 electron 应用 CLI 化,从而组合
40
40
  - **CLI All Electron** — 支持把所有 electron 应用(如 Antigravity Ultra)CLI 化,让 AI 控制自己!
41
41
  - **多站点覆盖** — 覆盖 B站、知乎、小红书、Twitter、Reddit,以及多种桌面应用
42
42
  - **零风控** — 复用 Chrome 登录态,无需存储任何凭证
43
- - **自修复配置** — `opencli setup` 自动发现 Token;`opencli doctor` 诊断 10+ 工具配置;`--fix` 一键修复
43
+ - **自修复配置** — `opencli setup` 检查 Browser Bridge 连通性;`opencli doctor` 诊断 daemon、扩展和浏览器连接状态
44
44
  - **AI 原生** — `explore` 自动发现 API,`synthesize` 生成适配器,`cascade` 探测认证策略
45
45
  - **动态加载引擎** — 声明式的 `.yaml` 或者底层定制的 `.ts` 适配器,放入 `clis/` 文件夹即可自动注册生效
46
46
 
@@ -55,10 +55,18 @@ OpenCLI 通过轻量化的 **Browser Bridge** Chrome 扩展 + 微型 daemon 与
55
55
 
56
56
  ### Browser Bridge 扩展配置
57
57
 
58
- 1. 在 Chrome 中安装 **opencli Browser Bridge** 扩展:
59
- - 打开 `chrome://extensions`,启用右上角的 **开发者模式**
60
- - 点击 **加载已解压的扩展程序**,选择本仓库的 `extension/` 文件夹
61
- 2. 完成!运行任何浏览器命令时 daemon 会自动启动。无需 token,无需手动配置。
58
+ 你可以选择以下任一方式安装扩展:
59
+
60
+ **方式一:下载构建好的安装包(推荐)**
61
+ 1. GitHub [Releases 页面](https://github.com/jackwener/opencli/releases) 下载最新的 `opencli-extension.zip` 或 `opencli-extension.crx`。
62
+ 2. 打开 Chrome 的 `chrome://extensions`,启用右上角的 **开发者模式**。
63
+ 3. 将 `.crx` 拖入浏览器窗口,或将解压后的文件夹拖入即可完成安装。
64
+
65
+ **方式二:加载源码(针对开发者)**
66
+ 1. 同样在 `chrome://extensions` 开启 **开发者模式**。
67
+ 2. 点击 **加载已解压的扩展程序**,选择本仓库代码树中的 `extension/` 文件夹。
68
+
69
+ 完成!运行任何 opencli 浏览器命令时,后台微型 daemon 会自动启动与浏览器通信。无需配 API Token,零代码配置。
62
70
 
63
71
  > **Tip**:后续诊断用 `opencli doctor`:
64
72
  > ```bash
@@ -118,7 +126,7 @@ npm install -g @jackwener/opencli@latest
118
126
  | **discord-app** | `status` `send` `read` `channels` `servers` `search` `members` | 🖥️ 桌面端 |
119
127
  | **v2ex** | `hot` `latest` `topic` `daily` `me` `notifications` | 🌐 / 🔐 |
120
128
  | **xueqiu** | `feed` `hot-stock` `hot` `search` `stock` `watchlist` | 🔐 浏览器 |
121
- | **antigravity** | `status` `send` `read` `new` `evaluate` | 🖥️ 桌面端 |
129
+ | **antigravity** | `status` `send` `read` `new` `dump` `extract-code` `model` `watch` | 🖥️ 桌面端 |
122
130
  | **chatgpt** | `status` `new` `send` `read` `ask` | 🖥️ 桌面端 |
123
131
  | **xiaohongshu** | `search` `notifications` `feed` `me` `user` `download` | 🔐 浏览器 |
124
132
  | **apple-podcasts** | `search` `episodes` `top` | 🌐 公开 |
@@ -182,7 +190,7 @@ brew install yt-dlp
182
190
 
183
191
  ```bash
184
192
  # 下载小红书笔记中的图片/视频
185
- opencli xiaohongshu download --note-id abc123 --output ./xhs
193
+ opencli xiaohongshu download --note_id abc123 --output ./xhs
186
194
 
187
195
  # 下载B站视频(需要 yt-dlp)
188
196
  opencli bilibili download --bvid BV1xxx --output ./bilibili
package/SKILL.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: opencli
3
3
  description: "OpenCLI — Make any website or Electron App your CLI. Zero risk, AI-powered, reuse Chrome login. 80+ commands across 19 sites."
4
- version: 0.7.3
4
+ version: 0.7.4
5
5
  author: jackwener
6
6
  tags: [cli, browser, web, chrome-extension, cdp, bilibili, zhihu, twitter, github, v2ex, hackernews, reddit, xiaohongshu, xueqiu, youtube, boss, coupang, AI, agent]
7
7
  ---
@@ -170,11 +170,9 @@ opencli list --json # JSON output
170
170
  opencli list -f yaml # YAML output
171
171
  opencli validate # Validate all CLI definitions
172
172
  opencli validate bilibili # Validate specific site
173
- opencli setup # Interactive token setup (auto-discover + TUI checkbox)
174
- opencli doctor # Diagnose token & extension config across all tools
173
+ opencli setup # Interactive Browser Bridge setup and connectivity check
174
+ opencli doctor # Diagnose daemon, extension, and browser connectivity
175
175
  opencli doctor --live # Also test live browser connectivity
176
- opencli doctor --fix # Fix mismatched configs (interactive confirmation)
177
- opencli doctor --fix -y # Fix all configs non-interactively
178
176
  ```
179
177
 
180
178
  ### AI Agent Workflow
@@ -0,0 +1,27 @@
1
+ /**
2
+ * CDP client — implements IPage by connecting directly to a Chrome/Electron CDP WebSocket.
3
+ */
4
+ import type { IPage } from '../types.js';
5
+ export interface CDPTarget {
6
+ type?: string;
7
+ url?: string;
8
+ title?: string;
9
+ webSocketDebuggerUrl?: string;
10
+ }
11
+ export declare class CDPBridge {
12
+ private _ws;
13
+ private _idCounter;
14
+ private _pending;
15
+ connect(opts?: {
16
+ timeout?: number;
17
+ }): Promise<IPage>;
18
+ close(): Promise<void>;
19
+ send(method: string, params?: any): Promise<any>;
20
+ }
21
+ declare function selectCDPTarget(targets: CDPTarget[]): CDPTarget | undefined;
22
+ declare function scoreCDPTarget(target: CDPTarget, preferredPattern?: RegExp): number;
23
+ export declare const __test__: {
24
+ selectCDPTarget: typeof selectCDPTarget;
25
+ scoreCDPTarget: typeof scoreCDPTarget;
26
+ };
27
+ export {};
@@ -0,0 +1,295 @@
1
+ /**
2
+ * CDP client — implements IPage by connecting directly to a Chrome/Electron CDP WebSocket.
3
+ */
4
+ import { WebSocket } from 'ws';
5
+ import { wrapForEval } from './utils.js';
6
+ export class CDPBridge {
7
+ _ws = null;
8
+ _idCounter = 0;
9
+ _pending = new Map();
10
+ async connect(opts) {
11
+ const endpoint = process.env.OPENCLI_CDP_ENDPOINT;
12
+ if (!endpoint)
13
+ throw new Error('OPENCLI_CDP_ENDPOINT is not set');
14
+ // If it's a direct ws:// URL, use it. Otherwise, fetch the /json endpoint to find a page.
15
+ let wsUrl = endpoint;
16
+ if (endpoint.startsWith('http')) {
17
+ const res = await fetch(`${endpoint.replace(/\/$/, '')}/json`);
18
+ if (!res.ok)
19
+ throw new Error(`Failed to fetch CDP targets: ${res.statusText}`);
20
+ const targets = await res.json();
21
+ const target = selectCDPTarget(targets);
22
+ if (!target || !target.webSocketDebuggerUrl) {
23
+ throw new Error('No inspectable targets found at CDP endpoint');
24
+ }
25
+ wsUrl = target.webSocketDebuggerUrl;
26
+ }
27
+ return new Promise((resolve, reject) => {
28
+ const ws = new WebSocket(wsUrl);
29
+ const timeout = setTimeout(() => reject(new Error('CDP connect timeout')), opts?.timeout ?? 10000);
30
+ ws.on('open', () => {
31
+ clearTimeout(timeout);
32
+ this._ws = ws;
33
+ resolve(new CDPPage(this));
34
+ });
35
+ ws.on('error', (err) => {
36
+ clearTimeout(timeout);
37
+ reject(err);
38
+ });
39
+ ws.on('message', (data) => {
40
+ try {
41
+ const msg = JSON.parse(data.toString());
42
+ if (msg.id && this._pending.has(msg.id)) {
43
+ const { resolve, reject } = this._pending.get(msg.id);
44
+ this._pending.delete(msg.id);
45
+ if (msg.error) {
46
+ reject(new Error(msg.error.message));
47
+ }
48
+ else {
49
+ resolve(msg.result);
50
+ }
51
+ }
52
+ }
53
+ catch (e) {
54
+ // ignore parsing errors
55
+ }
56
+ });
57
+ });
58
+ }
59
+ async close() {
60
+ if (this._ws) {
61
+ this._ws.close();
62
+ this._ws = null;
63
+ }
64
+ for (const p of this._pending.values()) {
65
+ p.reject(new Error('CDP connection closed'));
66
+ }
67
+ this._pending.clear();
68
+ }
69
+ async send(method, params = {}) {
70
+ if (!this._ws || this._ws.readyState !== WebSocket.OPEN) {
71
+ throw new Error('CDP connection is not open');
72
+ }
73
+ const id = ++this._idCounter;
74
+ return new Promise((resolve, reject) => {
75
+ this._pending.set(id, { resolve, reject });
76
+ this._ws.send(JSON.stringify({ id, method, params }));
77
+ });
78
+ }
79
+ }
80
+ class CDPPage {
81
+ bridge;
82
+ constructor(bridge) {
83
+ this.bridge = bridge;
84
+ }
85
+ async goto(url) {
86
+ await this.bridge.send('Page.navigate', { url });
87
+ await new Promise(r => setTimeout(r, 1000));
88
+ }
89
+ async evaluate(js) {
90
+ const expression = wrapForEval(js);
91
+ const result = await this.bridge.send('Runtime.evaluate', {
92
+ expression,
93
+ returnByValue: true,
94
+ awaitPromise: true
95
+ });
96
+ if (result.exceptionDetails) {
97
+ throw new Error('Evaluate error: ' + (result.exceptionDetails.exception?.description || 'Unknown exception'));
98
+ }
99
+ return result.result?.value;
100
+ }
101
+ async snapshot(opts) {
102
+ throw new Error('Method not implemented.');
103
+ }
104
+ async click(ref) {
105
+ const safeRef = JSON.stringify(ref);
106
+ const code = `
107
+ (() => {
108
+ const ref = ${safeRef};
109
+ const el = document.querySelector('[data-ref="' + ref + '"]')
110
+ || document.querySelectorAll('a, button, input, [role="button"], [tabindex]')[parseInt(ref, 10) || 0];
111
+ if (!el) throw new Error('Element not found: ' + ref);
112
+ el.scrollIntoView({ behavior: 'instant', block: 'center' });
113
+ el.click();
114
+ return 'clicked';
115
+ })()
116
+ `;
117
+ await this.evaluate(code);
118
+ }
119
+ async typeText(ref, text) {
120
+ const safeRef = JSON.stringify(ref);
121
+ const safeText = JSON.stringify(text);
122
+ const code = `
123
+ (() => {
124
+ const ref = ${safeRef};
125
+ const el = document.querySelector('[data-ref="' + ref + '"]')
126
+ || document.querySelectorAll('input, textarea, [contenteditable]')[parseInt(ref, 10) || 0];
127
+ if (!el) throw new Error('Element not found: ' + ref);
128
+ el.focus();
129
+ el.value = ${safeText};
130
+ el.dispatchEvent(new Event('input', { bubbles: true }));
131
+ el.dispatchEvent(new Event('change', { bubbles: true }));
132
+ return 'typed';
133
+ })()
134
+ `;
135
+ await this.evaluate(code);
136
+ }
137
+ async pressKey(key) {
138
+ const code = `
139
+ (() => {
140
+ const el = document.activeElement || document.body;
141
+ el.dispatchEvent(new KeyboardEvent('keydown', { key: ${JSON.stringify(key)}, bubbles: true }));
142
+ el.dispatchEvent(new KeyboardEvent('keyup', { key: ${JSON.stringify(key)}, bubbles: true }));
143
+ return 'pressed';
144
+ })()
145
+ `;
146
+ await this.evaluate(code);
147
+ }
148
+ async wait(options) {
149
+ if (typeof options === 'number') {
150
+ await new Promise(resolve => setTimeout(resolve, options * 1000));
151
+ return;
152
+ }
153
+ if (options.time) {
154
+ await new Promise(resolve => setTimeout(resolve, options.time * 1000));
155
+ return;
156
+ }
157
+ if (options.text) {
158
+ const timeout = (options.timeout ?? 30) * 1000;
159
+ const code = `
160
+ new Promise((resolve, reject) => {
161
+ const deadline = Date.now() + ${timeout};
162
+ const check = () => {
163
+ if (document.body.innerText.includes(${JSON.stringify(options.text)})) return resolve('found');
164
+ if (Date.now() > deadline) return reject(new Error('Text not found: ' + ${JSON.stringify(options.text)}));
165
+ setTimeout(check, 200);
166
+ };
167
+ check();
168
+ })
169
+ `;
170
+ await this.evaluate(code);
171
+ }
172
+ }
173
+ async tabs() {
174
+ throw new Error('Method not implemented.');
175
+ }
176
+ async closeTab(index) {
177
+ throw new Error('Method not implemented.');
178
+ }
179
+ async newTab() {
180
+ throw new Error('Method not implemented.');
181
+ }
182
+ async selectTab(index) {
183
+ throw new Error('Method not implemented.');
184
+ }
185
+ async networkRequests(includeStatic) {
186
+ throw new Error('Method not implemented.');
187
+ }
188
+ async consoleMessages(level) {
189
+ throw new Error('Method not implemented.');
190
+ }
191
+ async scroll(direction, amount) {
192
+ throw new Error('Method not implemented.');
193
+ }
194
+ async autoScroll(options) {
195
+ throw new Error('Method not implemented.');
196
+ }
197
+ async installInterceptor(pattern) {
198
+ throw new Error('Method not implemented.');
199
+ }
200
+ async getInterceptedRequests() {
201
+ throw new Error('Method not implemented.');
202
+ }
203
+ async screenshot(options) {
204
+ throw new Error('Method not implemented.');
205
+ }
206
+ }
207
+ function selectCDPTarget(targets) {
208
+ const preferredPattern = compilePreferredPattern(process.env.OPENCLI_CDP_TARGET);
209
+ const ranked = targets
210
+ .map((target, index) => ({ target, index, score: scoreCDPTarget(target, preferredPattern) }))
211
+ .filter(({ score }) => Number.isFinite(score))
212
+ .sort((a, b) => {
213
+ if (b.score !== a.score)
214
+ return b.score - a.score;
215
+ return a.index - b.index;
216
+ });
217
+ return ranked[0]?.target;
218
+ }
219
+ function scoreCDPTarget(target, preferredPattern) {
220
+ if (!target.webSocketDebuggerUrl)
221
+ return Number.NEGATIVE_INFINITY;
222
+ const type = (target.type ?? '').toLowerCase();
223
+ const url = (target.url ?? '').toLowerCase();
224
+ const title = (target.title ?? '').toLowerCase();
225
+ const haystack = `${title} ${url}`;
226
+ if (!haystack.trim() && !type)
227
+ return Number.NEGATIVE_INFINITY;
228
+ if (haystack.includes('devtools'))
229
+ return Number.NEGATIVE_INFINITY;
230
+ let score = 0;
231
+ if (preferredPattern && preferredPattern.test(haystack))
232
+ score += 1000;
233
+ if (type === 'app')
234
+ score += 120;
235
+ else if (type === 'webview')
236
+ score += 100;
237
+ else if (type === 'page')
238
+ score += 80;
239
+ else if (type === 'iframe')
240
+ score += 20;
241
+ if (url.startsWith('http://localhost') || url.startsWith('https://localhost'))
242
+ score += 90;
243
+ if (url.startsWith('file://'))
244
+ score += 60;
245
+ if (url.startsWith('http://127.0.0.1') || url.startsWith('https://127.0.0.1'))
246
+ score += 50;
247
+ if (url.startsWith('about:blank'))
248
+ score -= 120;
249
+ if (url === '' || url === 'about:blank')
250
+ score -= 40;
251
+ if (title && title !== 'devtools')
252
+ score += 25;
253
+ if (title.includes('antigravity'))
254
+ score += 120;
255
+ if (title.includes('codex'))
256
+ score += 120;
257
+ if (title.includes('cursor'))
258
+ score += 120;
259
+ if (title.includes('chatwise'))
260
+ score += 120;
261
+ if (title.includes('notion'))
262
+ score += 120;
263
+ if (title.includes('discord'))
264
+ score += 120;
265
+ if (title.includes('netease'))
266
+ score += 120;
267
+ if (url.includes('antigravity'))
268
+ score += 100;
269
+ if (url.includes('codex'))
270
+ score += 100;
271
+ if (url.includes('cursor'))
272
+ score += 100;
273
+ if (url.includes('chatwise'))
274
+ score += 100;
275
+ if (url.includes('notion'))
276
+ score += 100;
277
+ if (url.includes('discord'))
278
+ score += 100;
279
+ if (url.includes('netease'))
280
+ score += 100;
281
+ return score;
282
+ }
283
+ function compilePreferredPattern(raw) {
284
+ const value = raw?.trim();
285
+ if (!value)
286
+ return undefined;
287
+ return new RegExp(escapeRegExp(value.toLowerCase()));
288
+ }
289
+ function escapeRegExp(value) {
290
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
291
+ }
292
+ export const __test__ = {
293
+ selectCDPTarget,
294
+ scoreCDPTarget,
295
+ };
@@ -6,6 +6,7 @@
6
6
  */
7
7
  export { Page } from './page.js';
8
8
  export { BrowserBridge, BrowserBridge as PlaywrightMCP } from './mcp.js';
9
+ export { CDPBridge } from './cdp.js';
9
10
  export { isDaemonRunning } from './daemon-client.js';
10
11
  import { extractTabEntries, diffTabIndexes, appendLimited } from './tabs.js';
11
12
  import { withTimeoutMs } from '../runtime.js';
@@ -14,4 +15,6 @@ export declare const __test__: {
14
15
  diffTabIndexes: typeof diffTabIndexes;
15
16
  appendLimited: typeof appendLimited;
16
17
  withTimeoutMs: typeof withTimeoutMs;
18
+ selectCDPTarget: (targets: import("./cdp.js").CDPTarget[]) => import("./cdp.js").CDPTarget | undefined;
19
+ scoreCDPTarget: (target: import("./cdp.js").CDPTarget, preferredPattern?: RegExp) => number;
17
20
  };
@@ -6,12 +6,16 @@
6
6
  */
7
7
  export { Page } from './page.js';
8
8
  export { BrowserBridge, BrowserBridge as PlaywrightMCP } from './mcp.js';
9
+ export { CDPBridge } from './cdp.js';
9
10
  export { isDaemonRunning } from './daemon-client.js';
10
11
  import { extractTabEntries, diffTabIndexes, appendLimited } from './tabs.js';
12
+ import { __test__ as cdpTest } from './cdp.js';
11
13
  import { withTimeoutMs } from '../runtime.js';
12
14
  export const __test__ = {
13
15
  extractTabEntries,
14
16
  diffTabIndexes,
15
17
  appendLimited,
16
18
  withTimeoutMs,
19
+ selectCDPTarget: cdpTest.selectCDPTarget,
20
+ scoreCDPTarget: cdpTest.scoreCDPTarget,
17
21
  };