@sleepinsummer/agent-browser-cli 0.2.7 → 0.2.8

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.
package/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
  <a href="https://github.com/sleepinginsummer/agent-browser-cli"><img src="https://img.shields.io/badge/CLI-agentbrowsercli-2ea44f" alt="CLI agentbrowsercli"></a>
11
11
  <a href="https://github.com/sleepinginsummer/agent-browser-cli/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-green" alt="License MIT"></a>
12
12
  <a href="https://github.com/sleepinginsummer/agent-browser-cli"><img src="https://img.shields.io/badge/sys-win%2Fmac%2Flinux-0078D6?labelColor=0078D6&color=C0C0C0" alt="sys win/mac/linux"></a>
13
- <a href="https://github.com/sleepinginsummer/agent-browser-cli/releases"><img src="https://img.shields.io/badge/release-v0.2.7-blue" alt="release v0.2.7"></a>
13
+ <a href="https://github.com/sleepinginsummer/agent-browser-cli/releases"><img src="https://img.shields.io/badge/release-v0.2.8-blue" alt="release v0.2.8"></a>
14
14
  <a href="https://github.com/sleepinginsummer/agent-browser-cli/pulls"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen" alt="PRs welcome"></a>
15
15
  </p>
16
16
 
@@ -26,7 +26,7 @@
26
26
 
27
27
  ## 项目信息
28
28
 
29
- - 当前版本:`0.2.7`
29
+ - 当前版本:`0.2.8`
30
30
  - 支持平台:Windows(包括 WSL)/ Mac / Linux
31
31
  - 浏览器:Chrome,需加载拓展 `assets/tmwd_cdp_bridge`
32
32
  - Linux 支持前提:本机 Chrome / Chromium 需要支持安装扩展
@@ -112,17 +112,51 @@ cargo build --release
112
112
 
113
113
  ## Chrome 扩展
114
114
 
115
- 推荐从 [最新 Release](https://github.com/sleepinginsummer/agent-browser-cli/releases/latest) 下载 `chrome-extensions.zip`,下载后解压,Chrome 打开 `chrome://extensions`,开启“开发者模式”,点击“加载已解压的扩展程序”,选择解压后的 `tmwd_cdp_bridge` 目录。
115
+ 1. 推荐从 [最新 Release](https://github.com/sleepinginsummer/agent-browser-cli/releases/latest) 下载 `chrome-extensions.zip`,下载后解压,Chrome 打开 `chrome://extensions`,开启“开发者模式”,点击“加载已解压的扩展程序”,选择解压后的 `tmwd_cdp_bridge` 目录。
116
116
 
117
- 本地源码构建时,也可以直接加载扩展目录:
117
+ 2. 本地源码构建时,也可以直接加载扩展目录:
118
118
 
119
119
  ```text
120
120
  assets/tmwd_cdp_bridge
121
121
  ```
122
122
 
123
- Chrome 需要至少打开一个正常网页标签页,不要只停留在 `about:blank` 或 `chrome://` 页面。
123
+ 3. Chrome 需要至少打开一个正常网页标签页,不要只停留在 `about:blank` 或 `chrome://` 页面。
124
+ 4. 扩展连接后会在页面右侧显示 Chrome 插件提示角标。角标支持拖动位置,鼠标悬浮时展开;10 秒无命令后自动隐藏,也可以点击 `本次隐藏` 手动隐藏,本次服务连接周期内不再显示,约 300 秒服务断开并下次重连后恢复。
125
+
126
+ ### 自定义Chrome插件的ws监听端口
127
+
128
+ - `18765`:默认插件 WebSocket 端口,Chrome 扩展连接使用,可通过 `agent-browser-cli set-extension-port <port>` 修改。
129
+ - `18767`:CLI HTTP API 端口,供 CLI 复用会话,不能作为插件端口使用。
130
+
131
+ CLI 修改插件端口:
132
+
133
+ ```bash
134
+ agent-browser-cli set-extension-port 18766
135
+ ```
136
+
137
+ 该命令会写入配置文件;如果 daemon 正在运行,会自动重启 daemon,让新端口立即生效。
138
+
139
+ 也可以手动修改配置文件。配置文件位于 `~/.agent-browser-cli/config.json`,不存在时会自动生成:
140
+
141
+ ```json
142
+ {
143
+ "extension_port": 18765
144
+ }
145
+ ```
146
+
147
+ 手动修改示例:
148
+
149
+ ```json
150
+ {
151
+ "extension_port": 18766
152
+ }
153
+ ```
154
+
155
+ 手动改配置后需要执行 `agent-browser-cli restart`,daemon 才会按新端口重新监听。
156
+
157
+ Chrome 插件 popup 中也可以修改插件端口并立即重连。插件端口必须和 CLI 配置中的 `extension_port` 一致。
158
+
124
159
 
125
- 扩展连接后会在页面右侧显示 Chrome 插件提示角标。角标支持拖动位置,鼠标悬浮时展开;10 秒无命令后自动隐藏,也可以点击 `本次隐藏` 手动隐藏,本次服务连接周期内不再显示,约 300 秒服务断开并下次重连后恢复。
126
160
 
127
161
  ## 快速自检
128
162
 
@@ -155,26 +189,13 @@ agent-browser-cli tabs
155
189
 
156
190
  ## 更新
157
191
 
158
- ```bash
159
- git pull
160
- cargo build --release
161
- ./target/release/agent-browser-cli restart
162
- ```
163
-
164
- 如果 Chrome 扩展有更新,在 `chrome://extensions` 中重新加载 `assets/tmwd_cdp_bridge` 扩展。
165
-
166
- 当前扩展配置标识为:
167
-
168
- ```js
169
- const TID = '__agent_browser_cli_bridge_26c9f1';
192
+ ai一句话更新
193
+ ```text
194
+ 请阅读 https://github.com/sleepinginsummer/agent-browser-cli/blob/main/AI_INSTALL.md,按说明更新 CLI、重新下载插件zip让用户指定位置,用户手动加载 Chrome 扩展,并更新相关 SKILL.md`。
170
195
  ```
171
196
 
172
- 如果你把 skill 安装到了 Codex/Agent 的全局目录,更新后同步复制:
197
+ 如果 Chrome 扩展有更新,在 `chrome://extensions` 中重新下载zpi覆盖,然后重新加载 `assets/tmwd_cdp_bridge` 扩展。
173
198
 
174
- ```bash
175
- mkdir -p ~/.agents/skills/agent-browser-cli
176
- cp skills/agent-browser-cli/SKILL.md ~/.agents/skills/agent-browser-cli/SKILL.md
177
- ```
178
199
 
179
200
  ## 卸载
180
201
 
@@ -193,10 +214,7 @@ rm -rf ~/.agents/skills/agent-browser-cli
193
214
 
194
215
  最后在 Chrome 扩展管理页中移除 `TMWD CDP Bridge` 扩展,或删除已加载的 `assets/tmwd_cdp_bridge` 扩展配置。
195
216
 
196
- ## 端口
197
217
 
198
- - `18765`:底层 `TMWebDriver` WebSocket,Chrome 扩展连接使用。
199
- - `18767`:外层 `agent-browser-cli` HTTP 服务,供 CLI 复用会话。
200
218
 
201
219
  ## 友情链接
202
220
 
package/README_EN.md CHANGED
@@ -10,7 +10,7 @@ Browser perception · Page control · Chrome session reuse · CDP · Conditional
10
10
  <a href="https://github.com/sleepinginsummer/agent-browser-cli"><img src="https://img.shields.io/badge/CLI-agentbrowsercli-2ea44f" alt="CLI agentbrowsercli"></a>
11
11
  <a href="https://github.com/sleepinginsummer/agent-browser-cli/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-green" alt="License MIT"></a>
12
12
  <a href="https://github.com/sleepinginsummer/agent-browser-cli"><img src="https://img.shields.io/badge/sys-win%2Fmac%2Flinux-0078D6?labelColor=0078D6&color=C0C0C0" alt="sys win/mac/linux"></a>
13
- <a href="https://github.com/sleepinginsummer/agent-browser-cli/releases"><img src="https://img.shields.io/badge/release-v0.2.7-blue" alt="release v0.2.7"></a>
13
+ <a href="https://github.com/sleepinginsummer/agent-browser-cli/releases"><img src="https://img.shields.io/badge/release-v0.2.8-blue" alt="release v0.2.8"></a>
14
14
  <a href="https://github.com/sleepinginsummer/agent-browser-cli/pulls"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen" alt="PRs welcome"></a>
15
15
  </p>
16
16
 
@@ -26,7 +26,7 @@ This project is not Selenium or Playwright. It is better suited for helping agen
26
26
 
27
27
  ## Project Info
28
28
 
29
- - Current version: `0.2.7`
29
+ - Current version: `0.2.8`
30
30
  - Supported platforms: Windows (including WSL) / Mac / Linux
31
31
  - Browser: Chrome, with the `assets/tmwd_cdp_bridge` extension loaded
32
32
  - Linux prerequisite: the local Chrome / Chromium build must support loading extensions
@@ -112,17 +112,49 @@ cargo build --release
112
112
 
113
113
  ## Chrome Extension
114
114
 
115
- Recommended: download `chrome-extensions.zip` from the [latest Release](https://github.com/sleepinginsummer/agent-browser-cli/releases/latest), extract it, open `chrome://extensions` in Chrome, enable `Developer mode`, click `Load unpacked`, and select the extracted `tmwd_cdp_bridge` directory.
115
+ 1. Recommended: download `chrome-extensions.zip` from the [latest Release](https://github.com/sleepinginsummer/agent-browser-cli/releases/latest), extract it, open `chrome://extensions` in Chrome, enable `Developer mode`, click `Load unpacked`, and select the extracted `tmwd_cdp_bridge` directory.
116
116
 
117
- When building from local source, you can also load this extension directory directly:
117
+ 2. When building from local source, you can also load this extension directory directly:
118
118
 
119
119
  ```text
120
120
  assets/tmwd_cdp_bridge
121
121
  ```
122
122
 
123
- Chrome needs at least one normal web page tab open. Do not leave it only on `about:blank` or `chrome://` pages.
123
+ 3. Chrome needs at least one normal web page tab open. Do not leave it only on `about:blank` or `chrome://` pages.
124
+ 4. After the extension is connected, a Chrome extension tip badge appears on the right side of the page. The badge position is draggable and expands on hover. It auto-hides after 10 seconds without commands, and you can also click `Hide for this session` to hide it manually. Manual hiding lasts for the current service connection cycle and resets after the service disconnects after about 300 seconds and reconnects.
124
125
 
125
- After the extension is connected, a Chrome extension tip badge appears on the right side of the page. The badge position is draggable and expands on hover. It auto-hides after 10 seconds without commands, and you can also click `Hide for this session` to hide it manually. Manual hiding lasts for the current service connection cycle and resets after the service disconnects after about 300 seconds and reconnects.
126
+ ### Custom Chrome Extension WebSocket Port
127
+
128
+ - `18765`: default extension WebSocket port, used by the Chrome extension. It can be changed with `agent-browser-cli set-extension-port <port>`.
129
+ - `18767`: CLI HTTP API port, used by the CLI to reuse the session. It cannot be used as the extension port.
130
+
131
+ Change the extension port from CLI:
132
+
133
+ ```bash
134
+ agent-browser-cli set-extension-port 18766
135
+ ```
136
+
137
+ This command writes the config file. If the daemon is running, it restarts the daemon so the new port takes effect immediately.
138
+
139
+ You can also edit the config file manually. The config file is `~/.agent-browser-cli/config.json`. It is created automatically when missing:
140
+
141
+ ```json
142
+ {
143
+ "extension_port": 18765
144
+ }
145
+ ```
146
+
147
+ Manual edit example:
148
+
149
+ ```json
150
+ {
151
+ "extension_port": 18766
152
+ }
153
+ ```
154
+
155
+ After manually editing the config file, run `agent-browser-cli restart` so the daemon listens on the new port.
156
+
157
+ The Chrome extension popup can also update the extension port and reconnect immediately. The popup port must match the CLI `extension_port` config.
126
158
 
127
159
  ## Quick Check
128
160
 
@@ -155,26 +187,13 @@ agent-browser-cli tabs
155
187
 
156
188
  ## Update
157
189
 
158
- ```bash
159
- git pull
160
- cargo build --release
161
- ./target/release/agent-browser-cli restart
162
- ```
163
-
164
- If the Chrome extension has updates, reload the `assets/tmwd_cdp_bridge` extension in `chrome://extensions`.
190
+ AI one-line update:
165
191
 
166
- Current extension bridge identifier:
167
-
168
- ```js
169
- const TID = '__agent_browser_cli_bridge_26c9f1';
192
+ ```text
193
+ Please read https://github.com/sleepinginsummer/agent-browser-cli/blob/main/AI_INSTALL.md, follow the instructions to update the CLI, download the extension zip again to the user-specified location, ask the user to manually load the Chrome extension, and update the related SKILL.md.
170
194
  ```
171
195
 
172
- If you installed the skill into a global Codex/Agent directory, copy it again after updating:
173
-
174
- ```bash
175
- mkdir -p ~/.agents/skills/agent-browser-cli
176
- cp skills/agent-browser-cli/SKILL.md ~/.agents/skills/agent-browser-cli/SKILL.md
177
- ```
196
+ If the Chrome extension has updates, download the zip again, overwrite the existing files, and reload the `assets/tmwd_cdp_bridge` extension in `chrome://extensions`.
178
197
 
179
198
  ## Uninstall
180
199
 
@@ -193,11 +212,6 @@ rm -rf ~/.agents/skills/agent-browser-cli
193
212
 
194
213
  Finally, remove the `TMWD CDP Bridge` extension from Chrome's extension management page, or remove the loaded `assets/tmwd_cdp_bridge` extension configuration.
195
214
 
196
- ## Ports
197
-
198
- - `18765`: underlying `TMWebDriver` WebSocket, used by the Chrome extension.
199
- - `18767`: outer `agent-browser-cli` HTTP service, used by the CLI to reuse the session.
200
-
201
215
  ## Friendly Links
202
216
 
203
217
  - [LINUX DO - A New Ideal Community](https://linux.do/)
@@ -16,9 +16,13 @@ chrome.runtime.onInstalled.addListener(() => {
16
16
  });
17
17
 
18
18
  let lastCommandAt = 0;
19
+ const DEFAULT_WS_PORT = 18765;
20
+ const CLI_API_PORT = 18767;
21
+ let wsPort = DEFAULT_WS_PORT;
19
22
 
20
23
  async function handleExtMessage(msg, sender) {
21
24
  if (msg.cmd === 'status') return handleStatus();
25
+ if (msg.cmd === 'setPort') return await handleSetPort(msg);
22
26
  lastCommandAt = Date.now();
23
27
  if (msg.cmd === 'cookies') return await handleCookies(msg, sender);
24
28
  if (msg.cmd === 'cdp') return await handleCDP(msg, sender);
@@ -78,12 +82,27 @@ function handleStatus() {
78
82
  ok: true,
79
83
  data: {
80
84
  wsConnected: !!ws && ws.readyState === WebSocket.OPEN,
81
- wsUrl: WS_URL,
85
+ wsUrl: getWsUrl(),
86
+ wsPort,
82
87
  lastCommandAt
83
88
  }
84
89
  };
85
90
  }
86
91
 
92
+ async function handleSetPort(msg) {
93
+ const port = normalizePort(msg.port);
94
+ await saveWsPort(port);
95
+ reconnectWS();
96
+ return {
97
+ ok: true,
98
+ data: {
99
+ wsPort,
100
+ wsUrl: getWsUrl(),
101
+ wsConnected: !!ws && ws.readyState === WebSocket.OPEN
102
+ }
103
+ };
104
+ }
105
+
87
106
  chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
88
107
  handleExtMessage(msg, sender).then(sendResponse);
89
108
  return true;
@@ -250,11 +269,36 @@ function buildCdpScript(code) {
250
269
 
251
270
  // --- WebSocket Client for TMWebDriver ---
252
271
  let ws = null;
253
- const WS_URL = 'ws://127.0.0.1:18765';
272
+
273
+ function normalizePort(port) {
274
+ const value = Number(port);
275
+ if (!Number.isInteger(value) || value < 1 || value > 65535) {
276
+ throw new Error('端口必须是 1-65535');
277
+ }
278
+ if (value === CLI_API_PORT) {
279
+ throw new Error('18767 是 agent-browser-cli API 端口,请换一个插件端口');
280
+ }
281
+ return value;
282
+ }
283
+
284
+ async function loadWsPort() {
285
+ const data = await chrome.storage.local.get({ wsPort: DEFAULT_WS_PORT });
286
+ wsPort = normalizePort(data.wsPort);
287
+ return wsPort;
288
+ }
289
+
290
+ async function saveWsPort(port) {
291
+ wsPort = normalizePort(port);
292
+ await chrome.storage.local.set({ wsPort });
293
+ }
294
+
295
+ function getWsUrl() {
296
+ return `ws://127.0.0.1:${wsPort}`;
297
+ }
254
298
 
255
299
  function scheduleProbe() {
256
300
  // Use chrome.alarms to survive MV3 service worker suspension
257
- chrome.alarms.create('tmwd-ws-probe', { delayInMinutes: 0.083 }); // ~5s
301
+ chrome.alarms.create('tmwd-ws-probe', { delayInMinutes: 0.017 }); // ~1s
258
302
  }
259
303
 
260
304
  function scheduleKeepalive() {
@@ -266,7 +310,7 @@ async function isServerAlive() {
266
310
  try {
267
311
  const ctrl = new AbortController();
268
312
  setTimeout(() => ctrl.abort(), 2000);
269
- await fetch('http://127.0.0.1:18765', { signal: ctrl.signal });
313
+ await fetch(`http://127.0.0.1:${wsPort}`, { signal: ctrl.signal });
270
314
  return true; // Got HTTP response → port is listening
271
315
  } catch (e) {
272
316
  return false; // Network error (connection refused) or timeout → server not alive
@@ -372,12 +416,20 @@ async function handleWsExec(data) {
372
416
  }
373
417
  }
374
418
 
375
- function connectWS() {
419
+ async function connectWS() {
420
+ await loadWsPort();
376
421
  if (ws && ws.readyState <= 1) return; // CONNECTING or OPEN
422
+ if (!(await isServerAlive())) {
423
+ console.warn('[TMWD-WS] Server not ready, retrying later:', getWsUrl());
424
+ ws = null;
425
+ scheduleProbe();
426
+ return;
427
+ }
377
428
  ws = null;
378
- console.log('[TMWD-WS] Connecting to', WS_URL);
429
+ const wsUrl = getWsUrl();
430
+ console.log('[TMWD-WS] Connecting to', wsUrl);
379
431
  try {
380
- ws = new WebSocket(WS_URL);
432
+ ws = new WebSocket(wsUrl);
381
433
  } catch (e) {
382
434
  console.error('[TMWD-WS] Constructor error:', e);
383
435
  ws = null;
@@ -429,11 +481,21 @@ function connectWS() {
429
481
  scheduleProbe();
430
482
  };
431
483
  ws.onerror = (e) => {
432
- console.error('[TMWD-WS] Error:', e);
484
+ console.warn('[TMWD-WS] Connection warning:', e);
433
485
  // onclose will fire after this, which triggers reconnect
434
486
  };
435
487
  }
436
488
 
489
+ function reconnectWS() {
490
+ if (ws) {
491
+ try { ws.onclose = null; ws.close(); } catch (_) {}
492
+ }
493
+ ws = null;
494
+ chrome.alarms.clear('tmwd-ws-probe');
495
+ chrome.alarms.clear('tmwd-ws-keepalive');
496
+ connectWS();
497
+ }
498
+
437
499
  // Initial connect + wake-up hooks
438
500
  connectWS();
439
501
  injectContentScriptsIntoExistingTabs();
@@ -1 +1 @@
1
- const TID = '__agent_browser_cli_bridge_26c9f1';
1
+ globalThis.__agent_browser_cli_TID = '__agent_browser_cli_bridge_26c9f1';
@@ -1,4 +1,5 @@
1
1
  ;(function(){ if (/streamlit/i.test(document.title)) return;
2
+ const TID = globalThis.__agent_browser_cli_TID || '__agent_browser_cli_bridge_26c9f1';
2
3
  if (window.__agentBrowserCliCleanup) window.__agentBrowserCliCleanup();
3
4
  if (window.__agentBrowserCliObserverCleanup) window.__agentBrowserCliObserverCleanup();
4
5
  document.querySelectorAll('#agent-browser-cli-ind,#agent-browser-cli-style').forEach(e => e.remove());
@@ -10,6 +10,7 @@
10
10
  "debugger",
11
11
  "scripting",
12
12
  "alarms",
13
+ "storage",
13
14
  "declarativeNetRequest",
14
15
  "management",
15
16
  "contentSettings"
@@ -37,4 +38,4 @@
37
38
  "default_popup": "popup.html",
38
39
  "default_title": "TMWD CDP Bridge"
39
40
  }
40
- }
41
+ }
@@ -3,17 +3,32 @@
3
3
  <head>
4
4
  <meta charset="utf-8">
5
5
  <style>
6
- body{width:420px;max-height:500px;margin:0;padding:8px;font:12px monospace;background:#1e1e1e;color:#d4d4d4;overflow-y:auto}
6
+ body{width:420px;max-height:560px;margin:0;padding:8px;font:12px monospace;background:#1e1e1e;color:#d4d4d4;overflow-y:auto}
7
7
  h3{margin:4px 0;color:#569cd6}
8
8
  button{background:#264f78;color:#fff;border:none;padding:4px 12px;cursor:pointer;border-radius:3px;margin-bottom:6px}
9
9
  button:hover{background:#37699e}
10
+ input{width:86px;background:#252526;color:#d4d4d4;border:1px solid #3c3c3c;border-radius:3px;padding:4px;font:12px monospace}
11
+ label{display:inline-flex;align-items:center;gap:6px;margin-right:6px}
12
+ .panel{margin-bottom:8px;padding:6px;background:#252526;border-radius:3px}
13
+ .row{display:flex;align-items:center;gap:6px;margin-top:6px}
14
+ .status{color:#9cdcfe}
15
+ .error{color:#f48771}
10
16
  pre{white-space:pre-wrap;word-break:break-all;margin:0;padding:6px;background:#252526;border-radius:3px;max-height:420px;overflow-y:auto}
11
17
  </style>
12
18
  </head>
13
19
  <body>
20
+ <h3>🔌 Bridge</h3>
21
+ <div class="panel">
22
+ <div id="bridgeStatus" class="status">读取连接状态...</div>
23
+ <div class="row">
24
+ <label>端口 <input id="port" type="number" min="1" max="65535"></label>
25
+ <button id="savePort">保存并重连</button>
26
+ </div>
27
+ <div id="portMsg"></div>
28
+ </div>
14
29
  <h3>🍪 Cookies</h3>
15
30
  <button id="refresh">刷新</button>
16
31
  <pre id="out">点击刷新获取 cookies...</pre>
17
32
  <script src="popup.js"></script>
18
33
  </body>
19
- </html>
34
+ </html>
@@ -1,10 +1,49 @@
1
1
  document.addEventListener('DOMContentLoaded', () => {
2
- const out = document.getElementById('out');
3
2
  const btn = document.getElementById('refresh');
3
+ const savePortBtn = document.getElementById('savePort');
4
4
  btn.addEventListener('click', fetchCookies);
5
+ savePortBtn.addEventListener('click', savePort);
6
+ refreshBridgeStatus();
5
7
  fetchCookies();
6
8
  });
7
9
 
10
+ async function refreshBridgeStatus() {
11
+ const status = document.getElementById('bridgeStatus');
12
+ const portInput = document.getElementById('port');
13
+ try {
14
+ const resp = await chrome.runtime.sendMessage({ cmd: 'status' });
15
+ if (!resp?.ok) throw new Error(resp?.error || 'unknown');
16
+ const data = resp.data || {};
17
+ portInput.value = data.wsPort || 18765;
18
+ status.textContent = `状态: ${data.wsConnected ? '已连接' : '未连接'} ${data.wsUrl || ''}`;
19
+ } catch (e) {
20
+ status.textContent = '状态读取失败: ' + e.message;
21
+ status.className = 'error';
22
+ }
23
+ }
24
+
25
+ async function savePort() {
26
+ const portInput = document.getElementById('port');
27
+ const portMsg = document.getElementById('portMsg');
28
+ try {
29
+ const port = Number(portInput.value);
30
+ if (!Number.isInteger(port) || port < 1 || port > 65535) {
31
+ throw new Error('端口必须是 1-65535');
32
+ }
33
+ if (port === 18767) {
34
+ throw new Error('18767 是 agent-browser-cli API 端口,请换一个插件端口');
35
+ }
36
+ const resp = await chrome.runtime.sendMessage({ cmd: 'setPort', port });
37
+ if (!resp?.ok) throw new Error(resp?.error || 'unknown');
38
+ portMsg.textContent = `Success: 已保存端口 ${port},正在使用新端口重连`;
39
+ portMsg.className = 'status';
40
+ await refreshBridgeStatus();
41
+ } catch (e) {
42
+ portMsg.textContent = '保存失败: ' + e.message;
43
+ portMsg.className = 'error';
44
+ }
45
+ }
46
+
8
47
  async function fetchCookies() {
9
48
  const out = document.getElementById('out');
10
49
  try {
@@ -21,4 +60,4 @@ async function fetchCookies() {
21
60
  const str = resp.data.map(c => `${c.name}=${c.value}`).join('; ');
22
61
  await navigator.clipboard.writeText(str);
23
62
  } catch (e) { out.textContent = 'Error: ' + e.message; }
24
- }
63
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleepinsummer/agent-browser-cli",
3
- "version": "0.2.7",
3
+ "version": "0.2.8",
4
4
  "description": "Agent-oriented browser sensing and control CLI backed by a native Rust daemon.",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -44,7 +44,7 @@ assets/tmwd_cdp_bridge
44
44
  扩展配置必须存在:
45
45
 
46
46
  ```js
47
- const TID = '__agent_browser_cli_bridge_26c9f1';
47
+ globalThis.__agent_browser_cli_TID = '__agent_browser_cli_bridge_26c9f1';
48
48
  ```
49
49
 
50
50
  对应文件:
@@ -72,13 +72,36 @@ agent-browser-cli restart
72
72
  ```
73
73
 
74
74
  常驻服务端口:
75
- - `18765`:底层 `TMWebDriver` WebSocket,Chrome 扩展连接使用。
76
- - `18767`:外层 `agent-browser-cli` HTTP 服务,供 CLI 复用会话。
75
+ - `18765`:默认插件 WebSocket 端口,Chrome 扩展连接使用,可通过插件 popup 或 `agent-browser-cli set-extension-port <port>` 修改。
76
+ - `18767`:外层 `agent-browser-cli` HTTP API 端口,供 CLI 复用会话,不能作为插件端口使用。
77
+
78
+ 插件端口配置文件:
79
+
80
+ ```text
81
+ ~/.agent-browser-cli/config.json
82
+ ```
83
+
84
+ 最小配置:
85
+
86
+ ```json
87
+ {
88
+ "extension_port": 18765
89
+ }
90
+ ```
91
+
92
+ CLI 修改插件端口:
93
+
94
+ ```bash
95
+ agent-browser-cli set-extension-port 18766
96
+ ```
97
+
98
+ Chrome 插件 popup 也可以修改插件端口并立即重连。插件端口必须和 CLI 配置中的 `extension_port` 一致。
77
99
 
78
100
  成功标志:
79
101
  - 返回 `status=success`
80
102
  - 能看到 `tabs_count`
81
- - 首次运行会拉起本地 WS 服务 `ws://127.0.0.1:18765`
103
+ - `agent-browser-cli status` `ports.extension.matched=true`
104
+ - `agent-browser-cli status` 中 `connection.extension_connected=true`
82
105
 
83
106
  ## 推荐 CLI 调用
84
107