dsh-annotator 0.0.0-stage → 0.5.5
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 +160 -2
- package/cordis.patch.yml +10 -0
- package/lib/cdp.js +218 -0
- package/lib/client.js +1911 -0
- package/lib/index.js +447 -0
- package/lib/overlay.js +862 -0
- package/lib/session.js +1780 -0
- package/lib/shared/auto-pick.js +29 -0
- package/lib/shared/binding-action.js +26 -0
- package/lib/shared/browsers.js +126 -0
- package/lib/shared/cdp-endpoint.js +70 -0
- package/lib/shared/ego.js +68 -0
- package/lib/shared/embedded-runtime.js +53 -0
- package/lib/shared/gui-page.js +14 -0
- package/lib/shared/host-rev.js +120 -0
- package/lib/shared/pick-actions.js +21 -0
- package/lib/shared/pick-dedupe.js +26 -0
- package/lib/shared/png.js +173 -0
- package/lib/shared/scratch-dir.js +57 -0
- package/lib/shared/spaces.js +54 -0
- package/lib/shared/tabs.js +45 -0
- package/lib/shared/tool-schema.js +72 -0
- package/lib/shared/url.js +33 -0
- package/package.json +53 -5
package/README.md
CHANGED
|
@@ -1,3 +1,161 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-annotator
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/ZhongqiXiao/dsh-annotator/actions/workflows/ci.yml) [](https://www.npmjs.com/package/dsh-annotator) [](https://www.npmjs.com/package/dsh-annotator)
|
|
4
|
+
|
|
5
|
+
一个 [DeepSeek Harness](https://www.deepseek.com) Desktop 插件:在**真实的 Chrome / Edge / ego lite 页面**上拾取、标注、评论、截图页面元素,并把元素定位 + 评论 + 截图一并交给 agent —— 让 agent 按照「你实际看到的页面」做前端可视化修改。
|
|
6
|
+
|
|
7
|
+
类似 ChatGPT / Codex 的内置浏览功能,但驱动的是你自己的浏览器,并深度接入 DSH GUI 的输入框(引用芯片机制)。
|
|
8
|
+
|
|
9
|
+
## 功能特性
|
|
10
|
+
|
|
11
|
+
- **元素拾取**:hover 高亮、点击拾取,拾取层不污染页面样式。
|
|
12
|
+
- **元素级信息**:唯一 CSS selector、页面 URL、坐标与尺寸、文本摘录、元素截图。
|
|
13
|
+
- **评论标注**:每个拾取元素可附评论,连同定位信息一起交给 agent。
|
|
14
|
+
- **引用芯片**:点「插入」后,DSH GUI 输入框里出现 `标注 #pN` 芯片和元素截图附件;提交时自动展开为完整定位信息。芯片管线不可用时自动回退为纯文本插入。
|
|
15
|
+
- **浏览器长命复用**:首次「启动」拉起所选浏览器,之后每次都接回同一个实例——不丢登录态,断开连接也不关窗。默认不接管你的日常 Chrome;ego lite 是例外,它优先接管你已经开着的那个 ego lite,并在自己的「agent 空间」里干活。
|
|
16
|
+
- **一键清空与撤销**:底部「清空」一次清掉全部标注,提示条上的「撤销」可整批恢复。
|
|
17
|
+
- **元素截图**:截图存本地,并作为真实图片附件插入输入框。
|
|
18
|
+
|
|
19
|
+
## 环境要求
|
|
20
|
+
|
|
21
|
+
- Node.js >= 22
|
|
22
|
+
- DeepSeek Harness Desktop,或用 `dsh web` 起的 Web 客户端(两者都是 web 客户端平台)
|
|
23
|
+
- 本机装有 Chrome、Edge 或 [ego lite](https://ego.app) 之一(面板三个都列出来,没装的标「(未安装)」并禁用)
|
|
24
|
+
|
|
25
|
+
## 安装
|
|
26
|
+
|
|
27
|
+
同一个包,桌面端与 Web 端都能装,区别只在装进哪个 profile:Desktop 用 `--profile desktop`,Web 用 `--profile web`(`dsh web` 等价于 `dsh --profile web`)。
|
|
28
|
+
|
|
29
|
+
### 方式 A:插件市场(图形界面)
|
|
30
|
+
|
|
31
|
+
**DSH 自带「插件」页**:侧栏 → **插件** → **添加插件**,输入准确包名 `dsh-annotator` → 安装。装完提供「立即启用」,需要重启时会提示。
|
|
32
|
+
|
|
33
|
+
**社区插件市场**:[dshmarket](https://dshmarket.com)(npm 包 `dshmarket`)收录了本插件,装上之后在市场里搜 `dsh-annotator` 即可安装;插件介绍与收录页见 [awesome-dsh-plugin](https://awesome-dsh-plugin.com/zh/p/ZhongqiXiao/dsh-annotator)。社区市场由第三方维护,收录不代表背书。
|
|
34
|
+
|
|
35
|
+
### 方式 B:命令行从 npm 安装(推荐)
|
|
36
|
+
|
|
37
|
+
包发布在 [npm](https://www.npmjs.com/package/dsh-annotator),要求 Node.js >= 22。
|
|
38
|
+
|
|
39
|
+
Desktop:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
dsh plugin --profile desktop add dsh-annotator
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Web:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
dsh plugin --profile web add dsh-annotator
|
|
49
|
+
dsh web
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
钉版本写成 `dsh-annotator@0.5.4`,从仓库装写成 `github:zhongqixiao/dsh-annotator`。
|
|
53
|
+
|
|
54
|
+
### 方式 C:本地目录(开发模式)
|
|
55
|
+
|
|
56
|
+
把本仓库克隆到任意位置,然后在 `~/.dsh/profiles/desktop/package.json` 中:
|
|
57
|
+
|
|
58
|
+
```jsonc
|
|
59
|
+
{
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"dsh-annotator": "link:C:/path/to/dsh-annotator",
|
|
62
|
+
},
|
|
63
|
+
"dsh": {
|
|
64
|
+
"profile": {
|
|
65
|
+
"bundles": ["dsh-annotator"],
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
改的是哪个 profile 就写哪个目录:桌面端 `~/.dsh/profiles/desktop/`,Web 端 `~/.dsh/profiles/web/`。
|
|
72
|
+
|
|
73
|
+
### 生效与卸载
|
|
74
|
+
|
|
75
|
+
重启 DSH 生效:Desktop 退出后重新打开;Web 端重新跑一次 `dsh web`。GUI 右侧边栏出现「元素标注」面板即安装成功;后续升级只需更新包再重启,面板会自动加载新版。
|
|
76
|
+
|
|
77
|
+
卸载与更新走同一套入口:侧栏「插件」页里可停用、更新、卸载,命令行则用 `dsh plugin --profile desktop remove dsh-annotator`。
|
|
78
|
+
|
|
79
|
+
## 使用流程
|
|
80
|
+
|
|
81
|
+
1. 打开 DSH GUI 右侧边栏的 **元素标注** 面板。
|
|
82
|
+
2. 输入地址(可以像浏览器地址栏那样只写域名:`gitee.com` 自动补成 `https://gitee.com/`,`localhost:5173` 补成 `http://`,带空格的一句会当成搜索词),按需在左侧选择浏览器(选过一次就记住),点击 **启动** —— 第一次会打开所选浏览器的真实窗口,之后都接回同一个实例;新地址在同一窗口新开标签页。选 ego lite 时会优先接管你已经开着的那个 ego lite,并在其中切到「agent 空间」。
|
|
83
|
+
3. 启动成功后**自动进入拾取模式**——打开新网页、切标签页、切空间也一样,不用再点一次;hover 高亮元素,点击完成拾取。面板里把开关**关掉**之后,本次会话内不会再自动开启(手动再打开即恢复自动)。
|
|
84
|
+
4. 为该元素填写评论,点击卡片上的 **插入**(或底部 **全部插入(N 条)**)—— 输入框里出现引用芯片 + 元素截图附件。
|
|
85
|
+
5. 插入的同时,这些标注会从列表里移除(底部提示条可 **撤销** 恢复),所以下一轮标注天然从零开始,不用手动逐条清理。
|
|
86
|
+
6. 正常提交对话;agent 收到完整定位信息,随后即可对照页面修改前端代码。
|
|
87
|
+
|
|
88
|
+
### 浏览器里的快捷键与悬浮菜单
|
|
89
|
+
|
|
90
|
+
调试窗口的每个页面右下角都会注入一个小悬浮层,**不需要安装任何浏览器扩展**:
|
|
91
|
+
|
|
92
|
+
| 位置 / 操作 | 说明 |
|
|
93
|
+
| ---------------------- | ------------------------------------------------------------------------------------------------------- |
|
|
94
|
+
| 右下角胶囊「开启标注」 | 拾取模式关闭时的入口,点它或按快捷键开启 |
|
|
95
|
+
| ⌥ A / Alt A | 在页面里直接开关拾取模式(按物理键位判定,不受输入法/大写影响) |
|
|
96
|
+
| 控制台「已标注 N 处」 | 拾取模式开启后的悬浮控制台:暂停、撤销、完成·插入 |
|
|
97
|
+
| Esc | 取消当前这次拾取 / 退出拾取模式 |
|
|
98
|
+
| ⌘/Ctrl + Enter | 标注卡片内提交(等于点「添加标注」);键帽按系统显示:macOS 是 `⌘` `⏎`,Windows/Linux 是 `Ctrl` `Enter` |
|
|
99
|
+
| Tab | 在标注卡片的输入框与按钮之间移动 |
|
|
100
|
+
| 卡片右上「复制」 | 复制该元素的选择器,直接贴进代码或对话 |
|
|
101
|
+
|
|
102
|
+
标注卡片贴在目标元素旁边(空间不够时翻到上方,小尖角始终指向元素),卡片打开期间目标元素保持高亮;页面滚动或改变窗口大小时,高亮与卡片一起跟随。
|
|
103
|
+
|
|
104
|
+
## 浏览器复用与调试空间
|
|
105
|
+
|
|
106
|
+
### 一个 profile,多次调试
|
|
107
|
+
|
|
108
|
+
1. 第一次「启动」用专用 profile 拉起浏览器。
|
|
109
|
+
2. 之后点「断开连接」(或关掉面板)只断开连接,**窗口、标签页、登录态都留着**;面板显示「已断开 · 浏览器保留」。
|
|
110
|
+
3. 再次「启动」时直接**接回那个实例**,不重开进程,登录态还在;提示「已接回同一个浏览器实例」。
|
|
111
|
+
4. 窗口由 DSH 退出时收尾;想立刻关窗,直接点浏览器的关闭按钮或用系统快捷键即可。
|
|
112
|
+
|
|
113
|
+
新地址一律在同一窗口新开标签页(已开着的同地址则切过去),所以一个实例可以反复调多个页面。
|
|
114
|
+
|
|
115
|
+
### 标签页条
|
|
116
|
+
|
|
117
|
+
面板顶部那条「标签页 N」就是浏览器里真实的那一排,**实时同步**:你在浏览器里自己新开、关掉、改标题的标签页都会立即出现/消失。标题是页面自己写的,常常比标签页晚一步出现——没渲染完时先显示 URL,几百毫秒后自动换成标题。点一下某个标签页 = 切到浏览器前台,之后的标注、截图都从这一页取。一行放不下就换行,最多直接显示 10 个;第 11 个起收进右上角「更多 N」弹层。选 ego lite 时这条只列 **agent 空间**里的页面。
|
|
118
|
+
|
|
119
|
+
### 调试空间(独立登录态,仅 ego lite)
|
|
120
|
+
|
|
121
|
+
**只有选 ego lite 时面板才出现「空间」一栏**:Chrome / Edge 用的是插件专属 profile,本来就是干净环境;ego lite 会和你自己那一屏共存,插件需要一个自己的空间才不抢你的窗口。
|
|
122
|
+
|
|
123
|
+
「空间」一栏可以在**同一个窗口**里另开一份独立登录态:cookie、localStorage、各空间的标签页互不可见。「+」新建并切过去,「×」销毁当前空间(连带它的页面与 cookie)。想登两个账号、或把「调试用账号」和日常登录态隔开时,开一个空间即可。
|
|
124
|
+
|
|
125
|
+
### ego lite:接管你开着的那个实例
|
|
126
|
+
|
|
127
|
+
- **优先接管已运行的实例**:你登录过的站点、书签都还在,不用再登一次。`DSH_VISUAL_ANNOTATOR_ADOPT_RUNNING=0` 可关掉这条,改成自己起一个新实例。
|
|
128
|
+
- **在「agent 空间」里干活**:接管之后,插件会建/复用一个名为 `agent 空间` 的空间并切过去,它自己的标签页、cookie 与你当前那一屏互不打扰。
|
|
129
|
+
- **关掉新手引导**:全新 profile 的 ego lite 会开一个引导窗口挡住用户,插件启动后自动关掉它。
|
|
130
|
+
|
|
131
|
+
只有你**显式**设了 `DSH_VISUAL_ANNOTATOR_CDP_URL` 时,插件才会去连一个**别的**外部浏览器;Chrome 与 Edge 不会自动探测常见调试端口,也就不会悄悄接管你的日常 Chrome。
|
|
132
|
+
|
|
133
|
+
## 环境变量
|
|
134
|
+
|
|
135
|
+
| 变量 | 作用 |
|
|
136
|
+
| ------------------------------------------------ | -------------------------------------------------------------- |
|
|
137
|
+
| `DSH_VISUAL_ANNOTATOR_CHROME` / `_EDGE` / `_EGO` | 覆盖对应浏览器的可执行文件路径 |
|
|
138
|
+
| `DSH_VISUAL_ANNOTATOR_WINDOW` | 标注窗口尺寸,如 `1440x900`(DIP);不设则最大化启动 |
|
|
139
|
+
| `DSH_VISUAL_ANNOTATOR_HEADLESS` | `1` 时无头启动,仅供插件自测 |
|
|
140
|
+
| `DSH_VISUAL_ANNOTATOR_BROWSER_ARGS` | 额外追加给浏览器的启动开关(逃生口,平时不需要) |
|
|
141
|
+
| `DSH_VISUAL_ANNOTATOR_SANDBOX_FALLBACK` | 设 `0` 关掉「沙箱崩溃后自动改用 `--no-sandbox` 重试一次」 |
|
|
142
|
+
| `DSH_VISUAL_ANNOTATOR_ADOPT_RUNNING` | 设 `0` 不接管你已开着的 ego lite,改为自己起一个新实例 |
|
|
143
|
+
| `DSH_VISUAL_ANNOTATOR_AUTO_PICK` | 设 `0` 关掉「启动 / 打开网页自动进入标注模式」,改成每次手动开 |
|
|
144
|
+
| `DSH_VISUAL_ANNOTATOR_DIR` | 覆盖临时文件根目录(默认系统临时目录下的同名目录) |
|
|
145
|
+
| `DSH_VISUAL_ANNOTATOR_CDP_URL` | 可选:改为连接该调试端点上的外部浏览器,如 `127.0.0.1:9222` |
|
|
146
|
+
|
|
147
|
+
插件**不往代码仓写文件**:浏览器 profile 与元素截图都在系统临时目录(macOS `$TMPDIR`、Linux `/tmp`、Windows `%TEMP%`)下的 `dsh-visual-annotator/`。面板带上会话 cwd 时,落点进一步细分到 `<项目名>-<cwd 哈希前 8 位>/`:不同项目各用一份 profile 与截图目录,不会串登录态,也不会互相覆盖。系统重启或临时目录清理后会自然消失,无需手动删除。
|
|
148
|
+
|
|
149
|
+
环境变量由 host 进程读取,改完需重启 DSH Desktop。
|
|
150
|
+
|
|
151
|
+
## Agent 侧
|
|
152
|
+
|
|
153
|
+
插件向 agent 注册 `visual_annotator_read` 工具:传入 `ids` 可按标注 id 精确读取,不传则返回当前全部标注(selector、URL、坐标尺寸、文本摘录、评论、截图路径)。收到 `标注 #pN` 引用时务必调用该工具获取完整上下文。
|
|
154
|
+
|
|
155
|
+
## 参与贡献
|
|
156
|
+
|
|
157
|
+
欢迎提 Issue 与 Pull Request。开发环境配置、架构说明、代码规范与发版流程见 [`CONTRIBUTING.md`](CONTRIBUTING.md);面向 AI 代理的协作约定见 [`AGENTS.md`](AGENTS.md)。
|
|
158
|
+
|
|
159
|
+
## License
|
|
160
|
+
|
|
161
|
+
MIT
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# dsh-annotator bundle layer.
|
|
2
|
+
#
|
|
3
|
+
# The loader row specifier MUST equal the package.json `name`: the client-modules
|
|
4
|
+
# scan matches the nearest manifest name against the Loader specifier, so an alias
|
|
5
|
+
# here would silently classify the entry as "not a client row" and the Web panel
|
|
6
|
+
# would never load while the host routes kept working.
|
|
7
|
+
- insert:
|
|
8
|
+
- id: dsh-annotator
|
|
9
|
+
name: 'dsh-annotator'
|
|
10
|
+
config: {}
|
package/lib/cdp.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal Chrome DevTools Protocol client speaking the `--remote-debugging-pipe`
|
|
3
|
+
* transport: NUL-terminated JSON over fd 3 (out) / fd 4 (in).
|
|
4
|
+
*
|
|
5
|
+
* Why the pipe and not WebSocket: no ws dependency, no port to collide with,
|
|
6
|
+
* and Chrome hands us a private channel for exactly this child process.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export class CdpPipe {
|
|
10
|
+
/** @param {import('node:child_process').ChildProcess} proc */
|
|
11
|
+
constructor(proc, label = 'browser') {
|
|
12
|
+
this.label = label;
|
|
13
|
+
this.proc = proc;
|
|
14
|
+
this.nextId = 1;
|
|
15
|
+
this.pending = new Map();
|
|
16
|
+
this.handlers = new Set();
|
|
17
|
+
this.buffer = Buffer.alloc(0);
|
|
18
|
+
this.closed = false;
|
|
19
|
+
this.out = proc.stdio?.[3];
|
|
20
|
+
this.inc = proc.stdio?.[4];
|
|
21
|
+
if (!this.out || !this.inc) throw new Error('remote-debugging-pipe stdio fds are missing');
|
|
22
|
+
this.out.on('error', (err) => this._failAll(err));
|
|
23
|
+
this.inc.on('data', (chunk) => this._onData(chunk));
|
|
24
|
+
this.inc.on('error', (err) => this._failAll(err));
|
|
25
|
+
this.inc.on('close', () => this._failAll(new Error(`${this.label} pipe closed`)));
|
|
26
|
+
proc.once('exit', (code, signal) => {
|
|
27
|
+
this.closed = true;
|
|
28
|
+
this._failAll(new Error(`${this.label} exited (code=${code} signal=${signal})`));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_onData(chunk) {
|
|
33
|
+
this.buffer = this.buffer.length === 0 ? chunk : Buffer.concat([this.buffer, chunk]);
|
|
34
|
+
for (;;) {
|
|
35
|
+
const end = this.buffer.indexOf(0);
|
|
36
|
+
if (end < 0) break;
|
|
37
|
+
const text = this.buffer.subarray(0, end).toString('utf8');
|
|
38
|
+
this.buffer = this.buffer.subarray(end + 1);
|
|
39
|
+
if (!text.trim()) continue;
|
|
40
|
+
let message;
|
|
41
|
+
try {
|
|
42
|
+
message = JSON.parse(text);
|
|
43
|
+
} catch {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
this._dispatch(message);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_dispatch(message) {
|
|
51
|
+
if (message.id !== undefined && this.pending.has(message.id)) {
|
|
52
|
+
const entry = this.pending.get(message.id);
|
|
53
|
+
this.pending.delete(message.id);
|
|
54
|
+
if (message.error) {
|
|
55
|
+
const err = new Error(`${entry.method}: ${message.error.message ?? 'protocol error'}`);
|
|
56
|
+
err.cdp = message.error;
|
|
57
|
+
entry.reject(err);
|
|
58
|
+
} else {
|
|
59
|
+
entry.resolve(message.result ?? {});
|
|
60
|
+
}
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
for (const handler of this.handlers) {
|
|
64
|
+
try {
|
|
65
|
+
handler(message);
|
|
66
|
+
} catch {
|
|
67
|
+
/* a broken listener must not break the transport */
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
send(method, params = {}, sessionId) {
|
|
73
|
+
if (this.closed) return Promise.reject(new Error(`${this.label} connection is closed`));
|
|
74
|
+
const id = this.nextId++;
|
|
75
|
+
const payload = { id, method, params };
|
|
76
|
+
if (sessionId) payload.sessionId = sessionId;
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
this.pending.set(id, { resolve, reject, method });
|
|
79
|
+
try {
|
|
80
|
+
this.out.write(JSON.stringify(payload) + '\u0000');
|
|
81
|
+
} catch (err) {
|
|
82
|
+
this.pending.delete(id);
|
|
83
|
+
reject(err);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
onEvent(handler) {
|
|
89
|
+
this.handlers.add(handler);
|
|
90
|
+
return () => this.handlers.delete(handler);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
_failAll(err) {
|
|
94
|
+
for (const entry of this.pending.values()) entry.reject(err);
|
|
95
|
+
this.pending.clear();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
close() {
|
|
99
|
+
this.closed = true;
|
|
100
|
+
this._failAll(new Error(`${this.label} connection closed`));
|
|
101
|
+
try {
|
|
102
|
+
this.proc.kill();
|
|
103
|
+
} catch {
|
|
104
|
+
/* already gone */
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* WebSocket transport for `--remote-debugging-port=0`. Same interface as
|
|
111
|
+
* CdpPipe, but it can also attach to a browser this process did not spawn:
|
|
112
|
+
* Chrome writes the port + browser ws path into <profile>/DevToolsActivePort,
|
|
113
|
+
* so after a host restart (or a singleton handoff) we reconnect to the SAME
|
|
114
|
+
* window instead of spawning a duplicate one.
|
|
115
|
+
*/
|
|
116
|
+
export class CdpWs {
|
|
117
|
+
/** @param {string} wsUrl e.g. ws://127.0.0.1:PORT/devtools/browser/<uuid> */
|
|
118
|
+
constructor(wsUrl, label = 'browser') {
|
|
119
|
+
this.label = label;
|
|
120
|
+
this.proc = null; // adopted browsers have no child process of ours
|
|
121
|
+
this.nextId = 1;
|
|
122
|
+
this.pending = new Map();
|
|
123
|
+
this.handlers = new Set();
|
|
124
|
+
this.closed = false;
|
|
125
|
+
this.ws = new WebSocket(wsUrl);
|
|
126
|
+
this.open = new Promise((resolve, reject) => {
|
|
127
|
+
this.ws.addEventListener('open', () => resolve());
|
|
128
|
+
this.ws.addEventListener('error', () => reject(new Error(`${label} websocket failed to connect`)));
|
|
129
|
+
setTimeout(() => reject(new Error(`${label} websocket connect timeout`)), 8000);
|
|
130
|
+
});
|
|
131
|
+
this.open.catch(() => {});
|
|
132
|
+
this.ws.addEventListener('message', async (event) => {
|
|
133
|
+
let text = event.data;
|
|
134
|
+
if (typeof text !== 'string') {
|
|
135
|
+
try {
|
|
136
|
+
text = Buffer.from(text instanceof Blob ? await text.arrayBuffer() : text).toString('utf8');
|
|
137
|
+
} catch {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
let message;
|
|
142
|
+
try {
|
|
143
|
+
message = JSON.parse(text);
|
|
144
|
+
} catch {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
this._dispatch(message);
|
|
148
|
+
});
|
|
149
|
+
this.ws.addEventListener('close', () => {
|
|
150
|
+
this.closed = true;
|
|
151
|
+
this._failAll(new Error(`${this.label} connection closed`));
|
|
152
|
+
});
|
|
153
|
+
this.ws.addEventListener('error', () => {
|
|
154
|
+
if (!this.closed) {
|
|
155
|
+
this.closed = true;
|
|
156
|
+
this._failAll(new Error(`${this.label} websocket error`));
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
_dispatch(message) {
|
|
162
|
+
if (message.id !== undefined && this.pending.has(message.id)) {
|
|
163
|
+
const entry = this.pending.get(message.id);
|
|
164
|
+
this.pending.delete(message.id);
|
|
165
|
+
if (message.error) {
|
|
166
|
+
const err = new Error(`${entry.method}: ${message.error.message ?? 'protocol error'}`);
|
|
167
|
+
err.cdp = message.error;
|
|
168
|
+
entry.reject(err);
|
|
169
|
+
} else {
|
|
170
|
+
entry.resolve(message.result ?? {});
|
|
171
|
+
}
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
for (const handler of this.handlers) {
|
|
175
|
+
try {
|
|
176
|
+
handler(message);
|
|
177
|
+
} catch {
|
|
178
|
+
/* a broken listener must not break the transport */
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
send(method, params = {}, sessionId) {
|
|
184
|
+
if (this.closed) return Promise.reject(new Error(`${this.label} connection is closed`));
|
|
185
|
+
const id = this.nextId++;
|
|
186
|
+
const payload = { id, method, params };
|
|
187
|
+
if (sessionId) payload.sessionId = sessionId;
|
|
188
|
+
return new Promise((resolve, reject) => {
|
|
189
|
+
this.pending.set(id, { resolve, reject, method });
|
|
190
|
+
try {
|
|
191
|
+
this.ws.send(JSON.stringify(payload));
|
|
192
|
+
} catch (err) {
|
|
193
|
+
this.pending.delete(id);
|
|
194
|
+
reject(err);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
onEvent(handler) {
|
|
200
|
+
this.handlers.add(handler);
|
|
201
|
+
return () => this.handlers.delete(handler);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
_failAll(err) {
|
|
205
|
+
for (const entry of this.pending.values()) entry.reject(err);
|
|
206
|
+
this.pending.clear();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
close() {
|
|
210
|
+
this.closed = true;
|
|
211
|
+
this._failAll(new Error(`${this.label} connection closed`));
|
|
212
|
+
try {
|
|
213
|
+
this.ws.close();
|
|
214
|
+
} catch {
|
|
215
|
+
/* already gone */
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|