@xiaoqiong0v0/opencode-browser-tool 1.2.0
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 +182 -0
- package/dist/client.js +229 -0
- package/dist/config/index.js +165 -0
- package/dist/devices.js +26 -0
- package/dist/i18n/en.js +95 -0
- package/dist/i18n/index.js +24 -0
- package/dist/i18n/types.js +1 -0
- package/dist/i18n/zh.js +95 -0
- package/dist/index.js +451 -0
- package/dist/types.js +1 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# opencode-browser-tool
|
|
2
|
+
|
|
3
|
+
opencode 插件:Tauri 多 Webview 浏览器外壳 + 零注入视觉元素选取面板,提供 40+ 个浏览器自动化工具。
|
|
4
|
+
|
|
5
|
+
> 架构演进:V1-V3 使用 Playwright 注入式面板(存在弹框类元素无法批注的问题);V4 改为 Rust Shell + 原始调试协议;V5 迁移到 Tauri 2 多 Webview(跨平台)。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```jsonc
|
|
10
|
+
// opencode.json
|
|
11
|
+
{
|
|
12
|
+
"plugin": ["@xiaoqiong0v0/opencode-browser-tool"]
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## 配置
|
|
17
|
+
|
|
18
|
+
配置文件:`~/.config/opencode/browser-tool.jsonc`(首次运行自动生成)
|
|
19
|
+
|
|
20
|
+
```jsonc
|
|
21
|
+
{
|
|
22
|
+
"panelLang": "zh",
|
|
23
|
+
"toolLang": "en",
|
|
24
|
+
"sessionIsolation": false
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
项目级配置放 `<项目根目录>/.opencode/browser-tool.jsonc`,会覆盖全局。
|
|
29
|
+
|
|
30
|
+
环境变量(优先级最高):
|
|
31
|
+
- `BT_LANG` — 同时设置 panelLang 和 toolLang
|
|
32
|
+
- `BT_BROWSERS_PATH` — 浏览器缓存目录
|
|
33
|
+
- `BT_SHELL_URL` — **附着模式**:连接到已运行的 shell(完整基地址,如 `http://127.0.0.1:18280`),优先级最高
|
|
34
|
+
- `BT_SHELL_PORT` — **附着模式**:仅指定端口,等价于 `http://127.0.0.1:<port>`
|
|
35
|
+
- `BT_SHELL_PATH` — **启动模式**:指定要 spawn 的 Rust shell 二进制路径(开发用)
|
|
36
|
+
|
|
37
|
+
> **附着模式 vs 启动模式**:设置了 `BT_SHELL_URL`(优先)或 `BT_SHELL_PORT` 时进入附着模式——插件**不 spawn** 任何进程,直接连接该地址的外部 shell;两者都未设置时按启动模式 spawn 本地二进制(`BT_SHELL_PATH` 可覆盖查找路径)。附着模式下 `bt_close` 只关闭浏览器窗口并断开连接,**不会**结束外部 shell 进程(进程不归插件管理)。
|
|
38
|
+
|
|
39
|
+
**WSL / Linux 附着示例**(shell 跑在 WSL,插件跑在 Windows 宿主):
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
# WSL 内:构建并启动 Linux 版 shell,监听 18280
|
|
43
|
+
cd rust && cargo build --release
|
|
44
|
+
./target/release/bt-shell --port 18280
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# Windows 宿主:设置环境变量后重启 opencode(WSL2 支持从宿主经 localhost 访问)
|
|
49
|
+
set BT_SHELL_URL=http://127.0.0.1:18280
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
⚠ WSL 内启动 shell 时**不要**设置 `WEBKIT_DISABLE_DMABUF_RENDERER` / `WEBKIT_DISABLE_COMPOSITING_MODE`——它们会破坏透明合成(覆盖层渲染成黑块/透出桌面),详见 [docs/linux-support.md](./docs/linux-support.md)。
|
|
53
|
+
|
|
54
|
+
## 架构(V5)
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
opencode(Bun)
|
|
58
|
+
└─ spawn("bt-shell", [--port ...]) → HTTP 127.0.0.1:PORT
|
|
59
|
+
└─ Rust Shell(Tauri 2 单窗口 + 多 Webview)
|
|
60
|
+
├─ toolbar Webview(顶部:标签行 + 导航行 + 批注/截图/面板/窗口按钮)
|
|
61
|
+
├─ page-1..page-N Webview(真多标签,每标签独立 webview,渲染目标网页)
|
|
62
|
+
├─ overlay Webview(透明覆盖层,批注高亮/截图选区/通知)
|
|
63
|
+
├─ panel Webview(右侧覆盖式浮层,AI 功能区 + 配置区)
|
|
64
|
+
└─ newtab(内部新标签页,跟随主题)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
- 浏览器渲染引擎(WebView2 / WebKitGTK / WKWebView,随平台)直接绘制到插件窗口
|
|
68
|
+
- 页面零注入残留脚本,弹框类元素可正常批注
|
|
69
|
+
- 所有工具调用通过 `POST http://127.0.0.1:PORT/api/{command}` 通信
|
|
70
|
+
- 除 spawn 本地 shell 外,也可用 `BT_SHELL_URL` / `BT_SHELL_PORT` **附着到已运行的 shell**(不 spawn,适用于 shell 跑在 WSL/Linux、插件跑在 Windows)
|
|
71
|
+
- 跨平台:Windows / Linux / macOS
|
|
72
|
+
- 真多标签:每标签独立 page Webview,切换不重载;`target=_blank` / `window.open` 链接通过 WebView2 原生事件拦截并打开为新标签
|
|
73
|
+
- 窗口无标题栏,自定义窗口按钮在标签行右侧;最小尺寸 500×400(逻辑像素)
|
|
74
|
+
|
|
75
|
+
## 工具
|
|
76
|
+
|
|
77
|
+
| 类别 | 工具 |
|
|
78
|
+
|------|------|
|
|
79
|
+
| **导航** | `bt_navigate`, `bt_go_back`, `bt_go_forward`, `bt_reload`, `bt_close` |
|
|
80
|
+
| **交互** | `bt_click`, `bt_fill`, `bt_clear`, `bt_select`, `bt_hover`, `bt_drag`, `bt_press_key`, `bt_upload_file`, `bt_iframe_click`, `bt_iframe_fill` |
|
|
81
|
+
| **标签页** | `bt_list_tabs`, `bt_switch_tab`, `bt_new_tab`, `bt_close_tab`, `bt_click_and_switch_tab` |
|
|
82
|
+
| **信息** | `bt_screenshot`, `bt_evaluate`, `bt_get_visible_text`, `bt_get_visible_html`, `bt_console_logs`, `bt_get_browser_status`, `bt_get_element_state`, `bt_get_dropdown_options`, `bt_list_records`, `bt_expect_response`, `bt_assert_response`, `bt_get_accessibility_tree` |
|
|
83
|
+
| **滚动/等待** | `bt_scroll`, `bt_scroll_to_element`, `bt_wait_for_selector`, `bt_resize`, `bt_set_device` |
|
|
84
|
+
| **设备/工具** | `bt_custom_user_agent`, `bt_devtools`(开发者工具开关) |
|
|
85
|
+
| **其他** | `bt_show_notification`, `bt_read_record_content` |
|
|
86
|
+
|
|
87
|
+
### 工具参数
|
|
88
|
+
|
|
89
|
+
| 工具 | 参数(类型) | 说明 |
|
|
90
|
+
|------|------|------|
|
|
91
|
+
| `bt_navigate` | `url`(string) 必填 | 导航到指定地址(裸域名自动补 http://) |
|
|
92
|
+
| `bt_click` | `selector`(string) 必填 | 点击 CSS 选择器匹配的元素 |
|
|
93
|
+
| `bt_fill` | `selector`, `value`(string) 必填 | 向输入框填入文本 |
|
|
94
|
+
| `bt_clear` | `selector`(string) 必填 | 清空输入框 |
|
|
95
|
+
| `bt_select` | `selector`, `value`(string) 必填 | 选择下拉框选项 |
|
|
96
|
+
| `bt_hover` | `selector`(string) 必填 | 悬停在元素上 |
|
|
97
|
+
| `bt_drag` | `sourceSelector`, `targetSelector`(string) 必填 | 从源元素拖到目标元素 |
|
|
98
|
+
| `bt_press_key` | `key`(string) 必填, `selector`(string) 可选 | 按键(如 Enter/Escape);可指定聚焦元素 |
|
|
99
|
+
| `bt_upload_file` | `selector`, `filePath`(string) 必填 | 向文件输入框上传文件 |
|
|
100
|
+
| `bt_screenshot` | `selector`(string) 可选(当前忽略) | 截取当前视口,返回图片附件(PNG) |
|
|
101
|
+
| `bt_evaluate` | `script`(string) 必填 | 在页面执行 JS 并返回 JSON 结果 |
|
|
102
|
+
| `bt_get_visible_text` | `selector`(string) 可选 | 返回页面/元素可见文本 |
|
|
103
|
+
| `bt_get_visible_html` | `selector`, `removeScripts`(boolean), `removeComments`(boolean), `maxLength`(number) | 返回页面/元素 HTML |
|
|
104
|
+
| `bt_console_logs` | `type`(string), `search`(string), `limit`(number), `clear`(boolean) | 读取页面控制台日志 |
|
|
105
|
+
| `bt_go_back` | 无 | 浏览器后退,返回当前 URL |
|
|
106
|
+
| `bt_go_forward` | 无 | 浏览器前进,返回当前 URL |
|
|
107
|
+
| `bt_resize` | `width`, `height`(number) 必填 | 调整窗口尺寸(逻辑像素) |
|
|
108
|
+
| `bt_set_device` | `name`(string) 可选 | 应用设备预设(空参数列出 13 种预设) |
|
|
109
|
+
| `bt_devtools` | `action`(string):`toggle`/`open`/`close` | 开关开发者工具 |
|
|
110
|
+
| `bt_reload` | 无 | 刷新当前页面,返回 URL |
|
|
111
|
+
| `bt_close` | 无 | 退出浏览器外壳 |
|
|
112
|
+
| `bt_show_notification` | `message`(string), `type`(string):`ok`/`bad`/`err` | 显示页面通知气泡 |
|
|
113
|
+
| `bt_scroll` | `direction`(string):`up`/`down`/`left`/`right`, `amount`(number) | 滚动页面 |
|
|
114
|
+
| `bt_wait_for_selector` | `selector`, `timeout`(number, ms) | 等待元素出现 |
|
|
115
|
+
| `bt_click_and_switch_tab` | `selector`(string) 必填 | 点击元素;若触发新标签则自动切换,返回当前 URL |
|
|
116
|
+
| `bt_iframe_click` | `iframeSelector`, `selector`(string) 必填 | 在 iframe 中点击(仅同源可访问) |
|
|
117
|
+
| `bt_iframe_fill` | `iframeSelector`, `selector`, `value`(string) 必填 | 在 iframe 中输入(仅同源可访问) |
|
|
118
|
+
| `bt_get_browser_status` | 无 | 返回 open/url/title/tabs 数量 |
|
|
119
|
+
| `bt_list_tabs` | 无 | 列出标签页(索引+URL) |
|
|
120
|
+
| `bt_switch_tab` | `index`(number) 必填 | 按位置(0 起)切换标签 |
|
|
121
|
+
| `bt_new_tab` | `url`(string) 必填 | 新建独立标签页(真多标签)并导航 |
|
|
122
|
+
| `bt_close_tab` | `index`(number) 可选 | 关闭指定标签;缺省关闭当前激活标签 |
|
|
123
|
+
| `bt_get_element_state` | `selector`(string) 必填 | 返回元素可见性/文本/位置 |
|
|
124
|
+
| `bt_scroll_to_element` | `selector`(string) 必填 | 滚动到元素 |
|
|
125
|
+
| `bt_get_dropdown_options` | `selector`(string) 必填 | 列出下拉框选项 |
|
|
126
|
+
| `bt_custom_user_agent` | `userAgent`(string) 必填 | 设置自定义 User-Agent(仅 Windows 支持) |
|
|
127
|
+
| `bt_expect_response` | `url`(string) 必填 | 记录期望匹配的响应模式,清空响应历史后重新捕获 |
|
|
128
|
+
| `bt_assert_response` | `id`(string) 必填 | 断言是否存在匹配的响应(返回 `url` + `status` 码) |
|
|
129
|
+
| `bt_get_accessibility_tree` | `selector`, `maxDepth`(number) | 返回页面可访问性树(role/name/value 嵌套结构) |
|
|
130
|
+
| `bt_list_records` | 无 | 列出批注/截图记录(索引/标签/说明) |
|
|
131
|
+
| `bt_read_record_content` | `id`(number) 必填 | 读取单条记录详情 |
|
|
132
|
+
|
|
133
|
+
## 设备预设
|
|
134
|
+
|
|
135
|
+
内置 13 种设备预设(窗口尺寸 + User-Agent),可通过 `bt_set_device` 或右侧面板下拉框一键切换:
|
|
136
|
+
|
|
137
|
+
| 分类 | 预设 |
|
|
138
|
+
|------|------|
|
|
139
|
+
| 桌面 | `desktop-1080p`, `desktop-1440p` |
|
|
140
|
+
| iPad | `ipad-pro-11`, `ipad-10`, `ipad-mini-6` |
|
|
141
|
+
| iPhone | `iphone-15-pro`, `iphone-14`, `iphone-13`, `iphone-12`, `iphone-se` |
|
|
142
|
+
| Android | `pixel-7`, `pixel-6`, `galaxy-s23` |
|
|
143
|
+
|
|
144
|
+
切换时自动调整窗口尺寸并运行时修改 UA(UA 修改仅 Windows 支持)。
|
|
145
|
+
|
|
146
|
+
## 批注 / 截图
|
|
147
|
+
|
|
148
|
+
工具栏导航行的 ✏️ 批注、📷 截图按钮进入对应模式(进入后自动收起面板):
|
|
149
|
+
|
|
150
|
+
- **批注模式** — 悬停高亮元素(覆盖层自绘),点击元素弹出输入框填写说明;引擎层拦截,弹框/下拉类元素不失焦
|
|
151
|
+
- **截图模式** — 点击立即截全屏;也可进入截图模式后拖动框选区域(选区可拖拽手柄调整大小),保存时可按需裁剪(遮罩式选区,默认整图,双击还原)
|
|
152
|
+
- **记录列表** — 面板「AI 功能」区:批注/截图记录卡片(截图带缩略图),显示所属标签 URL 与说明
|
|
153
|
+
- **发送** — 面板底部「发送」按钮:所有记录(批注文字 + 截图图片)推送到 opencode 对话,发送后清空列表
|
|
154
|
+
- 批注和截图**必须填写说明**才能保存/发送,否则弹出提示
|
|
155
|
+
|
|
156
|
+
面板「配置」区:外观主题(跟随系统/浅色/深色)、设备预设下拉(等价 `bt_set_device`)、开发者工具按钮(等价 `bt_devtools`)。
|
|
157
|
+
|
|
158
|
+
## 多会话
|
|
159
|
+
|
|
160
|
+
父子会话共享一个浏览器。开启 `sessionIsolation: true` 后,每个 opencode 会话有独立上下文(cookies/存储隔离)。
|
|
161
|
+
|
|
162
|
+
## 发布
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
cd rust && cargo build --release
|
|
166
|
+
npm publish
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
只发布 `dist/`(`files: ["dist"]`)。
|
|
170
|
+
|
|
171
|
+
## 开发
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
npm install
|
|
175
|
+
cd rust && cargo build
|
|
176
|
+
npm run build
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## 文档
|
|
180
|
+
|
|
181
|
+
- [docs/v5-tauri-architecture.md](./docs/v5-tauri-architecture.md) — V5 当前架构
|
|
182
|
+
- [docs/implementation.md](./docs/implementation.md) — 实施状态
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { spawn } from "child_process";
|
|
2
|
+
import { resolve, dirname } from "path";
|
|
3
|
+
import { existsSync } from "fs";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
import createLogger from "@xiaoqiong0v0/opencode-plugin-logger";
|
|
6
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
/** 就绪轮询超时(ms) */
|
|
8
|
+
const READY_TIMEOUT_MS = 20000;
|
|
9
|
+
/** 就绪轮询间隔(ms) */
|
|
10
|
+
const READY_POLL_INTERVAL_MS = 300;
|
|
11
|
+
// Rust 二进制查找:dist/bin/bt-shell-{platform} 或环境变量覆盖(仅启动模式使用)
|
|
12
|
+
function resolveShellBinary() {
|
|
13
|
+
const platform = process.platform === "win32" ? "win-x64" : "linux-x64";
|
|
14
|
+
const bin = process.platform === "win32" ? "bt-shell.exe" : "bt-shell";
|
|
15
|
+
const cargoTargetDir = process.env.CARGO_TARGET_DIR;
|
|
16
|
+
const candidates = [
|
|
17
|
+
process.env.BT_SHELL_PATH,
|
|
18
|
+
resolve(__dirname, "bin", `bt-shell-${platform}.exe`),
|
|
19
|
+
resolve(__dirname, "bin", `bt-shell-${platform}`),
|
|
20
|
+
resolve(__dirname, "..", "rust", "target", "release", bin),
|
|
21
|
+
resolve(__dirname, "..", "rust", "target", "debug", bin),
|
|
22
|
+
// 尊重自定义 target 目录(WSL 内用 CARGO_TARGET_DIR 构建时)
|
|
23
|
+
cargoTargetDir ? resolve(cargoTargetDir, "release", bin) : undefined,
|
|
24
|
+
cargoTargetDir ? resolve(cargoTargetDir, "debug", bin) : undefined,
|
|
25
|
+
];
|
|
26
|
+
for (const c of candidates) {
|
|
27
|
+
if (c && existsSync(c))
|
|
28
|
+
return c;
|
|
29
|
+
}
|
|
30
|
+
// 默认:开发环境用 cargo target 产物(release 优先)
|
|
31
|
+
return resolve(__dirname, "..", "rust", "target", "release", bin);
|
|
32
|
+
}
|
|
33
|
+
/** 解析附着模式目标地址:BT_SHELL_URL 优先,其次 BT_SHELL_PORT;均未设置返回 null */
|
|
34
|
+
function resolveAttachBaseUrl() {
|
|
35
|
+
const url = process.env.BT_SHELL_URL?.trim();
|
|
36
|
+
if (url)
|
|
37
|
+
return url.replace(/\/+$/, "");
|
|
38
|
+
const port = process.env.BT_SHELL_PORT?.trim();
|
|
39
|
+
if (port)
|
|
40
|
+
return `http://127.0.0.1:${port}`;
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
let serviceProcess = null;
|
|
44
|
+
/** 完整基地址(启动模式 http://127.0.0.1:<port>,附着模式 BT_SHELL_URL) */
|
|
45
|
+
let serviceBaseUrl = "";
|
|
46
|
+
let serviceReady = false;
|
|
47
|
+
/** 附着模式:连接外部已运行 shell,不 spawn 也不 kill */
|
|
48
|
+
let attachMode = false;
|
|
49
|
+
/** 服务启动参数(configureService 保存,ensureService 懒启动时使用) */
|
|
50
|
+
let serviceConfig = null;
|
|
51
|
+
/** 正在启动中的 Promise(并发保护,多个工具同时调用只启动一次) */
|
|
52
|
+
let starting = null;
|
|
53
|
+
/** 保存服务启动参数(插件加载时调用,不立即启动窗口) */
|
|
54
|
+
export function configureService(opts) {
|
|
55
|
+
serviceConfig = opts;
|
|
56
|
+
}
|
|
57
|
+
/** 运行时切换用户数据目录(下次 ensureService 重启时生效) */
|
|
58
|
+
export function setUserDataDir(dir) {
|
|
59
|
+
if (serviceConfig)
|
|
60
|
+
serviceConfig.userDataDir = dir;
|
|
61
|
+
}
|
|
62
|
+
/** 懒启动服务:未启动则 spawn bt-shell(弹窗);已启动直接返回;并发时复用同一个 Promise */
|
|
63
|
+
export async function ensureService() {
|
|
64
|
+
if (serviceReady)
|
|
65
|
+
return;
|
|
66
|
+
if (starting)
|
|
67
|
+
return starting;
|
|
68
|
+
// 附着模式:连接外部已运行的 shell,不 spawn
|
|
69
|
+
const attachUrl = resolveAttachBaseUrl();
|
|
70
|
+
if (attachUrl) {
|
|
71
|
+
starting = attachToService(attachUrl).finally(() => {
|
|
72
|
+
starting = null;
|
|
73
|
+
});
|
|
74
|
+
return starting;
|
|
75
|
+
}
|
|
76
|
+
// 启动模式:懒启动本地 shell
|
|
77
|
+
const cfg = serviceConfig;
|
|
78
|
+
if (!cfg)
|
|
79
|
+
throw new Error("Service not configured");
|
|
80
|
+
starting = startService(cfg.nodePath, cfg.browsersPath, cfg.sessionIsolation, cfg.browserType, cfg.userDataDir).finally(() => {
|
|
81
|
+
starting = null;
|
|
82
|
+
});
|
|
83
|
+
return starting;
|
|
84
|
+
}
|
|
85
|
+
async function callApi(path, body, sessionId) {
|
|
86
|
+
// 懒启动:首次调用工具时自动打开窗口
|
|
87
|
+
await ensureService();
|
|
88
|
+
const payload = { ...(body || {}), _sessionId: sessionId || "" };
|
|
89
|
+
const res = await fetch(`${serviceBaseUrl}${path}`, {
|
|
90
|
+
method: "POST",
|
|
91
|
+
headers: { "Content-Type": "application/json" },
|
|
92
|
+
body: JSON.stringify(payload),
|
|
93
|
+
});
|
|
94
|
+
const json = await res.json();
|
|
95
|
+
if (!json.success)
|
|
96
|
+
throw new Error(json.error || "Service error");
|
|
97
|
+
return json.data;
|
|
98
|
+
}
|
|
99
|
+
/** 轮询等待服务就绪(READY_TIMEOUT_MS 超时);超时抛出给定提示 */
|
|
100
|
+
async function waitForReady(baseUrl, timeoutMessage) {
|
|
101
|
+
const deadline = Date.now() + READY_TIMEOUT_MS;
|
|
102
|
+
while (Date.now() < deadline) {
|
|
103
|
+
try {
|
|
104
|
+
const res = await fetch(`${baseUrl}/api/status`, {
|
|
105
|
+
method: "POST",
|
|
106
|
+
headers: { "Content-Type": "application/json" },
|
|
107
|
+
body: "{}",
|
|
108
|
+
});
|
|
109
|
+
if (res.ok)
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// 服务未启动,继续等待
|
|
114
|
+
}
|
|
115
|
+
await new Promise((r) => setTimeout(r, READY_POLL_INTERVAL_MS));
|
|
116
|
+
}
|
|
117
|
+
throw new Error(timeoutMessage);
|
|
118
|
+
}
|
|
119
|
+
/** 附着到外部已运行的 shell(不 spawn):等待就绪后记录 base URL */
|
|
120
|
+
async function attachToService(baseUrl) {
|
|
121
|
+
const log = createLogger("opencode-browser-tool");
|
|
122
|
+
attachMode = true;
|
|
123
|
+
log.info(`Attaching to existing bt-shell at ${baseUrl} (no spawn)`);
|
|
124
|
+
await waitForReady(baseUrl, `External bt-shell not ready at ${baseUrl}. Check BT_SHELL_URL/BT_SHELL_PORT and that the shell is running.`);
|
|
125
|
+
serviceBaseUrl = baseUrl;
|
|
126
|
+
serviceReady = true;
|
|
127
|
+
log.info(`Attached to existing bt-shell at ${baseUrl}`);
|
|
128
|
+
}
|
|
129
|
+
export async function startService(nodePath, browsersPath, sessionIsolation, browserType, userDataDir) {
|
|
130
|
+
const log = createLogger("opencode-browser-tool");
|
|
131
|
+
attachMode = false;
|
|
132
|
+
const shell = resolveShellBinary();
|
|
133
|
+
// 显式指定端口(release 模式 GUI 程序无控制台,无法解析 stdout)
|
|
134
|
+
const port = 18000 + Math.floor(Math.random() * 1000);
|
|
135
|
+
const args = ["--browsers-path", browsersPath || "", "--port", String(port)];
|
|
136
|
+
if (browserType)
|
|
137
|
+
args.push("--browser", browserType);
|
|
138
|
+
if (sessionIsolation)
|
|
139
|
+
args.push("--session-isolation");
|
|
140
|
+
// 多用户配置:userDataDir 指向独立 WebView2 用户数据目录
|
|
141
|
+
if (userDataDir)
|
|
142
|
+
args.push("--user-data-dir", userDataDir);
|
|
143
|
+
serviceProcess = spawn(shell, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
144
|
+
// 等待服务就绪(轮询端口)
|
|
145
|
+
const baseUrl = `http://127.0.0.1:${port}`;
|
|
146
|
+
await waitForReady(baseUrl, "Service timeout: bt-shell did not start. Build it with: cd rust && cargo build --release");
|
|
147
|
+
serviceBaseUrl = baseUrl;
|
|
148
|
+
serviceReady = true;
|
|
149
|
+
log.info(`Shell service ready on port ${port} (${shell})`);
|
|
150
|
+
}
|
|
151
|
+
export async function stopService() {
|
|
152
|
+
const log = createLogger("opencode-browser-tool");
|
|
153
|
+
if (attachMode) {
|
|
154
|
+
// 附着模式:外部 shell 进程不归插件管,只清空本地状态
|
|
155
|
+
log.info("Detached from external bt-shell (process left running)");
|
|
156
|
+
}
|
|
157
|
+
else if (serviceProcess) {
|
|
158
|
+
try {
|
|
159
|
+
serviceProcess.kill("SIGTERM");
|
|
160
|
+
}
|
|
161
|
+
catch { }
|
|
162
|
+
serviceProcess = null;
|
|
163
|
+
}
|
|
164
|
+
serviceReady = false;
|
|
165
|
+
serviceBaseUrl = "";
|
|
166
|
+
attachMode = false;
|
|
167
|
+
}
|
|
168
|
+
/** 打开浏览器窗口(未启动则启动;已启动幂等) */
|
|
169
|
+
export async function openWindow() {
|
|
170
|
+
await ensureService();
|
|
171
|
+
}
|
|
172
|
+
/** 服务当前是否运行 */
|
|
173
|
+
export function isRunning() {
|
|
174
|
+
return serviceReady;
|
|
175
|
+
}
|
|
176
|
+
function cmd(name) {
|
|
177
|
+
return (params, sessionId) => callApi(`/api/${name}`, params, sessionId);
|
|
178
|
+
}
|
|
179
|
+
export const service = {
|
|
180
|
+
navigate: cmd("navigate"),
|
|
181
|
+
click: cmd("click"),
|
|
182
|
+
fill: cmd("fill"),
|
|
183
|
+
clear: cmd("clear"),
|
|
184
|
+
select: cmd("select"),
|
|
185
|
+
hover: cmd("hover"),
|
|
186
|
+
pressKey: cmd("press-key"),
|
|
187
|
+
drag: cmd("drag"),
|
|
188
|
+
uploadFile: cmd("upload-file"),
|
|
189
|
+
screenshot: cmd("screenshot"),
|
|
190
|
+
evaluate: cmd("evaluate"),
|
|
191
|
+
visibleText: cmd("visible-text"),
|
|
192
|
+
visibleHtml: cmd("visible-html"),
|
|
193
|
+
elementState: cmd("element-state"),
|
|
194
|
+
dropdownOptions: cmd("dropdown-options"),
|
|
195
|
+
waitForSelector: cmd("wait-for-selector"),
|
|
196
|
+
scroll: cmd("scroll"),
|
|
197
|
+
scrollToElement: cmd("scroll-to-element"),
|
|
198
|
+
reload: cmd("reload"),
|
|
199
|
+
goBack: cmd("go-back"),
|
|
200
|
+
goForward: cmd("go-forward"),
|
|
201
|
+
resize: cmd("resize"),
|
|
202
|
+
device: cmd("device"),
|
|
203
|
+
deviceList: cmd("device/list"),
|
|
204
|
+
devtools: cmd("devtools"),
|
|
205
|
+
mediaMode: cmd("media/mode"),
|
|
206
|
+
mediaAudio: cmd("media/audio"),
|
|
207
|
+
mediaVideo: cmd("media/video"),
|
|
208
|
+
close: cmd("close"),
|
|
209
|
+
newTab: cmd("tabs/new"),
|
|
210
|
+
switchTab: cmd("tabs/switch"),
|
|
211
|
+
closeTab: cmd("tabs/close"),
|
|
212
|
+
clickSwitchTab: cmd("click-switch-tab"),
|
|
213
|
+
iframeClick: cmd("iframe-click"),
|
|
214
|
+
iframeFill: cmd("iframe-fill"),
|
|
215
|
+
userAgent: cmd("user-agent"),
|
|
216
|
+
accessibility: cmd("accessibility"),
|
|
217
|
+
notify: cmd("notify"),
|
|
218
|
+
consoleLogs: cmd("console-logs"),
|
|
219
|
+
expectResponse: cmd("expect-response"),
|
|
220
|
+
assertResponse: cmd("assert-response"),
|
|
221
|
+
panelConfig: cmd("panel-config"),
|
|
222
|
+
closeSession: cmd("close-session"),
|
|
223
|
+
status: cmd("status"),
|
|
224
|
+
tabs: cmd("tabs"),
|
|
225
|
+
annotateToggle: cmd("annotate/toggle"),
|
|
226
|
+
annotateRecords: cmd("annotate/records"),
|
|
227
|
+
annotateSend: cmd("annotate/send"),
|
|
228
|
+
annotateConsumeSent: cmd("annotate/consume-sent"),
|
|
229
|
+
};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { readFileSync, existsSync, writeFileSync, mkdirSync } from "fs";
|
|
2
|
+
import { resolve, dirname } from "path";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
const PLUGIN_NAME = "opencode-browser-tool";
|
|
5
|
+
const CONFIG_FILE = "browser-tool.jsonc";
|
|
6
|
+
const defaults = { panelLang: "en", toolLang: "en" };
|
|
7
|
+
let _config = { ...defaults };
|
|
8
|
+
let _worktree = "";
|
|
9
|
+
/** 获取 opencode 全局配置目录 */
|
|
10
|
+
export function getOpenCodeDir() {
|
|
11
|
+
return resolve(homedir(), ".config", "opencode");
|
|
12
|
+
}
|
|
13
|
+
/** 获取 opencode 全局数据目录 */
|
|
14
|
+
export function getOpenCodeDataDir() {
|
|
15
|
+
return resolve(homedir(), ".opencode");
|
|
16
|
+
}
|
|
17
|
+
/** 获取插件数据目录 */
|
|
18
|
+
export function getPluginDataDir() {
|
|
19
|
+
return resolve(getOpenCodeDataDir(), "plugins-data", PLUGIN_NAME);
|
|
20
|
+
}
|
|
21
|
+
/** 获取浏览器缓存目录 */
|
|
22
|
+
export function getBrowsersDir() {
|
|
23
|
+
return _config.browsersPath || resolve(getPluginDataDir(), "browsers");
|
|
24
|
+
}
|
|
25
|
+
/** 插件缓存根目录(用户配置/导出/临时文件,默认 ~/.opencode/plugins-cache/opencode-browser-tool) */
|
|
26
|
+
export function getCacheDir() {
|
|
27
|
+
return _config.cacheDir || resolve(getOpenCodeDataDir(), "plugins-cache", PLUGIN_NAME);
|
|
28
|
+
}
|
|
29
|
+
/** 多用户配置目录(user-data/<profile>,每个 profile 独立 WebView2 用户数据) */
|
|
30
|
+
export function getProfilesDir() {
|
|
31
|
+
return resolve(getCacheDir(), "user-data");
|
|
32
|
+
}
|
|
33
|
+
/** 某个配置的 WebView2 用户数据目录 */
|
|
34
|
+
export function getProfileDir(name) {
|
|
35
|
+
return resolve(getProfilesDir(), name);
|
|
36
|
+
}
|
|
37
|
+
/** 导出目录(用户从 Edge/Chrome 导出的密码 CSV / 书签 HTML) */
|
|
38
|
+
export function getExportsDir() {
|
|
39
|
+
return resolve(getCacheDir(), "exports");
|
|
40
|
+
}
|
|
41
|
+
/** 临时目录(密码临时文件,用完即删) */
|
|
42
|
+
export function getTmpDir() {
|
|
43
|
+
return resolve(getCacheDir(), "tmp");
|
|
44
|
+
}
|
|
45
|
+
/** active profile 状态文件 */
|
|
46
|
+
function activeProfileFile() {
|
|
47
|
+
return resolve(getCacheDir(), "active-profile");
|
|
48
|
+
}
|
|
49
|
+
/** 获取当前激活配置(默认 "default";不存在时回落默认) */
|
|
50
|
+
export function getActiveProfile() {
|
|
51
|
+
try {
|
|
52
|
+
const v = readFileSync(activeProfileFile(), "utf-8").trim();
|
|
53
|
+
if (v)
|
|
54
|
+
return v;
|
|
55
|
+
}
|
|
56
|
+
catch { }
|
|
57
|
+
return "default";
|
|
58
|
+
}
|
|
59
|
+
/** 持久化当前激活配置 */
|
|
60
|
+
export function setActiveProfile(name) {
|
|
61
|
+
mkdirSync(getCacheDir(), { recursive: true });
|
|
62
|
+
writeFileSync(activeProfileFile(), name, "utf-8");
|
|
63
|
+
}
|
|
64
|
+
/** 加载配置:环境变量 > 项目配置 > 全局配置 > 默认值 */
|
|
65
|
+
export function loadConfig(worktree) {
|
|
66
|
+
_worktree = worktree || "";
|
|
67
|
+
const config = { ...defaults };
|
|
68
|
+
const globalPath = resolve(getOpenCodeDir(), CONFIG_FILE);
|
|
69
|
+
if (!existsSync(globalPath))
|
|
70
|
+
generateDefaultConfig(globalPath);
|
|
71
|
+
applyConfigFile(config, globalPath);
|
|
72
|
+
if (_worktree) {
|
|
73
|
+
applyConfigFile(config, resolve(_worktree, ".opencode", CONFIG_FILE));
|
|
74
|
+
}
|
|
75
|
+
applyEnv(config);
|
|
76
|
+
_config = config;
|
|
77
|
+
return config;
|
|
78
|
+
}
|
|
79
|
+
export function getConfig() {
|
|
80
|
+
return _config;
|
|
81
|
+
}
|
|
82
|
+
function applyConfigFile(config, filePath) {
|
|
83
|
+
if (!existsSync(filePath))
|
|
84
|
+
return;
|
|
85
|
+
try {
|
|
86
|
+
const raw = readFileSync(filePath, "utf-8");
|
|
87
|
+
// 剥离注释后可能存在尾逗号(如最后一项有效配置后跟注释行),需容错处理
|
|
88
|
+
const json = raw
|
|
89
|
+
.replace(/\/\/.*$/gm, "")
|
|
90
|
+
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
91
|
+
.replace(/,\s*}/g, "}");
|
|
92
|
+
const fileConfig = JSON.parse(json);
|
|
93
|
+
if (fileConfig.browserType !== undefined)
|
|
94
|
+
config.browserType = fileConfig.browserType;
|
|
95
|
+
if (fileConfig.browsersPath !== undefined)
|
|
96
|
+
config.browsersPath = fileConfig.browsersPath;
|
|
97
|
+
if (fileConfig.nodePath !== undefined)
|
|
98
|
+
config.nodePath = fileConfig.nodePath;
|
|
99
|
+
if (fileConfig.panelLang && isValidLang(fileConfig.panelLang))
|
|
100
|
+
config.panelLang = fileConfig.panelLang;
|
|
101
|
+
if (fileConfig.toolLang && isValidLang(fileConfig.toolLang))
|
|
102
|
+
config.toolLang = fileConfig.toolLang;
|
|
103
|
+
if (fileConfig.disabledTools)
|
|
104
|
+
config.disabledTools = fileConfig.disabledTools;
|
|
105
|
+
if (fileConfig.sessionIsolation !== undefined)
|
|
106
|
+
config.sessionIsolation = fileConfig.sessionIsolation;
|
|
107
|
+
if (fileConfig.cacheDir !== undefined)
|
|
108
|
+
config.cacheDir = fileConfig.cacheDir;
|
|
109
|
+
}
|
|
110
|
+
catch { }
|
|
111
|
+
}
|
|
112
|
+
function applyEnv(config) {
|
|
113
|
+
if (process.env.BT_LANG && isValidLang(process.env.BT_LANG)) {
|
|
114
|
+
config.panelLang = process.env.BT_LANG;
|
|
115
|
+
config.toolLang = process.env.BT_LANG;
|
|
116
|
+
}
|
|
117
|
+
if (process.env.BT_PANEL_LANG && isValidLang(process.env.BT_PANEL_LANG)) {
|
|
118
|
+
config.panelLang = process.env.BT_PANEL_LANG;
|
|
119
|
+
}
|
|
120
|
+
if (process.env.BT_TOOL_LANG && isValidLang(process.env.BT_TOOL_LANG)) {
|
|
121
|
+
config.toolLang = process.env.BT_TOOL_LANG;
|
|
122
|
+
}
|
|
123
|
+
if (process.env.BT_BROWSERS_PATH) {
|
|
124
|
+
config.browsersPath = process.env.BT_BROWSERS_PATH;
|
|
125
|
+
}
|
|
126
|
+
if (process.env.BT_NODE_PATH) {
|
|
127
|
+
config.nodePath = process.env.BT_NODE_PATH;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function isValidLang(lang) {
|
|
131
|
+
return ["en", "zh"].includes(lang);
|
|
132
|
+
}
|
|
133
|
+
function generateDefaultConfig(filePath) {
|
|
134
|
+
try {
|
|
135
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
136
|
+
writeFileSync(filePath, `{
|
|
137
|
+
// 面板 UI 语言: "en" | "zh"(默认 "en")
|
|
138
|
+
"panelLang": "en",
|
|
139
|
+
|
|
140
|
+
// 工具描述语言: "en" | "zh"(默认 "en",用英文可节省 token)
|
|
141
|
+
"toolLang": "en",
|
|
142
|
+
|
|
143
|
+
// 浏览器缓存目录(可选,不设则使用插件数据目录)
|
|
144
|
+
// "browsersPath": "D:/browsers/chromium",
|
|
145
|
+
|
|
146
|
+
// 缓存根目录(可选,默认 ~/.opencode/plugins-cache/opencode-browser-tool)
|
|
147
|
+
// 下设 user-data/<profile> 用户配置、exports 导出、tmp 临时密码文件
|
|
148
|
+
// "cacheDir": "D:/browser-cache",
|
|
149
|
+
|
|
150
|
+
// 禁用的工具列表(可选,工具名不加 bt_ 前缀)
|
|
151
|
+
// "disabledTools": ["close", "custom_user_agent"],
|
|
152
|
+
|
|
153
|
+
// 会话隔离:为子会话创建独立 BrowserContext(默认 false,全部共享)
|
|
154
|
+
// "sessionIsolation": false,
|
|
155
|
+
|
|
156
|
+
// Node.js 路径(可选,不设则从 PATH 查找 node)
|
|
157
|
+
// "nodePath": "C:/Program Files/nodejs/node.exe",
|
|
158
|
+
|
|
159
|
+
// 浏览器类型: "chromium" | "firefox" | "webkit"(默认 chromium)
|
|
160
|
+
// "browserType": "firefox",
|
|
161
|
+
}
|
|
162
|
+
`, "utf-8");
|
|
163
|
+
}
|
|
164
|
+
catch { }
|
|
165
|
+
}
|
package/dist/devices.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内置设备预设表(名称 → User-Agent)
|
|
3
|
+
* 空字符串表示使用浏览器默认 UA(桌面类)
|
|
4
|
+
* 数据为静态硬编码,不依赖 Playwright 运行时
|
|
5
|
+
*/
|
|
6
|
+
export const DEVICES = {
|
|
7
|
+
// 桌面(默认 UA)
|
|
8
|
+
"Desktop Chrome": "",
|
|
9
|
+
"Desktop Edge": "",
|
|
10
|
+
"Desktop Firefox": "",
|
|
11
|
+
"Desktop Safari": "",
|
|
12
|
+
// iPhone
|
|
13
|
+
"iPhone 15 Pro": "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1",
|
|
14
|
+
"iPhone 14": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Mobile/15E148 Safari/604.1",
|
|
15
|
+
"iPhone 13": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1",
|
|
16
|
+
"iPhone 12": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Mobile/15E148 Safari/604.1",
|
|
17
|
+
"iPhone SE (3rd gen)": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1",
|
|
18
|
+
// iPad
|
|
19
|
+
"iPad Pro 11": "Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1",
|
|
20
|
+
"iPad (10th gen)": "Mozilla/5.0 (iPad; CPU OS 16_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Mobile/15E148 Safari/604.1",
|
|
21
|
+
"iPad Mini (6th gen)": "Mozilla/5.0 (iPad; CPU OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1",
|
|
22
|
+
// Android
|
|
23
|
+
"Pixel 7": "Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36",
|
|
24
|
+
"Pixel 6": "Mozilla/5.0 (Linux; Android 13; Pixel 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36",
|
|
25
|
+
"Samsung Galaxy S23": "Mozilla/5.0 (Linux; Android 13; SM-S911B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Mobile Safari/537.36",
|
|
26
|
+
};
|