Skip to content

Commit 4e987ab

Browse files
committed
feat: add browser backend and profile management commands
1 parent 7700704 commit 4e987ab

22 files changed

Lines changed: 2439 additions & 55 deletions

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Turn ANY Electron application into a CLI tool! Recombine, script, and extend app
2323
- [Highlights](#highlights)
2424
- [Prerequisites](#prerequisites)
2525
- [Quick Start](#quick-start)
26+
- [Browser Backend Utilities](#browser-backend-utilities)
2627
- [Built-in Commands](#built-in-commands)
2728
- [Desktop App Adapters](#desktop-app-adapters)
2829
- [Download Support](#download-support)
@@ -112,6 +113,32 @@ opencli list # Now you can use it anywhere!
112113
npm install -g @jackwener/opencli@latest
113114
```
114115

116+
## Browser Backend Utilities
117+
118+
OpenCLI also ships an additive `browser` command group for managing extension/CDP workflows without changing any existing site commands.
119+
120+
```bash
121+
opencli browser list
122+
opencli browser launch --port 9222
123+
opencli browser launch --port 9222 --profile zhihu
124+
opencli browser launch --port 9222 --browser-arg=--window-size=1440,900
125+
opencli browser run --backend cdp --cdp-endpoint http://127.0.0.1:9222 -- zhihu search --keyword AI
126+
opencli browser stop --port 9222
127+
opencli browser profiles
128+
opencli browser profiles rm zhihu
129+
opencli browser profiles prune --temporary
130+
```
131+
132+
- `launch` uses a temporary browser profile by default.
133+
- Pass `--profile <name>` when you want to preserve login state or browser data.
134+
- Use `--browser-arg <arg>` to pass extra raw Chrome/Chromium launch flags.
135+
- `stop` only stops browser processes.
136+
- `profiles rm <name>` removes a named persistent profile.
137+
- `profiles prune --temporary` removes unused temporary profiles.
138+
- `doctor` works for both extension mode and direct CDP mode.
139+
140+
For remote or headless CDP setups, see [Connecting OpenCLI via CDP](./docs/advanced/cdp.md).
141+
115142
## Built-in Commands
116143

117144
Run `opencli list` for the live registry.

README.zh-CN.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CLI all electron!现在支持把所有 electron 应用 CLI 化,从而组合
2525
- [亮点](#亮点)
2626
- [前置要求](#前置要求)
2727
- [快速开始](#快速开始)
28+
- [浏览器后端工具](#浏览器后端工具)
2829
- [内置命令](#内置命令)
2930
- [桌面应用适配器](#桌面应用适配器)
3031
- [下载支持](#下载支持)
@@ -112,6 +113,32 @@ opencli list # 可以在任何地方使用了!
112113
npm install -g @jackwener/opencli@latest
113114
```
114115

116+
## 浏览器后端工具
117+
118+
OpenCLI 额外提供了一组独立的 `browser` 子命令,用来管理扩展模式和直连 CDP 模式,而不改动现有站点命令的调用方式。
119+
120+
```bash
121+
opencli browser list
122+
opencli browser launch --port 9222
123+
opencli browser launch --port 9222 --profile zhihu
124+
opencli browser launch --port 9222 --browser-arg=--window-size=1440,900
125+
opencli browser run --backend cdp --cdp-endpoint http://127.0.0.1:9222 -- zhihu search --keyword AI
126+
opencli browser stop --port 9222
127+
opencli browser profiles
128+
opencli browser profiles rm zhihu
129+
opencli browser profiles prune --temporary
130+
```
131+
132+
- `launch` 默认使用临时 profile。
133+
- 只有在你需要保留登录态或浏览器数据时,才传 `--profile <name>`
134+
- 需要透传额外 Chrome/Chromium 启动参数时,使用 `--browser-arg <arg>`
135+
- `stop` 只负责停止浏览器进程。
136+
- `profiles rm <name>` 用来删除命名的持久 profile。
137+
- `profiles prune --temporary` 用来清理未使用的临时 profile。
138+
- `doctor` 同时支持扩展模式和直连 CDP 模式排查。
139+
140+
如果你是在远程机或无头环境直连 CDP,参考 [Connecting OpenCLI via CDP](./docs/advanced/cdp.md)
141+
115142
## 内置命令
116143

117144
运行 `opencli list` 查看完整注册表。

docs/advanced/cdp.md

Lines changed: 147 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,198 @@
1-
# Connecting OpenCLI via CDP (Remote/Headless Servers)
1+
# Connecting OpenCLI via CDP
22

3-
If you cannot use the opencli Browser Bridge extension (e.g., in a remote headless server environment without a UI), OpenCLI provides an alternative: connecting directly to Chrome via **CDP (Chrome DevTools Protocol)**.
3+
If you cannot or do not want to use the opencli Browser Bridge extension, OpenCLI can also connect directly to a Chrome/Chromium debugging endpoint via **CDP (Chrome DevTools Protocol)**.
44

5-
Because CDP binds to `localhost` by default for security reasons, accessing it from a remote server requires an additional networking tunnel.
5+
OpenCLI now provides a dedicated `browser` command group for this workflow, so you do not have to manage everything manually through environment variables and raw Chrome commands.
66

7-
This guide is broken down into three phases:
8-
1. **Preparation**: Start Chrome with CDP enabled locally.
9-
2. **Network Tunnels**: Expose that CDP port to your remote server using either **SSH Tunnels** or **Reverse Proxies**.
10-
3. **Execution**: Run OpenCLI on your server.
7+
This guide covers two common modes:
8+
9+
1. **Local CDP browser managed by OpenCLI**
10+
2. **Remote or headless CDP endpoint managed outside OpenCLI**
1111

1212
---
1313

14-
## Phase 1: Preparation (Local Machine)
14+
## Local CDP Workflow with `opencli browser`
1515

16-
First, you need to start a Chrome browser on your local machine with remote debugging enabled.
16+
For most local workflows, start with the built-in browser commands:
1717

18-
**macOS:**
1918
```bash
20-
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
21-
--remote-debugging-port=9222 \
22-
--user-data-dir="$HOME/chrome-debug-profile" \
23-
--remote-allow-origins="*"
19+
opencli browser launch --port 9222
20+
opencli browser list
21+
opencli browser doctor --backend cdp --cdp-endpoint http://127.0.0.1:9222 --live
22+
opencli browser run --backend cdp --cdp-endpoint http://127.0.0.1:9222 -- zhihu search --keyword AI
23+
opencli browser stop --port 9222
2424
```
2525

26-
**Linux:**
26+
### Temporary vs Persistent Profiles
27+
28+
By default, `opencli browser launch` creates a **temporary profile**:
29+
2730
```bash
28-
google-chrome \
29-
--remote-debugging-port=9222 \
30-
--user-data-dir="$HOME/chrome-debug-profile" \
31-
--remote-allow-origins="*"
31+
opencli browser launch --port 9222
3232
```
3333

34-
**Windows:**
35-
```cmd
36-
"C:\Program Files\Google\Chrome\Application\chrome.exe" ^
37-
--remote-debugging-port=9222 ^
38-
--user-data-dir="%USERPROFILE%\chrome-debug-profile" ^
39-
--remote-allow-origins="*"
34+
If you want to preserve login state or browser data, use a named persistent profile:
35+
36+
```bash
37+
opencli browser launch --port 9222 --profile zhihu
38+
opencli browser profiles
4039
```
4140

42-
> **Note**: The `--remote-allow-origins="*"` flag is often required for modern Chrome versions to accept cross-origin CDP WebSocket connections (e.g. from reverse proxies like ngrok).
41+
You can later reuse the same profile on a different port:
4342

44-
Once this browser instance opens, **log into the target websites you want to use** (e.g., bilibili.com, zhihu.com) so that the session contains the correct cookies.
43+
```bash
44+
opencli browser stop --port 9222
45+
opencli browser launch --port 9339 --profile zhihu
46+
```
4547

46-
---
48+
### Managing Profiles
4749

48-
## Phase 2: Remote Access Methods
50+
List persistent and temporary profiles:
4951

50-
Once CDP is running locally on port `9222`, you must securely expose this port to your remote server. Choose one of the two methods below depending on your network conditions.
52+
```bash
53+
opencli browser profiles
54+
```
5155

52-
### Method A: SSH Tunnel (Recommended)
56+
Remove a named persistent profile:
5357

54-
If your local machine has SSH access to the remote server, this is the most secure and straightforward method.
58+
```bash
59+
opencli browser profiles rm zhihu
60+
```
5561

56-
Run this command on your **Local Machine** to forward the remote server's port `9222` back to your local port `9222`:
62+
Remove unused temporary profiles:
5763

5864
```bash
59-
ssh -R 9222:localhost:9222 your-server-user@your-server-ip
65+
opencli browser profiles prune --temporary
6066
```
6167

62-
Leave this SSH session running in the background.
68+
### Passing Raw Chrome Flags
69+
70+
If you need additional native Chrome/Chromium launch flags, repeat `--browser-arg`:
71+
72+
```bash
73+
opencli browser launch \
74+
--port 9222 \
75+
--profile zhihu \
76+
--browser-arg=--lang=en-US \
77+
--browser-arg=--window-size=1440,900
78+
```
79+
80+
This is useful for window sizing, language overrides, proxies, and other Chromium flags that OpenCLI does not expose as first-class options.
81+
82+
---
6383

64-
### Method B: Reverse Proxy (ngrok / frp / socat)
84+
## Remote or Headless CDP Endpoints
6585

66-
If you cannot establish a direct SSH connection (e.g., due to NAT or firewalls), you can use an intranet penetration tool like `ngrok`.
86+
If Chrome is already running elsewhere and exposing a CDP endpoint, you can connect OpenCLI directly without using `opencli browser launch`.
6787

68-
Run this command on your **Local Machine** to expose your local port `9222` to the public internet securely via ngrok:
88+
Typical examples:
89+
90+
- a remote Linux server running headless Chrome
91+
- a manually started local Chrome instance
92+
- a tunneled CDP endpoint exposed through SSH or ngrok
93+
94+
You can either pass the endpoint explicitly:
6995

7096
```bash
71-
ngrok http 9222
97+
opencli browser doctor --backend cdp --cdp-endpoint http://127.0.0.1:9222 --live
98+
opencli browser run --backend cdp --cdp-endpoint http://127.0.0.1:9222 -- bilibili hot --limit 5
99+
```
100+
101+
Or export it once:
102+
103+
```bash
104+
export OPENCLI_CDP_ENDPOINT="http://127.0.0.1:9222"
105+
opencli doctor
106+
opencli bilibili hot --limit 5
72107
```
73108

74-
This will print a forwarding URL, such as `https://abcdef.ngrok.app`. **Copy this URL**.
109+
> 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 targets exist, narrow selection with `OPENCLI_CDP_TARGET`.
75110
76111
---
77112

78-
## Phase 3: Execution (Remote Server)
113+
## Exposing a Local CDP Port to a Remote Server
79114

80-
Now switch to your **Remote Server** where OpenCLI is installed.
115+
Because CDP binds to `localhost` by default, a remote machine usually cannot access it directly. Use one of these patterns if OpenCLI runs on a different machine than the browser.
81116

82-
Depending on the network tunnel method you chose in Phase 2, set the `OPENCLI_CDP_ENDPOINT` environment variable and run your commands.
117+
### Method A: SSH Reverse Tunnel
83118

84-
### If you used Method A (SSH Tunnel):
119+
Run this on your **local machine**:
120+
121+
```bash
122+
ssh -R 9222:localhost:9222 your-server-user@your-server-ip
123+
```
124+
125+
Then on the **remote server**:
85126

86127
```bash
87128
export OPENCLI_CDP_ENDPOINT="http://localhost:9222"
88-
opencli doctor # Verify connection
89-
opencli bilibili hot --limit 5 # Test a command
129+
opencli doctor
130+
opencli bilibili hot --limit 5
90131
```
91132

92-
### If you used Method B (Reverse Proxy like ngrok):
133+
### Method B: Reverse Proxy or Tunnel Tool
134+
135+
For example with `ngrok` on your **local machine**:
136+
137+
```bash
138+
ngrok http 9222
139+
```
140+
141+
Then on the **remote server**:
93142

94143
```bash
95-
# Use the URL you copied from ngrok earlier
96144
export OPENCLI_CDP_ENDPOINT="https://abcdef.ngrok.app"
97-
opencli doctor # Verify connection
98-
opencli bilibili hot --limit 5 # Test a command
145+
opencli doctor
146+
opencli bilibili hot --limit 5
147+
```
148+
149+
> Note: Some Chrome versions may require `--remote-allow-origins="*"` when CDP is accessed through reverse proxies or other cross-origin WebSocket paths.
150+
151+
---
152+
153+
## Starting Chrome Manually
154+
155+
If you still prefer to start Chrome yourself instead of using `opencli browser launch`, these commands work.
156+
157+
**macOS**
158+
159+
```bash
160+
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
161+
--remote-debugging-port=9222 \
162+
--user-data-dir="$HOME/chrome-debug-profile" \
163+
--remote-allow-origins="*"
99164
```
100165

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`).*
166+
**Linux**
167+
168+
```bash
169+
google-chrome \
170+
--remote-debugging-port=9222 \
171+
--user-data-dir="$HOME/chrome-debug-profile" \
172+
--remote-allow-origins="*"
173+
```
174+
175+
**Windows**
176+
177+
```cmd
178+
"C:\Program Files\Google\Chrome\Application\chrome.exe" ^
179+
--remote-debugging-port=9222 ^
180+
--user-data-dir="%USERPROFILE%\chrome-debug-profile" ^
181+
--remote-allow-origins="*"
182+
```
183+
184+
Once the browser is open, log into the target websites you want OpenCLI to reuse.
185+
186+
---
187+
188+
## Recommended Workflow
189+
190+
Use this order of operations for most CDP tasks:
191+
192+
1. Start or discover a CDP browser
193+
2. Verify connectivity with `opencli browser doctor`
194+
3. Run existing site commands through `opencli browser run`
195+
4. Stop the browser with `opencli browser stop`
196+
5. Manage profiles with `opencli browser profiles`
102197

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.
198+
That keeps the CDP path explicit, inspectable, and scriptable for both humans and AI agents.

docs/guide/getting-started.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ opencli bilibili hot --limit 5 # Browser command
3535
opencli zhihu hot -f json # JSON output
3636
```
3737

38+
### Browser Backend Utilities
39+
40+
OpenCLI also ships an additive `browser` command group for browser backend and profile management:
41+
42+
```bash
43+
opencli browser launch --port 9222
44+
opencli browser launch --port 9222 --profile zhihu
45+
opencli browser list
46+
opencli browser profiles
47+
opencli browser run --backend cdp --cdp-endpoint http://127.0.0.1:9222 -- zhihu search --keyword AI
48+
```
49+
50+
Use `launch` without `--profile` for a temporary browser profile. Use `--profile <name>` when you want persistent browser state.
51+
3852
### Output Formats
3953

4054
All built-in commands support `--format` / `-f`:
@@ -52,5 +66,6 @@ opencli bilibili hot -v # Verbose: show pipeline debug
5266

5367
- [Installation details](/guide/installation)
5468
- [Browser Bridge setup](/guide/browser-bridge)
69+
- [Direct CDP workflows](/advanced/cdp)
5570
- [All available adapters](/adapters/)
5671
- [For developers / AI agents](/developer/contributing)

0 commit comments

Comments
 (0)