dsh-annotator 0.0.0-stage → 0.5.4
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 +159 -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 +1745 -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 +52 -5
package/lib/session.js
ADDED
|
@@ -0,0 +1,1745 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Owns one visible Chrome/Edge window driven over the CDP pipe, keeps the picks
|
|
3
|
+
* the page overlay reports, and writes element screenshots to disk.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { execFileSync, spawn } from 'node:child_process';
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import os from 'node:os';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
|
|
11
|
+
import { CdpWs } from './cdp.js';
|
|
12
|
+
import { BINDING_NAME, OVERLAY_SCRIPT } from './overlay.js';
|
|
13
|
+
import { classifyBindingAction } from './shared/binding-action.js';
|
|
14
|
+
import {
|
|
15
|
+
ADOPT_RUNNING_ENV,
|
|
16
|
+
BROWSER_ARGS_ENV,
|
|
17
|
+
BROWSER_ENV,
|
|
18
|
+
BROWSER_IDS,
|
|
19
|
+
BROWSER_LABELS,
|
|
20
|
+
browserLabel,
|
|
21
|
+
defaultBrowserId,
|
|
22
|
+
normalizeBrowserId,
|
|
23
|
+
parseExtraArgs,
|
|
24
|
+
SANDBOX_FALLBACK_ENV,
|
|
25
|
+
shouldTrySandboxFallback,
|
|
26
|
+
} from './shared/browsers.js';
|
|
27
|
+
import { externalCandidates, parseVersionReply } from './shared/cdp-endpoint.js';
|
|
28
|
+
import { EGO_AGENT_SPACE_NAME, isEgoBrowserCommand, isEgoOnboardingTarget, listeningPortsByPid } from './shared/ego.js';
|
|
29
|
+
import { advancePendingConsume, popLastPick } from './shared/pick-actions.js';
|
|
30
|
+
import { isDuplicatePick, pickSignature } from './shared/pick-dedupe.js';
|
|
31
|
+
import { cropPng, readPngSize } from './shared/png.js';
|
|
32
|
+
import { isGuiPage } from './shared/gui-page.js';
|
|
33
|
+
import { scratchDir } from './shared/scratch-dir.js';
|
|
34
|
+
import { mergeTab } from './shared/tabs.js';
|
|
35
|
+
import { normalizeAddress } from './shared/url.js';
|
|
36
|
+
import {
|
|
37
|
+
DEFAULT_SPACE_NAME,
|
|
38
|
+
filterTabsBySpace,
|
|
39
|
+
nextSpaceName,
|
|
40
|
+
normalizeSpaceId,
|
|
41
|
+
sanitizeSpaceName,
|
|
42
|
+
} from './shared/spaces.js';
|
|
43
|
+
|
|
44
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
45
|
+
|
|
46
|
+
/** How many consumed/cleared picks keep answering /pick before they fall off. */
|
|
47
|
+
const MAX_RETIRED = 200;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Executable candidates per platform, most likely first. The per-browser env
|
|
51
|
+
* override (BROWSER_ENV) is prepended in candidatesFor(), so all three browsers
|
|
52
|
+
* accept a custom path on every platform instead of only on Windows.
|
|
53
|
+
*/
|
|
54
|
+
const CANDIDATES = {
|
|
55
|
+
win32: {
|
|
56
|
+
chrome: [
|
|
57
|
+
'C:/Program Files/Google/Chrome/Application/chrome.exe',
|
|
58
|
+
'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe',
|
|
59
|
+
path.join(process.env.LOCALAPPDATA ?? '', 'Google/Chrome/Application/chrome.exe'),
|
|
60
|
+
],
|
|
61
|
+
edge: [
|
|
62
|
+
'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe',
|
|
63
|
+
'C:/Program Files/Microsoft/Edge/Application/msedge.exe',
|
|
64
|
+
],
|
|
65
|
+
// ego lite 的 Windows 安装位(Program Files 与 per-user 安装都覆盖)。
|
|
66
|
+
ego: [
|
|
67
|
+
'C:/Program Files/ego lite/ego lite.exe',
|
|
68
|
+
'C:/Program Files (x86)/ego lite/ego lite.exe',
|
|
69
|
+
path.join(process.env.LOCALAPPDATA ?? '', 'Programs/ego lite/ego lite.exe'),
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
darwin: {
|
|
73
|
+
chrome: ['/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'],
|
|
74
|
+
edge: [
|
|
75
|
+
'/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
|
|
76
|
+
path.join(os.homedir(), 'Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge'),
|
|
77
|
+
],
|
|
78
|
+
// ego lite 的外层二进制只是个 launcher,真正的 Chromium 在 Framework 里;
|
|
79
|
+
// 直接启动外层就行——它照常处理 --user-data-dir / --remote-debugging-port=0。
|
|
80
|
+
ego: [
|
|
81
|
+
'/Applications/ego lite.app/Contents/MacOS/ego lite',
|
|
82
|
+
path.join(os.homedir(), 'Applications/ego lite.app/Contents/MacOS/ego lite'),
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
linux: {
|
|
86
|
+
chrome: [
|
|
87
|
+
'/usr/bin/google-chrome',
|
|
88
|
+
'/usr/bin/google-chrome-stable',
|
|
89
|
+
'/usr/bin/chromium',
|
|
90
|
+
'/usr/bin/chromium-browser',
|
|
91
|
+
],
|
|
92
|
+
edge: ['/usr/bin/microsoft-edge', '/usr/bin/microsoft-edge-stable'],
|
|
93
|
+
ego: ['/usr/bin/ego-lite', '/usr/share/ego-lite/ego-lite', '/opt/ego lite/ego lite'],
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
function candidatesFor(browser) {
|
|
98
|
+
const table = CANDIDATES[process.platform] ?? CANDIDATES.linux;
|
|
99
|
+
const defaults = table[browser] ?? [];
|
|
100
|
+
const override = (process.env[BROWSER_ENV[browser]] ?? '').trim();
|
|
101
|
+
return override ? [override, ...defaults] : defaults;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function resolveBrowserExecutable(browser = 'chrome') {
|
|
105
|
+
for (const candidate of candidatesFor(normalizeBrowserId(browser))) {
|
|
106
|
+
if (!candidate) continue;
|
|
107
|
+
try {
|
|
108
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
109
|
+
} catch {
|
|
110
|
+
/* keep looking */
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function availableBrowsers() {
|
|
117
|
+
const out = {};
|
|
118
|
+
for (const browser of BROWSER_IDS) out[browser] = resolveBrowserExecutable(browser);
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 请求没点名浏览器时的缺省:第一个装了的(面板与裸 /launch 走同一条规则)。 */
|
|
123
|
+
export function defaultBrowser(available = availableBrowsers()) {
|
|
124
|
+
return defaultBrowserId(available) ?? 'chrome';
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 新 profile 的第一扇窗口由 Chrome 自己定尺寸:在 4K / 高缩放的桌面上它会开成一块
|
|
129
|
+
* 又窄又矮的小窗,用户看到的「整个浏览器都被缩小」往往就是这扇窗自身尺寸偏小。
|
|
130
|
+
* 标注窗口是专用目标,所以默认最大化;要精确尺寸用 DSH_VISUAL_ANNOTATOR_WINDOW=1440x900
|
|
131
|
+
* 覆盖(--window-size 的单位是 DIP,分隔符是逗号不是 x)。
|
|
132
|
+
*/
|
|
133
|
+
function windowArgs() {
|
|
134
|
+
const size = (process.env.DSH_VISUAL_ANNOTATOR_WINDOW ?? '').trim();
|
|
135
|
+
if (/^\d+[x,]\d+$/i.test(size)) return [`--window-size=${size.replace(/[x,]/i, ',')}`];
|
|
136
|
+
return ['--start-maximized'];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Ask one candidate base for its browser-level ws endpoint. Anything slower than
|
|
141
|
+
* a beat counts as "no browser there": a firewall-dropped port must not make
|
|
142
|
+
* `launch` look hung.
|
|
143
|
+
*/
|
|
144
|
+
async function fetchCdpEndpoint(base) {
|
|
145
|
+
const controller = new AbortController();
|
|
146
|
+
const timer = setTimeout(() => controller.abort(), 600);
|
|
147
|
+
try {
|
|
148
|
+
const response = await fetch(base + '/json/version', { signal: controller.signal });
|
|
149
|
+
if (!response.ok) return null;
|
|
150
|
+
return parseVersionReply(await response.text(), base);
|
|
151
|
+
} catch {
|
|
152
|
+
return null;
|
|
153
|
+
} finally {
|
|
154
|
+
clearTimeout(timer);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export class VisualPicker {
|
|
159
|
+
constructor(options = {}) {
|
|
160
|
+
// Scratch files (browser profile + element shots) are temp data: they belong
|
|
161
|
+
// in the OS temp directory, never in the user's workspace. DSH_VISUAL_ANNOTATOR_DIR
|
|
162
|
+
// moves the root (e.g. onto a bigger disk); options.baseDir does the same for tests.
|
|
163
|
+
const envRoot = (process.env.DSH_VISUAL_ANNOTATOR_DIR ?? '').trim();
|
|
164
|
+
this.tmpDir = envRoot || path.join(os.tmpdir(), 'dsh-visual-annotator');
|
|
165
|
+
this.baseDir = options.baseDir ?? this.tmpDir;
|
|
166
|
+
this.activeBaseDir = null;
|
|
167
|
+
this.profileDir = null;
|
|
168
|
+
this.profilePerLaunch = false;
|
|
169
|
+
this.cleanProfile = null;
|
|
170
|
+
this.state = {
|
|
171
|
+
status: 'idle',
|
|
172
|
+
error: null,
|
|
173
|
+
browser: null,
|
|
174
|
+
executable: null,
|
|
175
|
+
activeUrl: '',
|
|
176
|
+
activeTitle: '',
|
|
177
|
+
mode: 'off',
|
|
178
|
+
picks: [],
|
|
179
|
+
// Monotonic token bumped when the in-page HUD asks the panel to insert
|
|
180
|
+
// everything; the panel compares tokens so one request inserts exactly once.
|
|
181
|
+
pendingConsume: 0,
|
|
182
|
+
tabs: [],
|
|
183
|
+
// 宿主自带沙箱时 Chrome 的浏览器进程会立刻 SIGTRAP;这时我们会带
|
|
184
|
+
// --no-sandbox 重试一次,面板据此提示「本机浏览器在无沙箱模式下运行」。
|
|
185
|
+
sandboxFallback: false,
|
|
186
|
+
};
|
|
187
|
+
this.cdp = null;
|
|
188
|
+
this.proc = null;
|
|
189
|
+
this.adopted = false;
|
|
190
|
+
// 见 shouldTrySandboxFallback:只在「我们 spawn 的进程崩了」时置位,
|
|
191
|
+
// 置位后不再重复尝试,避免崩溃时来回重启。
|
|
192
|
+
this.useNoSandbox = false;
|
|
193
|
+
this.log = options.log ?? null;
|
|
194
|
+
// external = 显式 DSH_VISUAL_ANNOTATOR_CDP_URL 连上的外部浏览器(不是我们启动的实例):
|
|
195
|
+
// close() 只能断开,绝不能 Browser.close。默认永远不探测,免得接管用户日常 Chrome。
|
|
196
|
+
this.external = false;
|
|
197
|
+
this.attachBase = null;
|
|
198
|
+
// 复用用户已经开着的 ego lite(见 _tryAttachRunningEgo)。默认开;测试与
|
|
199
|
+
// 「别碰我日常浏览器」场景可用 DSH_VISUAL_ANNOTATOR_ADOPT_RUNNING=0 关掉。
|
|
200
|
+
this.adoptRunning = options.adoptRunning ?? process.env[ADOPT_RUNNING_ENV] !== '0';
|
|
201
|
+
// 窗口与 profile 都留在磁盘上反复用:断开只丢 socket,下次 launch 按这个目录重连。
|
|
202
|
+
this.windowKept = false;
|
|
203
|
+
this.windowProfileDir = null;
|
|
204
|
+
this.attached = new Map();
|
|
205
|
+
this.activeTargetId = null;
|
|
206
|
+
// 调试空间 = Chromium BrowserContext(与 ego lite 的 task space 同一原语):
|
|
207
|
+
// 同一进程、同一 profile,但 cookie / storage / 标签页各自独立。'' = 默认空间,
|
|
208
|
+
// 也就是浏览器启动时那个 context,它的 id 只有连上之后才知道。
|
|
209
|
+
this.spaces = [];
|
|
210
|
+
this.defaultContextId = '';
|
|
211
|
+
this.activeSpace = '';
|
|
212
|
+
// /state 轮询顺带核对标签页标题/URL 的节流时间戳。
|
|
213
|
+
this.tabsRefreshedAt = 0;
|
|
214
|
+
this.seq = 0;
|
|
215
|
+
this.lastRemoved = null;
|
|
216
|
+
// Picks already inserted or cleared: gone from the list, but still read by
|
|
217
|
+
// /pick — an inserted chip expands minutes later. Their screenshots only die
|
|
218
|
+
// when the archive overflows or the window closes.
|
|
219
|
+
this.retired = [];
|
|
220
|
+
this.listeners = new Set();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
onChange(listener) {
|
|
224
|
+
this.listeners.add(listener);
|
|
225
|
+
return () => this.listeners.delete(listener);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
_emit() {
|
|
229
|
+
for (const listener of this.listeners) {
|
|
230
|
+
try {
|
|
231
|
+
listener(this.snapshot());
|
|
232
|
+
} catch {
|
|
233
|
+
/* a listener must not break the picker */
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
snapshot() {
|
|
239
|
+
// 每次快照都探测一遍:装/卸载浏览器后不用重启 host,面板下一次轮询就能看到。
|
|
240
|
+
const browsers = availableBrowsers();
|
|
241
|
+
return {
|
|
242
|
+
status: this.state.status,
|
|
243
|
+
error: this.state.error,
|
|
244
|
+
browser: this.state.browser,
|
|
245
|
+
executable: this.state.executable,
|
|
246
|
+
activeUrl: this.state.activeUrl,
|
|
247
|
+
activeTitle: this.state.activeTitle,
|
|
248
|
+
mode: this.state.mode,
|
|
249
|
+
// 面板据此把「关闭浏览器」改成「断开连接」,并说明当前窗口是复用的。
|
|
250
|
+
reused: this.external,
|
|
251
|
+
cdpBase: this.attachBase,
|
|
252
|
+
// 断开后窗口还在(同一 profile 的同一个实例),面板据此提示「浏览器保留」。
|
|
253
|
+
windowKept: this.windowKept,
|
|
254
|
+
// 这次 launch 是接回已有 profile 的实例(而不是新起进程),面板据此说明。
|
|
255
|
+
resumed: this.adopted,
|
|
256
|
+
// 宿主沙箱导致浏览器只能以 --no-sandbox 运行(见 shouldTrySandboxFallback):
|
|
257
|
+
// 面板与 agent 都能看出「为什么这个窗口没沙箱」,而不是默默降级。
|
|
258
|
+
sandboxFallback: this.useNoSandbox,
|
|
259
|
+
baseDir: this.activeBaseDir ?? this.baseDir,
|
|
260
|
+
browsers,
|
|
261
|
+
// 注册表由 host 单向下发:面板据此渲染浏览器选择器,两半不会各写一份 id。
|
|
262
|
+
browserOrder: BROWSER_IDS,
|
|
263
|
+
browserLabels: BROWSER_LABELS,
|
|
264
|
+
browserDefault: defaultBrowserId(browsers),
|
|
265
|
+
// 调试空间:面板据此渲染空间选择器;默认空间恒在首位,'' 是它的 id。
|
|
266
|
+
spaces: this.spaceList(),
|
|
267
|
+
space: normalizeSpaceId(this.activeSpace),
|
|
268
|
+
spaceName:
|
|
269
|
+
this.spaceList().find((space) => space.id === normalizeSpaceId(this.activeSpace))?.name ?? DEFAULT_SPACE_NAME,
|
|
270
|
+
// 只列当前空间的标签页:另一个空间开着的页面不该出现在这一格的标签条里。
|
|
271
|
+
tabs: filterTabsBySpace(this.state.tabs, this.activeSpace, this.defaultContextId).map((tab) => ({
|
|
272
|
+
...tab,
|
|
273
|
+
active: tab.targetId === this.activeTargetId,
|
|
274
|
+
})),
|
|
275
|
+
picks: this.state.picks.map((pick) => this._publicPick(pick)),
|
|
276
|
+
pendingConsume: this.state.pendingConsume,
|
|
277
|
+
// Inserted picks leave the list but stay resolvable: their chips expand at
|
|
278
|
+
// submit time, which can be minutes after the insert.
|
|
279
|
+
archived: this.retired.length,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** 面板要的空间清单:默认空间永远在第一项。 */
|
|
284
|
+
spaceList() {
|
|
285
|
+
return [{ id: '', name: DEFAULT_SPACE_NAME }, ...this.spaces.map((space) => ({ ...space }))];
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** 当前空间的 context id;默认空间在连上浏览器后才知道(可能暂时是 '')。 */
|
|
289
|
+
_spaceKey() {
|
|
290
|
+
const id = normalizeSpaceId(this.activeSpace);
|
|
291
|
+
return id || normalizeSpaceId(this.defaultContextId);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** 某个 context 里的页面目标(默认空间用 _spaceKey 的回落)。 */
|
|
295
|
+
async _spacePages(contextId) {
|
|
296
|
+
const key = normalizeSpaceId(contextId);
|
|
297
|
+
const result = await this.cdp.send('Target.getTargets').catch(() => ({ targetInfos: [] }));
|
|
298
|
+
return (result.targetInfos ?? []).filter(
|
|
299
|
+
(info) =>
|
|
300
|
+
info.type === 'page' &&
|
|
301
|
+
!String(info.url).startsWith('devtools://') &&
|
|
302
|
+
normalizeSpaceId(info.browserContextId) === key,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** The one public shape of a pick, shared by /state, /pick and the agent tool. */
|
|
307
|
+
_publicPick(pick) {
|
|
308
|
+
return {
|
|
309
|
+
id: pick.id,
|
|
310
|
+
seq: pick.seq,
|
|
311
|
+
comment: pick.comment,
|
|
312
|
+
selector: pick.selector,
|
|
313
|
+
tag: pick.tag,
|
|
314
|
+
text: pick.text,
|
|
315
|
+
html: pick.html,
|
|
316
|
+
rect: pick.rect,
|
|
317
|
+
pageRect: pick.pageRect,
|
|
318
|
+
url: pick.url,
|
|
319
|
+
title: pick.title,
|
|
320
|
+
ts: pick.ts,
|
|
321
|
+
shotPath: pick.shotPath ?? null,
|
|
322
|
+
shotUrl: pick.shotPath ? `/api/visual-annotator/shot?id=${encodeURIComponent(pick.id)}` : null,
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Resolve one pick by id, live or already inserted. A chip inserted in round 1 is
|
|
328
|
+
* still submitted later, so "not in the list" must not mean "gone".
|
|
329
|
+
*/
|
|
330
|
+
findPick(id) {
|
|
331
|
+
const key = String(id ?? '');
|
|
332
|
+
if (!key) return null;
|
|
333
|
+
const match = (pick) => String(pick.id) === key;
|
|
334
|
+
const hit = this.state.picks.find(match) ?? this.retired.find(match);
|
|
335
|
+
return hit ? this._publicPick(hit) : null;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Everything still worth reading: live picks first, then the ones already inserted. */
|
|
339
|
+
readablePicks() {
|
|
340
|
+
const bySeq = (a, b) => (a.seq ?? 0) - (b.seq ?? 0) || String(a.id).localeCompare(String(b.id));
|
|
341
|
+
return [...this.state.picks, ...this.retired].sort(bySeq).map((pick) => this._publicPick(pick));
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Scratch base dir for one launch: the project's subdirectory under the temp
|
|
346
|
+
* root when a workspace cwd is known, else the root itself (null = keep the
|
|
347
|
+
* shared root, which also means the profile survives close()).
|
|
348
|
+
*/
|
|
349
|
+
_scratchBase(cwd) {
|
|
350
|
+
return cwd && path.isAbsolute(cwd) ? scratchDir(this.baseDir, cwd) : null;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
get running() {
|
|
354
|
+
return this.state.status === 'ready' && this.cdp !== null && !this.cdp.closed;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
async launch({ url, browser, cwd, cleanProfile } = {}) {
|
|
358
|
+
// 面板里敲的是"地址":只写 gitee.com 也得能打开,不能交给 Target.createTarget
|
|
359
|
+
// 自己猜(它猜不出来就开一页 about:blank)。
|
|
360
|
+
url = normalizeAddress(url);
|
|
361
|
+
if (this.running) {
|
|
362
|
+
// 点名换了浏览器就真的换:profile 与进程都不同,只导航是换不过去的。
|
|
363
|
+
// 旧窗口在这里关掉(profile 保留),否则每换一次就多留一个窗口。
|
|
364
|
+
const next = browser === undefined ? null : normalizeBrowserId(browser, this.state.browser ?? 'chrome');
|
|
365
|
+
if (next && next !== this.state.browser) {
|
|
366
|
+
await this.close({ terminate: true });
|
|
367
|
+
} else {
|
|
368
|
+
// Relaunching with no (or a blank) URL must not wipe the page the
|
|
369
|
+
// user is annotating; an explicit URL still navigates.
|
|
370
|
+
if (url && url !== 'about:blank') await this.navigate(url);
|
|
371
|
+
return this.snapshot();
|
|
372
|
+
}
|
|
373
|
+
} else {
|
|
374
|
+
await this.close();
|
|
375
|
+
}
|
|
376
|
+
// Per-launch base dir: never mutate the default, or one session's cwd
|
|
377
|
+
// would leak its shots/profile into later launches. close() restores it.
|
|
378
|
+
// The cwd only *names* the scratch subdirectory (profile + shots under the
|
|
379
|
+
// OS temp root), so projects keep separate browser profiles without the
|
|
380
|
+
// plugin ever writing into the workspace.
|
|
381
|
+
this.activeBaseDir = this._scratchBase(cwd);
|
|
382
|
+
// 空间属于这一轮浏览器进程:换浏览器/重启后旧 context 已经不存在,
|
|
383
|
+
// 留着它们只会让面板列出一堆点不开的空间。
|
|
384
|
+
this.spaces = [];
|
|
385
|
+
this.defaultContextId = '';
|
|
386
|
+
this.activeSpace = '';
|
|
387
|
+
this.tabsRefreshedAt = 0;
|
|
388
|
+
this.profilePerLaunch = false;
|
|
389
|
+
this.profileDir = null;
|
|
390
|
+
this.cleanProfile = cleanProfile === undefined ? null : cleanProfile === true;
|
|
391
|
+
// 截图目录要先建好:无论走「接外部窗口」还是「自己起窗口」,_capture 都写这里。
|
|
392
|
+
fs.mkdirSync(path.join(this.activeBaseDir ?? this.baseDir, 'shots'), { recursive: true });
|
|
393
|
+
// 没点名(旧的 /launch 调用、或面板只发 url)时用第一个装了的浏览器,
|
|
394
|
+
// 这样只装了 Edge / ego lite 的机器不会一上来就报「未找到 chrome」。
|
|
395
|
+
const available = availableBrowsers();
|
|
396
|
+
const browserId = normalizeBrowserId(browser, defaultBrowserId(available) ?? 'chrome');
|
|
397
|
+
this.state.status = 'starting';
|
|
398
|
+
this.state.error = null;
|
|
399
|
+
this.state.browser = browserId;
|
|
400
|
+
this.state.executable = null;
|
|
401
|
+
this._emit();
|
|
402
|
+
// 显式配置了 DSH_VISUAL_ANNOTATOR_CDP_URL 才去连「外部浏览器」;默认不探测任何
|
|
403
|
+
// 端口——9222 上很可能就是用户的日常 Chrome,插件不该接管它。插件自己的窗口
|
|
404
|
+
// 不靠端口发现,而是走下面的 profile 目录重连(同一实例反复用)。
|
|
405
|
+
if (await this._tryAttachExternal(browserId)) {
|
|
406
|
+
this.state.status = 'ready';
|
|
407
|
+
this.state.error = null;
|
|
408
|
+
this._emit();
|
|
409
|
+
await this._syncSpaces();
|
|
410
|
+
// ego:接管来的窗口是用户自己的,插件只在独立空间里开页,不动他当前那屏。
|
|
411
|
+
if (browserId === 'ego') await this._ensureAgentSpace(browserId, url);
|
|
412
|
+
else if (url && url !== 'about:blank') await this.navigate(url);
|
|
413
|
+
return this.snapshot();
|
|
414
|
+
}
|
|
415
|
+
// ego lite 优先复用用户已经开着的实例:既不会被新手引导挡住,也不会在用户
|
|
416
|
+
// 面前多起一个带空 profile 的窗口。找不到再走下面自己启动的路。
|
|
417
|
+
if (browserId === 'ego' && (await this._tryAttachRunningEgo(browserId))) {
|
|
418
|
+
this.state.status = 'ready';
|
|
419
|
+
this.state.error = null;
|
|
420
|
+
this._emit();
|
|
421
|
+
// 用户自己的窗口不关引导:他要是正开着「欢迎使用 ego」,关掉就是抢他的窗口。
|
|
422
|
+
await this._ensureAgentSpace(browserId, url);
|
|
423
|
+
return this.snapshot();
|
|
424
|
+
}
|
|
425
|
+
const executable = available[browserId] ?? resolveBrowserExecutable(browserId);
|
|
426
|
+
if (!executable) {
|
|
427
|
+
this.state.status = 'error';
|
|
428
|
+
this.state.error = `未找到 ${browserLabel(browserId)} 可执行文件(可设置 ${BROWSER_ENV[browserId]} 指定路径)`;
|
|
429
|
+
this._emit();
|
|
430
|
+
return this.snapshot();
|
|
431
|
+
}
|
|
432
|
+
// 一个(项目 × 浏览器)只对应一个 profile:它跨 close() 保留,下次 launch 按目录
|
|
433
|
+
// 重连同一个实例,登录态与标签页都不丢——不再每次开一扇带空 profile 的新窗。
|
|
434
|
+
this.profilePerLaunch = false;
|
|
435
|
+
const baseDir = this.activeBaseDir ?? this.baseDir;
|
|
436
|
+
const profileDir = path.join(baseDir, `profile-${browserId}`);
|
|
437
|
+
fs.mkdirSync(profileDir, { recursive: true });
|
|
438
|
+
this.profileDir = profileDir;
|
|
439
|
+
this.state.executable = executable;
|
|
440
|
+
this._emit();
|
|
441
|
+
// A browser for this profile may already be alive (host restart, earlier
|
|
442
|
+
// panel session). Adopt it instead of spawning a duplicate: Chrome holds
|
|
443
|
+
// a per-profile singleton, so a second spawn would just hand its URL to
|
|
444
|
+
// the old instance and die with "pipe closed" — the duplicate-window bug.
|
|
445
|
+
if (await this._tryAdopt(profileDir, browserId)) {
|
|
446
|
+
this.state.status = 'ready';
|
|
447
|
+
await this._syncSpaces();
|
|
448
|
+
if (browserId === 'ego') {
|
|
449
|
+
await this._closeEgoOnboarding();
|
|
450
|
+
await this._ensureAgentSpace(browserId, url);
|
|
451
|
+
} else if (url && url !== 'about:blank') await this.navigate(url);
|
|
452
|
+
this._emit();
|
|
453
|
+
return this.snapshot();
|
|
454
|
+
}
|
|
455
|
+
this._retried = false;
|
|
456
|
+
const headless = process.env.DSH_VISUAL_ANNOTATOR_HEADLESS === '1';
|
|
457
|
+
const args = [
|
|
458
|
+
'--remote-debugging-port=0',
|
|
459
|
+
'--no-first-run',
|
|
460
|
+
'--no-default-browser-check',
|
|
461
|
+
'--disable-background-timer-throttling',
|
|
462
|
+
'--user-data-dir=' + profileDir,
|
|
463
|
+
];
|
|
464
|
+
// Escapes for a sandboxed host: see parseExtraArgs. Placed before the window
|
|
465
|
+
// flags so an explicit `--headless=new` there still wins for one run.
|
|
466
|
+
args.push(...parseExtraArgs(process.env[BROWSER_ARGS_ENV]));
|
|
467
|
+
// Armed only after a real crash was observed (see shouldTrySandboxFallback),
|
|
468
|
+
// never up front: a host that is not sandboxed keeps Chrome's own sandbox.
|
|
469
|
+
if (this.useNoSandbox) args.push('--no-sandbox');
|
|
470
|
+
// Headless is only for the plugin's own smoke test; the product is a visible window.
|
|
471
|
+
if (headless) args.push('--headless=new', '--disable-gpu');
|
|
472
|
+
// A headless run has no window to size, so only a visible launch earns the window flags.
|
|
473
|
+
else args.push(...windowArgs());
|
|
474
|
+
args.push('about:blank');
|
|
475
|
+
let proc;
|
|
476
|
+
try {
|
|
477
|
+
proc = spawn(executable, args, { stdio: ['ignore', 'ignore', 'pipe'], windowsHide: false });
|
|
478
|
+
} catch (error) {
|
|
479
|
+
this.state.status = 'error';
|
|
480
|
+
this.state.error = String(error?.message ?? error);
|
|
481
|
+
this._emit();
|
|
482
|
+
return this.snapshot();
|
|
483
|
+
}
|
|
484
|
+
proc.once('error', (error) => {
|
|
485
|
+
this.state.status = 'error';
|
|
486
|
+
this.state.error = String(error?.message ?? error);
|
|
487
|
+
this._emit();
|
|
488
|
+
});
|
|
489
|
+
proc.stderr?.on('data', () => {
|
|
490
|
+
/* keep stderr drained so Chrome never blocks on it */
|
|
491
|
+
});
|
|
492
|
+
this.exitPromise = new Promise((resolve) => {
|
|
493
|
+
proc.once('exit', () => resolve('exited'));
|
|
494
|
+
setTimeout(() => resolve('timeout'), 8000);
|
|
495
|
+
});
|
|
496
|
+
proc.once('exit', () => {
|
|
497
|
+
if (this.cdp?.proc === proc) {
|
|
498
|
+
this.cdp = null;
|
|
499
|
+
this.proc = null;
|
|
500
|
+
this.attached.clear();
|
|
501
|
+
this.state.status = 'idle';
|
|
502
|
+
this.state.activeUrl = '';
|
|
503
|
+
this.state.activeTitle = '';
|
|
504
|
+
this.windowKept = false;
|
|
505
|
+
this._emit();
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
this.proc = proc;
|
|
509
|
+
try {
|
|
510
|
+
try {
|
|
511
|
+
await this._connectWithRetry(profileDir, browserId, 12000);
|
|
512
|
+
} catch (error) {
|
|
513
|
+
// 连不上且进程已死,多半就是沙箱问题:先试回退,不行再把原错误抛出去。
|
|
514
|
+
const crashedRetry = await this._retryWithoutSandbox({ proc, browserId, url, cwd });
|
|
515
|
+
if (crashedRetry) return crashedRetry;
|
|
516
|
+
throw error;
|
|
517
|
+
}
|
|
518
|
+
// 宿主自带沙箱时,握手成功之后浏览器进程才会 SIGTRAP 消失,所以这里
|
|
519
|
+
// 必须真的探一次活,而不是相信 connect 成功。
|
|
520
|
+
const earlyRetry = await this._retryWithoutSandbox({ proc, browserId, url, cwd });
|
|
521
|
+
if (earlyRetry) return earlyRetry;
|
|
522
|
+
this.cdp.onEvent((message) => this._onCdpEvent(message));
|
|
523
|
+
this.launchUrl = url || 'about:blank';
|
|
524
|
+
const pages = await this._waitForPage();
|
|
525
|
+
const first = pages[0];
|
|
526
|
+
await this._attach(first.targetId);
|
|
527
|
+
// Reuse the blank tab Chrome opened with; anything else opens a new
|
|
528
|
+
// tab in the SAME window via navigate()'s multi-tab semantics.
|
|
529
|
+
// ego 例外:它的调试页只进独立空间,默认空间保持用户原样。
|
|
530
|
+
if (url && url !== 'about:blank' && browserId !== 'ego') {
|
|
531
|
+
if (first.url === 'about:blank') {
|
|
532
|
+
const sessionId = this.attached.get(first.targetId);
|
|
533
|
+
await this.cdp.send('Page.navigate', { url }, sessionId).catch(() => {});
|
|
534
|
+
this.state.activeUrl = url;
|
|
535
|
+
await sleep(300);
|
|
536
|
+
await this._syncActiveTarget(first.targetId, sessionId);
|
|
537
|
+
} else {
|
|
538
|
+
await this.navigate(url);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
this.state.status = 'ready';
|
|
542
|
+
this.state.error = null;
|
|
543
|
+
this.windowKept = true;
|
|
544
|
+
this.windowProfileDir = profileDir;
|
|
545
|
+
await this._seedTabs();
|
|
546
|
+
await this._syncSpaces();
|
|
547
|
+
// ego:新手引导窗口在这时候已经弹出来了,先关掉,再把调试页开进独立空间。
|
|
548
|
+
if (browserId === 'ego') {
|
|
549
|
+
await this._closeEgoOnboarding();
|
|
550
|
+
await this._ensureAgentSpace(browserId, url);
|
|
551
|
+
}
|
|
552
|
+
// 第二道检查:Chrome 在宿主沙箱里往往撑一两秒才被 SIGTRAP 带走,刚才
|
|
553
|
+
// 那次探活可能恰好赶在它死之前,所以「已经是能用的状态」之前再看一次。
|
|
554
|
+
const lateRetry = await this._retryWithoutSandbox({ proc, browserId, url, cwd });
|
|
555
|
+
if (lateRetry) return lateRetry;
|
|
556
|
+
this._emit();
|
|
557
|
+
} catch (error) {
|
|
558
|
+
// 沙箱回退优先:无论失败发生在哪一步,只要浏览器进程/连接已经死了,
|
|
559
|
+
// 带 --no-sandbox 重来一次就是最有希望的那条路,别让它等满重试超时。
|
|
560
|
+
const crashRetry = await this._retryWithoutSandbox({ proc, browserId, url, cwd });
|
|
561
|
+
if (crashRetry) return crashRetry;
|
|
562
|
+
const message = String(error?.message ?? error);
|
|
563
|
+
if (!this._retried && /pipe closed|exited|closed|timeout|websocket|DevToolsActivePort/i.test(message)) {
|
|
564
|
+
// Two recoverable cases: (1) a singleton handoff — our spawn
|
|
565
|
+
// forwarded the URL to a live instance and exited, and that
|
|
566
|
+
// instance owns a debug port we can adopt; (2) a lingering
|
|
567
|
+
// profile lock from an instance that just died — retry spawn.
|
|
568
|
+
this._retried = true;
|
|
569
|
+
await sleep(800);
|
|
570
|
+
if (await this._tryAdopt(profileDir, browserId)) {
|
|
571
|
+
this._retried = false;
|
|
572
|
+
this.state.status = 'ready';
|
|
573
|
+
if (url && url !== 'about:blank') await this.navigate(url);
|
|
574
|
+
this._emit();
|
|
575
|
+
return this.snapshot();
|
|
576
|
+
}
|
|
577
|
+
await this.close({ terminate: true });
|
|
578
|
+
await sleep(1500);
|
|
579
|
+
return this.launch({ url, browser: browserId, cwd });
|
|
580
|
+
}
|
|
581
|
+
this._retried = false;
|
|
582
|
+
this.state.status = 'error';
|
|
583
|
+
this.state.error = message;
|
|
584
|
+
this._emit();
|
|
585
|
+
}
|
|
586
|
+
return this.snapshot();
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Reuse a browser that is already running with a debugging port — the user's
|
|
591
|
+
* own window or one left over from an earlier run. This is the "do not open
|
|
592
|
+
* yet another browser" path: connect, attach its tabs, and leave the process
|
|
593
|
+
* alone (close() only drops the socket for external browsers).
|
|
594
|
+
*/
|
|
595
|
+
async _tryAttachExternal(browserId) {
|
|
596
|
+
for (const base of externalCandidates(process.env)) {
|
|
597
|
+
if (await this._adoptBase(base, browserId)) return true;
|
|
598
|
+
}
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* 用户自己开着的 ego lite 也会在 127.0.0.1 上监听一个标准 CDP 端口(随机分配,
|
|
604
|
+
* 没有端口文件),所以「复用已有实例」= 找出 ego 主进程的监听端口,再按外部
|
|
605
|
+
* 浏览器接管。只在本机 macOS / Linux 上做;Windows 缺 pgrep/lsof 这套组合,
|
|
606
|
+
* 退回自己启动一个。
|
|
607
|
+
*
|
|
608
|
+
* 注意取进程的方式:宿主沙箱里 `ps` 会被拒(spawnSync ps EPERM)、`lsof -p <pid>`
|
|
609
|
+
* 也拿不到别人的进程,但 `pgrep -fl` 与整表 `lsof -iTCP -sTCP:LISTEN` 可用,
|
|
610
|
+
* 所以走「pgrep 拿 pid + 整表查端口」,两步都失败就当作没有可复用的实例。
|
|
611
|
+
*/
|
|
612
|
+
egoEndpoints() {
|
|
613
|
+
if (process.platform === 'win32') return [];
|
|
614
|
+
let listing;
|
|
615
|
+
try {
|
|
616
|
+
listing = execFileSync('pgrep', ['-fl', 'ego lite'], { encoding: 'utf8', timeout: 4000 });
|
|
617
|
+
} catch {
|
|
618
|
+
return []; // pgrep 没匹配时退出码 1,属正常
|
|
619
|
+
}
|
|
620
|
+
const pids = new Set();
|
|
621
|
+
for (const line of listing.split('\n')) {
|
|
622
|
+
const match = /^\s*(\d+)\s+(.*)$/.exec(line);
|
|
623
|
+
if (!match) continue;
|
|
624
|
+
const pid = Number(match[1]);
|
|
625
|
+
if (pid === this.proc?.pid || !isEgoBrowserCommand(match[2])) continue;
|
|
626
|
+
pids.add(pid);
|
|
627
|
+
}
|
|
628
|
+
if (pids.size === 0) return [];
|
|
629
|
+
let portsByPid;
|
|
630
|
+
try {
|
|
631
|
+
portsByPid = listeningPortsByPid(
|
|
632
|
+
execFileSync('lsof', ['-nP', '-F', '-iTCP', '-sTCP:LISTEN'], { encoding: 'utf8', timeout: 6000 }),
|
|
633
|
+
);
|
|
634
|
+
} catch {
|
|
635
|
+
return [];
|
|
636
|
+
}
|
|
637
|
+
const bases = [];
|
|
638
|
+
for (const pid of pids) {
|
|
639
|
+
for (const port of portsByPid.get(pid) ?? []) bases.push(`http://127.0.0.1:${port}`);
|
|
640
|
+
}
|
|
641
|
+
return [...new Set(bases)];
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** 复用用户已经开着的 ego lite:有实例就接管,没有就返回 false 让调用方启动。 */
|
|
645
|
+
async _tryAttachRunningEgo(browserId) {
|
|
646
|
+
if (!this.adoptRunning) return false; // 测试与「别抢我日常浏览器」场景
|
|
647
|
+
for (const base of this.egoEndpoints()) {
|
|
648
|
+
if (await this._adoptBase(base, browserId)) return true;
|
|
649
|
+
}
|
|
650
|
+
return false;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/** 接管一个已经在监听 CDP 的窗口:连上、接上它的标签页、不碰它的进程。 */
|
|
654
|
+
async _adoptBase(base, browserId) {
|
|
655
|
+
const endpoint = await fetchCdpEndpoint(base);
|
|
656
|
+
if (!endpoint) return false;
|
|
657
|
+
let cdp = null;
|
|
658
|
+
try {
|
|
659
|
+
cdp = new CdpWs(endpoint, browserId || 'browser');
|
|
660
|
+
await cdp.open;
|
|
661
|
+
const version = await cdp.send('Browser.getVersion');
|
|
662
|
+
this.cdp = cdp;
|
|
663
|
+
this.external = true;
|
|
664
|
+
this.adopted = true;
|
|
665
|
+
this.proc = null;
|
|
666
|
+
this.exitPromise = null;
|
|
667
|
+
this.attachBase = base;
|
|
668
|
+
// Edge 的 product 里带 Edg/,能认出来;ego lite 报的是 Chrome/…,与 Chrome 无从区分,
|
|
669
|
+
// 这时保留调用方点的那个 id,而不是把它一律改写回 chrome。
|
|
670
|
+
if (/Edg\//.test(String(version?.product ?? ''))) this.state.browser = 'edge';
|
|
671
|
+
else if (!this.state.browser) this.state.browser = browserId || 'chrome';
|
|
672
|
+
this.state.executable = null;
|
|
673
|
+
this.cdp.onEvent((message) => this._onCdpEvent(message));
|
|
674
|
+
const pages = await this._waitForPage(6000);
|
|
675
|
+
for (const page of pages) await this._attach(page.targetId).catch(() => {});
|
|
676
|
+
const preferred = this._pickInitialPage(pages);
|
|
677
|
+
if (preferred) {
|
|
678
|
+
this.activeTargetId = preferred.targetId;
|
|
679
|
+
this.state.activeUrl = preferred.url ?? '';
|
|
680
|
+
this.state.activeTitle = preferred.title ?? '';
|
|
681
|
+
}
|
|
682
|
+
await this._seedTabs();
|
|
683
|
+
return true;
|
|
684
|
+
} catch {
|
|
685
|
+
if (this.cdp === cdp) this.cdp = null;
|
|
686
|
+
this.external = false;
|
|
687
|
+
this.attachBase = null;
|
|
688
|
+
try {
|
|
689
|
+
cdp?.close();
|
|
690
|
+
} catch {
|
|
691
|
+
/* already gone */
|
|
692
|
+
}
|
|
693
|
+
return false;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/** ego lite 全新 profile 会弹出新手引导窗口挡住用户;连上后把那一扇关掉。 */
|
|
698
|
+
async _closeEgoOnboarding() {
|
|
699
|
+
if (!this.cdp) return;
|
|
700
|
+
try {
|
|
701
|
+
const { targetInfos = [] } = await this.cdp.send('Target.getTargets');
|
|
702
|
+
for (const info of targetInfos) {
|
|
703
|
+
if (!isEgoOnboardingTarget(info)) continue;
|
|
704
|
+
await this.cdp.send('Target.closeTarget', { targetId: info.targetId }).catch(() => {});
|
|
705
|
+
}
|
|
706
|
+
} catch {
|
|
707
|
+
/* 没有引导窗口(或老版本不认这个 URL)时静默 */
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/** 把浏览器里已经存在的 BrowserContext 认回来,重连后切空间仍然有效。 */
|
|
712
|
+
async _syncSpaces() {
|
|
713
|
+
if (!this.cdp) return;
|
|
714
|
+
let contextIds = [];
|
|
715
|
+
try {
|
|
716
|
+
const reply = await this.cdp.send('Target.getBrowserContexts');
|
|
717
|
+
contextIds = reply?.browserContextIds ?? [];
|
|
718
|
+
} catch {
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
let targets = [];
|
|
722
|
+
try {
|
|
723
|
+
const reply = await this.cdp.send('Target.getTargets');
|
|
724
|
+
targets = reply?.targetInfos ?? [];
|
|
725
|
+
} catch {
|
|
726
|
+
/* 认不出目标就只按 context 列表对齐 */
|
|
727
|
+
}
|
|
728
|
+
// 名字要跨重连保住:context id 每次重连都在,但「哪个是 agent 空间」只有
|
|
729
|
+
// 落盘的注册表知道。否则上次的 agent 空间会被当成别人建的空间改名成
|
|
730
|
+
// 「空间」,再新建一个空的「agent 空间」,页面就找不回来了。
|
|
731
|
+
this._loadSpaces();
|
|
732
|
+
const live = new Set(contextIds.filter(Boolean));
|
|
733
|
+
this.spaces = this.spaces.filter((space) => live.has(space.id));
|
|
734
|
+
for (const id of live) {
|
|
735
|
+
if (this.spaces.some((space) => space.id === id)) continue;
|
|
736
|
+
this.spaces.push({ id, name: nextSpaceName(this.spaces.map((space) => space.name)) });
|
|
737
|
+
}
|
|
738
|
+
// 默认空间的 context id 没有 API 直给,但它有两个特征:带着页面、不在
|
|
739
|
+
// Target.getBrowserContexts 的列表里。按这个差别认,比「第一个挂上的目标」
|
|
740
|
+
// 稳——ego 启动后会直奔 agent 空间,先挂上的往往是空间页而不是默认页。
|
|
741
|
+
const known = new Set(this.spaces.map((space) => space.id));
|
|
742
|
+
const fallback = targets.map((info) => normalizeSpaceId(info.browserContextId)).find((id) => id && !known.has(id));
|
|
743
|
+
if (fallback) this.defaultContextId = fallback;
|
|
744
|
+
this._saveSpaces();
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/** 空间注册表落盘位置:跟 profile 一样按项目 + 浏览器分开,互不串。 */
|
|
748
|
+
_spacesFile() {
|
|
749
|
+
const base = this.activeBaseDir ?? this.baseDir;
|
|
750
|
+
if (!base) return null;
|
|
751
|
+
return path.join(base, `spaces-${this.state.browser ?? 'chrome'}.json`);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
_loadSpaces() {
|
|
755
|
+
const file = this._spacesFile();
|
|
756
|
+
if (!file) return [];
|
|
757
|
+
try {
|
|
758
|
+
const parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
759
|
+
if (Array.isArray(parsed?.spaces)) {
|
|
760
|
+
this.spaces = parsed.spaces
|
|
761
|
+
.filter((space) => space && typeof space.id === 'string' && space.id)
|
|
762
|
+
.map((space) => ({ id: space.id, name: typeof space.name === 'string' && space.name ? space.name : '空间' }));
|
|
763
|
+
}
|
|
764
|
+
} catch {
|
|
765
|
+
/* 没有注册表(首次启动)或内容坏了:按空表处理 */
|
|
766
|
+
}
|
|
767
|
+
return this.spaces;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
_saveSpaces() {
|
|
771
|
+
const file = this._spacesFile();
|
|
772
|
+
if (!file) return;
|
|
773
|
+
try {
|
|
774
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
775
|
+
fs.writeFileSync(file, JSON.stringify({ spaces: this.spaces, defaultContextId: this.defaultContextId }, null, 2));
|
|
776
|
+
} catch {
|
|
777
|
+
/* 落盘失败只影响重连后的名字,不该打断启动 */
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* ego lite 专属:插件只在自己的空间里干活,绝不混进用户当前那个空间。
|
|
783
|
+
* ego 的 TaskSpace 本身就是 Chromium BrowserContext,所以「单独一个 space」
|
|
784
|
+
* 用同一个原语实现:复用空着的 context 或新建一个,再在里面开页。
|
|
785
|
+
*/
|
|
786
|
+
async _ensureAgentSpace(browserId, url) {
|
|
787
|
+
if (browserId !== 'ego' || !this.running) return;
|
|
788
|
+
await this._syncSpaces();
|
|
789
|
+
const target = url && url !== 'about:blank' ? url : undefined;
|
|
790
|
+
const existing = this.spaces.find((space) => space.name === EGO_AGENT_SPACE_NAME);
|
|
791
|
+
try {
|
|
792
|
+
if (existing) await this.useSpace(existing.id, target);
|
|
793
|
+
else await this.createSpace({ name: EGO_AGENT_SPACE_NAME, url: target });
|
|
794
|
+
} catch (error) {
|
|
795
|
+
// 空间只影响「调试页开在哪」,不该让整个 launch 变成失败:记一条日志即可。
|
|
796
|
+
this.log?.('warn', `ego 独立空间创建失败,退回默认空间:${String(error?.message ?? error)}`);
|
|
797
|
+
await this.useSpace('', target).catch(() => {});
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/** The tab a fresh connection should show: last real page, never the DSH GUI. */
|
|
802
|
+
_pickInitialPage(pages) {
|
|
803
|
+
const list = pages ?? [];
|
|
804
|
+
const reversed = [...list].reverse();
|
|
805
|
+
return (
|
|
806
|
+
reversed.find((page) => page.url && page.url !== 'about:blank' && !this._isGuiPage(page.url, page.title)) ??
|
|
807
|
+
reversed.find((page) => page.url && page.url !== 'about:blank') ??
|
|
808
|
+
list[0] ??
|
|
809
|
+
null
|
|
810
|
+
);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/** Connect to an already-running browser that uses this exact profile dir. */
|
|
814
|
+
async _tryAdopt(profileDir, browser) {
|
|
815
|
+
const endpoint = await this._readDevToolsEndpoint(profileDir, 0);
|
|
816
|
+
if (!endpoint) return false;
|
|
817
|
+
try {
|
|
818
|
+
const cdp = new CdpWs(endpoint, browser);
|
|
819
|
+
await cdp.open;
|
|
820
|
+
await cdp.send('Browser.getVersion');
|
|
821
|
+
await this.close();
|
|
822
|
+
this.profileDir = profileDir;
|
|
823
|
+
// profileDir 是 <scratch>/profile-<browser>,dirname 就是这份临时目录本身:
|
|
824
|
+
// 截图与 profile 始终落在同一层,close() 清掉 activeBaseDir 后靠它恢复。
|
|
825
|
+
this.activeBaseDir = path.dirname(profileDir);
|
|
826
|
+
this.cdp = cdp;
|
|
827
|
+
this.adopted = true;
|
|
828
|
+
this.proc = null;
|
|
829
|
+
this.exitPromise = null;
|
|
830
|
+
this.windowKept = true;
|
|
831
|
+
this.windowProfileDir = profileDir;
|
|
832
|
+
this.cdp.onEvent((message) => this._onCdpEvent(message));
|
|
833
|
+
const pages = await this._waitForPage(6000);
|
|
834
|
+
for (const page of pages) await this._attach(page.targetId).catch(() => {});
|
|
835
|
+
const preferred = this._pickInitialPage(pages);
|
|
836
|
+
if (preferred) {
|
|
837
|
+
this.activeTargetId = preferred.targetId;
|
|
838
|
+
this.state.activeUrl = preferred.url ?? '';
|
|
839
|
+
this.state.activeTitle = preferred.title ?? '';
|
|
840
|
+
}
|
|
841
|
+
await this._seedTabs();
|
|
842
|
+
return true;
|
|
843
|
+
} catch {
|
|
844
|
+
return false;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
/**
|
|
849
|
+
* Poll <profileDir>/DevToolsActivePort and open a verified browser WS
|
|
850
|
+
* connection. The file may hold a stale port from a previous run (the
|
|
851
|
+
* profile survives close() when it is not per-launch), so every candidate
|
|
852
|
+
* endpoint is verified with Browser.getVersion before being accepted.
|
|
853
|
+
*/
|
|
854
|
+
async _connectWithRetry(profileDir, browser, maxWaitMs) {
|
|
855
|
+
const deadline = Date.now() + maxWaitMs;
|
|
856
|
+
let lastPort = null;
|
|
857
|
+
for (;;) {
|
|
858
|
+
// 进程已经没了就没必要再等满 12s:宿主沙箱把浏览器带走时,调试端口
|
|
859
|
+
// 可能刚好写完又被删掉,干等只会让用户看着「启动中」发呆。
|
|
860
|
+
if (this.proc && (this.proc.exitCode !== null || this.proc.signalCode)) {
|
|
861
|
+
throw new Error(`浏览器进程退出(${this.proc.signalCode ?? this.proc.exitCode})`);
|
|
862
|
+
}
|
|
863
|
+
const endpoint = await this._readDevToolsEndpoint(profileDir, 250);
|
|
864
|
+
if (endpoint) {
|
|
865
|
+
const port = endpoint.match(/^ws:\/\/127\.0\.0\.1:(\d+)\//)?.[1] ?? null;
|
|
866
|
+
if (port && port !== lastPort) {
|
|
867
|
+
lastPort = port;
|
|
868
|
+
try {
|
|
869
|
+
const cdp = new CdpWs(endpoint, browser);
|
|
870
|
+
await cdp.open;
|
|
871
|
+
await cdp.send('Browser.getVersion');
|
|
872
|
+
this.cdp = cdp;
|
|
873
|
+
return;
|
|
874
|
+
} catch {
|
|
875
|
+
/* stale or half-written endpoint; keep polling */
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
if (Date.now() > deadline) throw new Error('未能在 profile 中获得可用的浏览器调试端口(DevToolsActivePort)');
|
|
880
|
+
await sleep(200);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* Read <profileDir>/DevToolsActivePort and return the browser-level ws
|
|
886
|
+
* endpoint. maxWaitMs=0 means "must already be there" (adoption path).
|
|
887
|
+
*/
|
|
888
|
+
async _readDevToolsEndpoint(profileDir, maxWaitMs) {
|
|
889
|
+
const file = path.join(profileDir, 'DevToolsActivePort');
|
|
890
|
+
const deadline = Date.now() + maxWaitMs;
|
|
891
|
+
for (;;) {
|
|
892
|
+
try {
|
|
893
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
894
|
+
const [port, wsPath] = text
|
|
895
|
+
.split(/\r?\n/)
|
|
896
|
+
.map((line) => line.trim())
|
|
897
|
+
.filter(Boolean);
|
|
898
|
+
if (port && wsPath) return `ws://127.0.0.1:${port}${wsPath}`;
|
|
899
|
+
} catch {
|
|
900
|
+
/* not written yet */
|
|
901
|
+
}
|
|
902
|
+
if (Date.now() > deadline) return null;
|
|
903
|
+
await sleep(150);
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
/**
|
|
908
|
+
* 刚 spawn 的浏览器被宿主沙箱干掉了吗?干掉过就带 `--no-sandbox` 重来一次。
|
|
909
|
+
*
|
|
910
|
+
* 返回「重试后的 snapshot」表示已经重来过(调用方直接返回它),返回 null 表示
|
|
911
|
+
* 一切正常、继续原流程。只对**我们自己 spawn** 的进程生效:接回的外部窗口崩了
|
|
912
|
+
* 不该被我们改参数重启。
|
|
913
|
+
*/
|
|
914
|
+
async _retryWithoutSandbox({ proc, browserId, url, cwd }) {
|
|
915
|
+
const crash = await this._detectSpawnCrash(proc);
|
|
916
|
+
const retry = shouldTrySandboxFallback({
|
|
917
|
+
alreadyNoSandbox: this.useNoSandbox,
|
|
918
|
+
optOut: process.env[SANDBOX_FALLBACK_ENV] === '0',
|
|
919
|
+
crashed: crash.crashed,
|
|
920
|
+
});
|
|
921
|
+
if (!retry) return null;
|
|
922
|
+
this.useNoSandbox = true;
|
|
923
|
+
this.state.sandboxFallback = true;
|
|
924
|
+
this.log?.(
|
|
925
|
+
'warn',
|
|
926
|
+
`dsh-visual-annotator: ${browserLabel(browserId)} 在宿主沙箱下起不来(${crash.reason}),` +
|
|
927
|
+
`改用 --no-sandbox 重试一次;不希望这样可设 ${SANDBOX_FALLBACK_ENV}=0`,
|
|
928
|
+
);
|
|
929
|
+
await this.close({ terminate: true }).catch(() => {});
|
|
930
|
+
await sleep(500);
|
|
931
|
+
return this.launch({ url, browser: browserId, cwd });
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
/**
|
|
935
|
+
* 刚 spawn 的浏览器还活着吗?
|
|
936
|
+
*
|
|
937
|
+
* 要抓的失败很不客气:CDP 握手成功、甚至第一次 Browser.getVersion 都能答,
|
|
938
|
+
* 之后进程才消失(Chrome 自己的 sandbox 在宿主沙箱里装不上,浏览器进程
|
|
939
|
+
* SIGTRAP)。所以判据是「进程仍是我们的 → 隔一会儿再探一次」,两次都答上来
|
|
940
|
+
* 才算活着。接外部窗口(adopted)不走这里:那个进程不是我们起的。
|
|
941
|
+
*/
|
|
942
|
+
async _detectSpawnCrash(proc) {
|
|
943
|
+
if (!proc) return { crashed: false };
|
|
944
|
+
const exited = () => proc.exitCode !== null || Boolean(proc.signalCode);
|
|
945
|
+
if (exited()) return { crashed: true, reason: `浏览器进程退出(${proc.signalCode ?? proc.exitCode})` };
|
|
946
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
947
|
+
if (!this.cdp || this.cdp.closed) return { crashed: true, reason: 'CDP 连接已断开' };
|
|
948
|
+
try {
|
|
949
|
+
await this.cdp.send('Browser.getVersion');
|
|
950
|
+
} catch {
|
|
951
|
+
return { crashed: true, reason: 'CDP 连接已断开' };
|
|
952
|
+
}
|
|
953
|
+
await sleep(250);
|
|
954
|
+
}
|
|
955
|
+
if (exited()) return { crashed: true, reason: `浏览器进程退出(${proc.signalCode ?? proc.exitCode})` };
|
|
956
|
+
return { crashed: false };
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
/** See lib/shared/gui-page.js — kept as a method for internal call sites. */
|
|
960
|
+
_isGuiPage(url, title) {
|
|
961
|
+
return isGuiPage(url, title);
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
async _waitForPage(timeoutMs = 20000) {
|
|
965
|
+
const started = Date.now();
|
|
966
|
+
const deadline = started + timeoutMs;
|
|
967
|
+
let created = false;
|
|
968
|
+
for (;;) {
|
|
969
|
+
// 早点认出「浏览器已经死了」,让 launch 的 catch 走去 --no-sandbox 回退,
|
|
970
|
+
// 而不是在这里耗到 20s 超时。
|
|
971
|
+
if (this.cdp?.closed) throw new Error('CDP 连接已断开');
|
|
972
|
+
if (this.proc && (this.proc.exitCode !== null || this.proc.signalCode)) {
|
|
973
|
+
throw new Error(`浏览器进程退出(${this.proc.signalCode ?? this.proc.exitCode})`);
|
|
974
|
+
}
|
|
975
|
+
const result = await this.cdp.send('Target.getTargets');
|
|
976
|
+
const pages = (result.targetInfos ?? []).filter((info) => info.type === 'page');
|
|
977
|
+
if (pages.length) return pages;
|
|
978
|
+
// ego lite 启动后只开自家的 chrome:// 界面(browser_ui 类型),不会自己建可
|
|
979
|
+
// 调试页面;等一小会儿还是没有就替它开一个空白页,否则这里只能等到超时。
|
|
980
|
+
if (!created && Date.now() - started > 1500) {
|
|
981
|
+
created = true;
|
|
982
|
+
await this.cdp.send('Target.createTarget', { url: 'about:blank' }).catch(() => {});
|
|
983
|
+
}
|
|
984
|
+
if (Date.now() > deadline) throw new Error('浏览器已启动但未发现可调试页面');
|
|
985
|
+
await sleep(150);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
async _attach(targetId) {
|
|
990
|
+
if (this.attached.has(targetId)) return this.attached.get(targetId);
|
|
991
|
+
const { sessionId } = await this.cdp.send('Target.attachToTarget', { targetId, flatten: true });
|
|
992
|
+
this.attached.set(targetId, sessionId);
|
|
993
|
+
// Browser-level WS clients cannot rely on Target.targetCreated being
|
|
994
|
+
// broadcast; book the tab ourselves on every attach.
|
|
995
|
+
await this.cdp
|
|
996
|
+
.send('Target.getTargetInfo', { targetId })
|
|
997
|
+
.then((info) => {
|
|
998
|
+
if (!info?.targetInfo) return;
|
|
999
|
+
this._upsertTab(info.targetInfo);
|
|
1000
|
+
})
|
|
1001
|
+
.catch(() => {});
|
|
1002
|
+
if (!this.activeTargetId) this.activeTargetId = targetId;
|
|
1003
|
+
await this.cdp.send('Page.enable', {}, sessionId).catch(() => {});
|
|
1004
|
+
await this.cdp.send('Runtime.enable', {}, sessionId).catch(() => {});
|
|
1005
|
+
await this.cdp.send('Runtime.addBinding', { name: BINDING_NAME }, sessionId).catch(() => {});
|
|
1006
|
+
await this.cdp.send('Page.addScriptToEvaluateOnNewDocument', { source: OVERLAY_SCRIPT }, sessionId).catch(() => {});
|
|
1007
|
+
await this._evaluate(OVERLAY_SCRIPT, sessionId).catch(() => {});
|
|
1008
|
+
await this._applyMode(sessionId).catch(() => {});
|
|
1009
|
+
await this._syncActiveTarget(targetId, sessionId);
|
|
1010
|
+
return sessionId;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
async _syncActiveTarget(targetId, sessionId) {
|
|
1014
|
+
try {
|
|
1015
|
+
const result = await this.cdp.send(
|
|
1016
|
+
'Runtime.evaluate',
|
|
1017
|
+
{
|
|
1018
|
+
expression: 'JSON.stringify({url: location.href, title: document.title})',
|
|
1019
|
+
returnByValue: true,
|
|
1020
|
+
},
|
|
1021
|
+
sessionId,
|
|
1022
|
+
);
|
|
1023
|
+
const parsed = JSON.parse(result?.result?.value ?? '{}');
|
|
1024
|
+
const url = parsed.url ?? '';
|
|
1025
|
+
const isBlank = (value) => !value || value === 'about:blank';
|
|
1026
|
+
const activeSession = this.attached.get(this.activeTargetId);
|
|
1027
|
+
const adopt =
|
|
1028
|
+
targetId === this.activeTargetId ||
|
|
1029
|
+
(!activeSession && !this._isGuiPage(url, parsed.title)) ||
|
|
1030
|
+
(isBlank(this.state.activeUrl) && !isBlank(url) && !this._isGuiPage(url, parsed.title));
|
|
1031
|
+
if (!adopt) return;
|
|
1032
|
+
this.activeTargetId = targetId;
|
|
1033
|
+
this.state.activeUrl = url;
|
|
1034
|
+
this.state.activeTitle = parsed.title ?? '';
|
|
1035
|
+
this._emit();
|
|
1036
|
+
} catch {
|
|
1037
|
+
/* target may be navigating */
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
_onCdpEvent(message) {
|
|
1042
|
+
const method = message.method;
|
|
1043
|
+
if (!method) return;
|
|
1044
|
+
if (method === 'Runtime.bindingCalled' && message.params?.name === BINDING_NAME) {
|
|
1045
|
+
let payload;
|
|
1046
|
+
try {
|
|
1047
|
+
payload = JSON.parse(message.params.payload ?? '{}');
|
|
1048
|
+
} catch {
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
// 页面 -> host 的统一分发:pick 记录,mode 由 host 接管并广播,undo/consume
|
|
1052
|
+
// 操作列表;未知/畸形 payload 由分类器丢弃。
|
|
1053
|
+
const action = classifyBindingAction(payload);
|
|
1054
|
+
if (action === 'pick') void this._recordPick(payload, message.sessionId);
|
|
1055
|
+
else if (action === 'mode') void this.setMode(payload.mode);
|
|
1056
|
+
else if (action === 'undo') this.undoLastPick();
|
|
1057
|
+
else if (action === 'consume') this._requestConsume();
|
|
1058
|
+
return;
|
|
1059
|
+
}
|
|
1060
|
+
if (method === 'Page.loadEventFired') {
|
|
1061
|
+
void this._applyMode(message.sessionId);
|
|
1062
|
+
return;
|
|
1063
|
+
}
|
|
1064
|
+
if (method === 'Target.targetCreated' || method === 'Target.targetInfoChanged') {
|
|
1065
|
+
const info = message.params?.targetInfo;
|
|
1066
|
+
if (info?.type === 'page' && !info.url?.startsWith('devtools://')) {
|
|
1067
|
+
this._upsertTab(info);
|
|
1068
|
+
// 用户在浏览器里自己导航/切页时,面板顶部的地址与标题也要跟着走,
|
|
1069
|
+
// 否则「当前页」和真实前台对不上(这里只认已挂上的当前页)。
|
|
1070
|
+
if (info.targetId === this.activeTargetId && (info.url || info.title)) {
|
|
1071
|
+
this.state.activeUrl = info.url ?? this.state.activeUrl;
|
|
1072
|
+
this.state.activeTitle = info.title ?? this.state.activeTitle;
|
|
1073
|
+
this._emit();
|
|
1074
|
+
}
|
|
1075
|
+
void this._attach(info.targetId).catch(() => {});
|
|
1076
|
+
}
|
|
1077
|
+
return;
|
|
1078
|
+
}
|
|
1079
|
+
if (method === 'Target.targetDestroyed') {
|
|
1080
|
+
const targetId = message.params?.targetId;
|
|
1081
|
+
this.state.tabs = (this.state.tabs ?? []).filter((tab) => tab.targetId !== targetId);
|
|
1082
|
+
this.attached.delete(targetId);
|
|
1083
|
+
if (this.activeTargetId === targetId) {
|
|
1084
|
+
this.activeTargetId = null;
|
|
1085
|
+
const rest = this.state.tabs ?? [];
|
|
1086
|
+
const next = rest.find((tab) => !this._isGuiPage(tab.url, tab.title)) ?? rest[0];
|
|
1087
|
+
if (next) {
|
|
1088
|
+
void (async () => {
|
|
1089
|
+
const sessionId = await this._attach(next.targetId).catch(() => null);
|
|
1090
|
+
if (!sessionId) return;
|
|
1091
|
+
this.activeTargetId = next.targetId;
|
|
1092
|
+
await this._syncActiveTarget(next.targetId, sessionId);
|
|
1093
|
+
})();
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
this._emit();
|
|
1097
|
+
return;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
/**
|
|
1102
|
+
* 就地更新,不打乱顺序:面板里的标签条要和浏览器那一排同序,不能因为
|
|
1103
|
+
* 每收到一次事件就把整排重排。地址变了就认新标题(可能暂时为空),
|
|
1104
|
+
* 地址没变而这次没带标题时保留旧标题,别把已知标题擦成空。
|
|
1105
|
+
*/
|
|
1106
|
+
_upsertTab(info, { emit = true } = {}) {
|
|
1107
|
+
this.state.tabs = mergeTab(this.state.tabs, info);
|
|
1108
|
+
if (emit) this._emit();
|
|
1109
|
+
return this.state.tabs;
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/**
|
|
1113
|
+
* 把 Target.getTargets 的真实情况和面板对一遍。面板每秒轮询 /state,这里节流
|
|
1114
|
+
* 到 800ms 一次;只在真的有变化时才 emit,避免无谓的重渲染。
|
|
1115
|
+
*/
|
|
1116
|
+
async refreshTabs({ force = false } = {}) {
|
|
1117
|
+
if (!this.cdp || this.cdp.closed) return;
|
|
1118
|
+
const now = Date.now();
|
|
1119
|
+
if (!force && now - (this.tabsRefreshedAt ?? 0) < 800) return;
|
|
1120
|
+
this.tabsRefreshedAt = now;
|
|
1121
|
+
let targetInfos = [];
|
|
1122
|
+
try {
|
|
1123
|
+
const result = await this.cdp.send('Target.getTargets');
|
|
1124
|
+
targetInfos = (result.targetInfos ?? []).filter(
|
|
1125
|
+
(info) => info.type === 'page' && !String(info.url).startsWith('devtools://'),
|
|
1126
|
+
);
|
|
1127
|
+
} catch {
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
if (!targetInfos.length) return;
|
|
1131
|
+
const fingerprint = (tabs) =>
|
|
1132
|
+
JSON.stringify((tabs ?? []).map((tab) => [tab.targetId, tab.url, tab.title, tab.contextId]));
|
|
1133
|
+
const before = fingerprint(this.state.tabs);
|
|
1134
|
+
for (const info of targetInfos) this._upsertTab(info, { emit: false });
|
|
1135
|
+
if (before !== fingerprint(this.state.tabs)) this._emit();
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/** Pages that already exist when we connect never fire targetCreated. */
|
|
1139
|
+
async _seedTabs() {
|
|
1140
|
+
// 不开发现,浏览器只会把「我们自己 attach 的」页面告诉我们:用户在这个窗口里
|
|
1141
|
+
// 手动新开/关掉的标签页要等下次启动才对得上——面板里的标签条于是和浏览器
|
|
1142
|
+
// 里真实的一排对不上。开一次就够,之后 targetCreated/Destroyed 会自己来。
|
|
1143
|
+
await this.cdp.send('Target.setDiscoverTargets', { discover: true }).catch(() => {});
|
|
1144
|
+
try {
|
|
1145
|
+
const result = await this.cdp.send('Target.getTargets');
|
|
1146
|
+
for (const info of result.targetInfos ?? []) {
|
|
1147
|
+
if (info.type === 'page' && !String(info.url).startsWith('devtools://')) this._upsertTab(info);
|
|
1148
|
+
}
|
|
1149
|
+
} catch {
|
|
1150
|
+
/* best effort */
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
_evaluate(expression, sessionId) {
|
|
1155
|
+
return this.cdp.send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: false }, sessionId);
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
async _applyMode(sessionId) {
|
|
1159
|
+
if (!this.cdp) return;
|
|
1160
|
+
// sync() 是 host -> page 的唯一同步口:mode 加上当前已标注数一起推。
|
|
1161
|
+
const info = JSON.stringify({ mode: this.state.mode, count: this.state.picks.length });
|
|
1162
|
+
const expression = `window.__DSH_VP__ && window.__DSH_VP__.sync(${info})`;
|
|
1163
|
+
if (sessionId) await this._evaluate(expression, sessionId).catch(() => {});
|
|
1164
|
+
else for (const sid of this.attached.values()) await this._evaluate(expression, sid).catch(() => {});
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
async _recordPick(payload, sessionId) {
|
|
1168
|
+
const id = 'p' + ++this.seq;
|
|
1169
|
+
const pick = {
|
|
1170
|
+
id,
|
|
1171
|
+
seq: payload.seq ?? this.seq,
|
|
1172
|
+
comment: payload.comment ?? '',
|
|
1173
|
+
selector: payload.selector ?? '',
|
|
1174
|
+
tag: payload.tag ?? '',
|
|
1175
|
+
text: payload.text ?? '',
|
|
1176
|
+
html: payload.html ?? '',
|
|
1177
|
+
rect: payload.rect ?? null,
|
|
1178
|
+
pageRect: payload.pageRect ?? null,
|
|
1179
|
+
url: payload.url ?? this.state.activeUrl,
|
|
1180
|
+
title: payload.title ?? this.state.activeTitle,
|
|
1181
|
+
ts: Date.now(),
|
|
1182
|
+
shotPath: null,
|
|
1183
|
+
sessionId,
|
|
1184
|
+
};
|
|
1185
|
+
// 页面脚本一次提交只应发一份,但旧版/多份注入脚本会让同一次提交投递多次,
|
|
1186
|
+
// 列表里就成了「三条一模一样的标注」。判重规则见 lib/shared/pick-dedupe.js。
|
|
1187
|
+
const signature = pickSignature(payload, this.state.activeUrl);
|
|
1188
|
+
const now = Date.now();
|
|
1189
|
+
if (isDuplicatePick({ signature: this.lastPickSignature, at: this.lastPickAt }, signature, now)) return;
|
|
1190
|
+
this.lastPickSignature = signature;
|
|
1191
|
+
this.lastPickAt = now;
|
|
1192
|
+
this.state.picks.push(pick);
|
|
1193
|
+
if (payload.title) this.state.activeTitle = payload.title;
|
|
1194
|
+
if (payload.url) this.state.activeUrl = payload.url;
|
|
1195
|
+
this._emit();
|
|
1196
|
+
// 页面 HUD 计数来自 host sync;先广播再截图,页面计数不会落后。
|
|
1197
|
+
await this._applyMode(null);
|
|
1198
|
+
// 截图要强制一帧并回读合成结果(CDP 往返)。和「卡片消失 + HUD 计数变化」落在同一帧时,
|
|
1199
|
+
// 用户看到的就是点完标注那一下轻微一抖:画面内容已经变了,合成面却还在读。往后挪一两百毫秒,
|
|
1200
|
+
// 让这一次卡顿落在已经静止的画面上,肉眼就看不到了。
|
|
1201
|
+
const captureTimer = setTimeout(() => void this._capture(pick, sessionId), 180);
|
|
1202
|
+
captureTimer.unref?.();
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
async _capture(pick, sessionId) {
|
|
1206
|
+
if (!this.cdp || !pick.pageRect) return;
|
|
1207
|
+
// 截图有两条踩过的坑,都是「用户看得见的画面被打断」:
|
|
1208
|
+
// 1) captureBeyondViewport:true 为了截到视口外内容,把渲染视口临时撑成整页再还原,
|
|
1209
|
+
// 页面收到 resize + 整页重排,就是「点完标注浏览器抖一下」。
|
|
1210
|
+
// 2) 带 clip 截图则相反,Chrome 会把合成面换成 clip 尺寸重渲染一帧再还原(实测约 60ms,
|
|
1211
|
+
// screencast 抓到的中间帧尺寸正好等于 clip),肉眼就是一记闪屏。
|
|
1212
|
+
// 所以这里既不撑视口也不带 clip:只截当前视口,元素区域在 Node 侧用 lib/shared/png.js 裁。
|
|
1213
|
+
// 整视口截图对合成面的影响是「多推一帧内容相同的画面」,screencast 实测与静止帧逐像素相同。
|
|
1214
|
+
const viewport = await this.cdp.send('Page.getLayoutMetrics', {}, sessionId).catch(() => null);
|
|
1215
|
+
const visible = viewport?.cssLayoutViewport;
|
|
1216
|
+
if (!visible || !(visible.clientWidth > 0) || !(visible.clientHeight > 0)) return;
|
|
1217
|
+
const rect = pick.pageRect;
|
|
1218
|
+
// 元素 + 4px 边距,夹进当前视口;pageRect 与 cssLayoutViewport 都是页面坐标,相减即视口内偏移。
|
|
1219
|
+
const box = {
|
|
1220
|
+
x: Math.max(Math.round(rect.x) - 4, Math.floor(visible.pageX)),
|
|
1221
|
+
y: Math.max(Math.round(rect.y) - 4, Math.floor(visible.pageY)),
|
|
1222
|
+
right: Math.min(Math.round(rect.x + rect.width) + 4, Math.ceil(visible.pageX + visible.clientWidth)),
|
|
1223
|
+
bottom: Math.min(Math.round(rect.y + rect.height) + 4, Math.ceil(visible.pageY + visible.clientHeight)),
|
|
1224
|
+
};
|
|
1225
|
+
const width = box.right - box.x;
|
|
1226
|
+
const height = box.bottom - box.y;
|
|
1227
|
+
// 元素整个滚出视口时直接不给缩略图:退回整页截图就要重新撑视口,抖动会回来。
|
|
1228
|
+
if (width < 4 || height < 4) return;
|
|
1229
|
+
try {
|
|
1230
|
+
const result = await this.cdp.send(
|
|
1231
|
+
'Page.captureScreenshot',
|
|
1232
|
+
{
|
|
1233
|
+
format: 'png',
|
|
1234
|
+
captureBeyondViewport: false,
|
|
1235
|
+
// 缩略图只要看得清元素,速度优先:编码更快,占住渲染的时间更短。
|
|
1236
|
+
optimizeForSpeed: true,
|
|
1237
|
+
},
|
|
1238
|
+
sessionId,
|
|
1239
|
+
);
|
|
1240
|
+
if (!result?.data) return;
|
|
1241
|
+
const full = Buffer.from(result.data, 'base64');
|
|
1242
|
+
// 截图是设备像素(HiDPI 上是 2x),页面坐标是 CSS 像素,按「图宽 / 视口宽」换算。
|
|
1243
|
+
const size = readPngSize(full);
|
|
1244
|
+
const scale = size ? size.width / visible.clientWidth : 1;
|
|
1245
|
+
const cropped =
|
|
1246
|
+
scale > 0
|
|
1247
|
+
? cropPng(full, {
|
|
1248
|
+
x: Math.round((box.x - visible.pageX) * scale),
|
|
1249
|
+
y: Math.round((box.y - visible.pageY) * scale),
|
|
1250
|
+
width: Math.round(width * scale),
|
|
1251
|
+
height: Math.round(height * scale),
|
|
1252
|
+
})
|
|
1253
|
+
: null;
|
|
1254
|
+
const dir = path.join(this.activeBaseDir ?? this.baseDir, 'shots');
|
|
1255
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
1256
|
+
const file = path.join(dir, `${pick.id}.png`);
|
|
1257
|
+
// 裁不出来(Chrome 换了 PNG 形态)就存整视口原图:宁可图大一点,也别丢缩略图。
|
|
1258
|
+
fs.writeFileSync(file, cropped ?? full);
|
|
1259
|
+
pick.shotPath = file;
|
|
1260
|
+
this._emit();
|
|
1261
|
+
} catch {
|
|
1262
|
+
/* element may be scrolled away or the target closed */
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
/** Fixed-expression health probe used by /probe; never a general eval channel. */
|
|
1267
|
+
async probeOverlay() {
|
|
1268
|
+
if (!this.running) throw new Error('browser not running');
|
|
1269
|
+
const expression = `JSON.stringify({
|
|
1270
|
+
version: (window.__DSH_VP__ || {}).version ?? null,
|
|
1271
|
+
mode: (window.__DSH_VP__ || {}).getMode ? window.__DSH_VP__.getMode() : null,
|
|
1272
|
+
hosts: document.querySelectorAll('#__dsh-vp-root').length,
|
|
1273
|
+
ready: document.readyState,
|
|
1274
|
+
url: location.href,
|
|
1275
|
+
title: document.title,
|
|
1276
|
+
})`;
|
|
1277
|
+
const sessionId = this.attached.get(this.activeTargetId);
|
|
1278
|
+
if (!sessionId) throw new Error('no attached page');
|
|
1279
|
+
const result = await this._evaluate(expression, sessionId);
|
|
1280
|
+
const raw = result?.result?.value;
|
|
1281
|
+
try {
|
|
1282
|
+
return JSON.parse(raw);
|
|
1283
|
+
} catch {
|
|
1284
|
+
return { raw: raw ?? null, exception: result?.exceptionDetails?.text ?? null };
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
async setMode(mode) {
|
|
1289
|
+
this.state.mode = mode === 'pick' ? 'pick' : 'off';
|
|
1290
|
+
if (this.running) await this._applyMode(null);
|
|
1291
|
+
this._emit();
|
|
1292
|
+
return this.snapshot();
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
/**
|
|
1296
|
+
* 新建一个调试空间(= 新的 BrowserContext)。命名冲突由 nextSpaceName 兜底,
|
|
1297
|
+
* 空名字回落到自动编号,保证面板永远拿得到一个能显示的名字。
|
|
1298
|
+
*/
|
|
1299
|
+
async createSpace({ name, url } = {}) {
|
|
1300
|
+
if (!this.running) throw new Error('浏览器未启动,无法新建空间');
|
|
1301
|
+
const label = sanitizeSpaceName(name) ?? nextSpaceName(this.spaces.map((space) => space.name));
|
|
1302
|
+
const { browserContextId } = await this.cdp.send('Target.createBrowserContext', {});
|
|
1303
|
+
if (!browserContextId) throw new Error('浏览器没有返回空间 id');
|
|
1304
|
+
this.spaces.push({ id: browserContextId, name: label });
|
|
1305
|
+
this._saveSpaces();
|
|
1306
|
+
await this.useSpace(browserContextId, url);
|
|
1307
|
+
return this.snapshot();
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
/** 切到某个空间:已有页面就复用,没有就替它在自己的 context 里开一个。 */
|
|
1311
|
+
async useSpace(spaceId, url) {
|
|
1312
|
+
if (!this.running) throw new Error('浏览器未启动,无法切换空间');
|
|
1313
|
+
url = normalizeAddress(url);
|
|
1314
|
+
const id = normalizeSpaceId(spaceId);
|
|
1315
|
+
if (id && !this.spaces.some((space) => space.id === id)) throw new Error('空间不存在');
|
|
1316
|
+
this.activeSpace = id;
|
|
1317
|
+
const contextId = this._spaceKey();
|
|
1318
|
+
const pages = await this._spacePages(contextId);
|
|
1319
|
+
const page = pages.find((info) => info.url && info.url !== 'about:blank') ?? pages[0];
|
|
1320
|
+
if (page) {
|
|
1321
|
+
await this._attach(page.targetId);
|
|
1322
|
+
this.activeTargetId = page.targetId;
|
|
1323
|
+
await this.cdp.send('Target.activateTarget', { targetId: page.targetId }).catch(() => {});
|
|
1324
|
+
const sessionId = this.attached.get(page.targetId);
|
|
1325
|
+
if (sessionId) await this._syncActiveTarget(page.targetId, sessionId);
|
|
1326
|
+
if (url && url !== 'about:blank' && page.url !== url) await this.navigate(url);
|
|
1327
|
+
this._emit();
|
|
1328
|
+
return this.snapshot();
|
|
1329
|
+
}
|
|
1330
|
+
// 这个空间还没有页面:在它自己的 context 里开一个,绝不去搬默认空间的标签。
|
|
1331
|
+
const created = await this.cdp.send('Target.createTarget', {
|
|
1332
|
+
url: url && url !== 'about:blank' ? url : 'about:blank',
|
|
1333
|
+
background: false,
|
|
1334
|
+
...(contextId ? { browserContextId: contextId } : {}),
|
|
1335
|
+
});
|
|
1336
|
+
await this._attach(created.targetId);
|
|
1337
|
+
this.activeTargetId = created.targetId;
|
|
1338
|
+
await this.cdp.send('Target.activateTarget', { targetId: created.targetId }).catch(() => {});
|
|
1339
|
+
if (!contextId) {
|
|
1340
|
+
// 默认空间原本一页都没有时(ego 启动直奔 agent 空间),它的 context id
|
|
1341
|
+
// 只能从刚开出来的这一页问回来;记下来,关掉空间后还能回到原处。
|
|
1342
|
+
const info = await this.cdp.send('Target.getTargetInfo', { targetId: created.targetId }).catch(() => null);
|
|
1343
|
+
const ctx = normalizeSpaceId(info?.targetInfo?.browserContextId);
|
|
1344
|
+
if (ctx && ctx !== this.defaultContextId) {
|
|
1345
|
+
this.defaultContextId = ctx;
|
|
1346
|
+
this._saveSpaces();
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
if (url && url !== 'about:blank') this.state.activeUrl = url;
|
|
1350
|
+
this._emit();
|
|
1351
|
+
await sleep(300);
|
|
1352
|
+
const sessionId = this.attached.get(created.targetId);
|
|
1353
|
+
if (sessionId) await this._syncActiveTarget(created.targetId, sessionId);
|
|
1354
|
+
return this.snapshot();
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
/** 关掉空间:销毁它的 context(连带页面与 cookie),然后回默认空间。 */
|
|
1358
|
+
async closeSpace(spaceId) {
|
|
1359
|
+
const id = normalizeSpaceId(spaceId);
|
|
1360
|
+
const index = this.spaces.findIndex((space) => space.id === id);
|
|
1361
|
+
if (index < 0) return this.snapshot();
|
|
1362
|
+
if (this.running) {
|
|
1363
|
+
await this.cdp.send('Target.disposeBrowserContext', { browserContextId: id }).catch(() => {});
|
|
1364
|
+
}
|
|
1365
|
+
this.spaces.splice(index, 1);
|
|
1366
|
+
this._saveSpaces();
|
|
1367
|
+
this.state.tabs = (this.state.tabs ?? []).filter((tab) => normalizeSpaceId(tab.contextId) !== id);
|
|
1368
|
+
if (normalizeSpaceId(this.activeSpace) === id) {
|
|
1369
|
+
this.activeSpace = '';
|
|
1370
|
+
this.activeTargetId = null;
|
|
1371
|
+
await this.useSpace('', this.state.activeUrl).catch(() => {});
|
|
1372
|
+
}
|
|
1373
|
+
this._emit();
|
|
1374
|
+
return this.snapshot();
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
async navigate(url) {
|
|
1378
|
+
if (!this.running) return this.snapshot();
|
|
1379
|
+
url = normalizeAddress(url);
|
|
1380
|
+
if (!url) return this.snapshot();
|
|
1381
|
+
const norm = (value) => {
|
|
1382
|
+
try {
|
|
1383
|
+
const parsed = new URL(value);
|
|
1384
|
+
return parsed.origin + parsed.pathname + parsed.search;
|
|
1385
|
+
} catch {
|
|
1386
|
+
return value;
|
|
1387
|
+
}
|
|
1388
|
+
};
|
|
1389
|
+
const targets = await this.cdp.send('Target.getTargets').catch(() => ({ targetInfos: [] }));
|
|
1390
|
+
const pages = (targets.targetInfos ?? []).filter(
|
|
1391
|
+
(info) => info.type === 'page' && !String(info.url).startsWith('devtools://'),
|
|
1392
|
+
);
|
|
1393
|
+
// Same URL already open -> bring that tab to the front, never duplicate.
|
|
1394
|
+
// 只在当前空间里找同 URL 的标签:另一个空间的同名页面不该被切成当前页。
|
|
1395
|
+
const contextId = this._spaceKey();
|
|
1396
|
+
const inSpace = (info) => normalizeSpaceId(info.browserContextId) === contextId;
|
|
1397
|
+
const existing = pages.find((info) => inSpace(info) && norm(info.url) === norm(url));
|
|
1398
|
+
if (existing) {
|
|
1399
|
+
await this.cdp.send('Target.activateTarget', { targetId: existing.targetId }).catch(() => {});
|
|
1400
|
+
await this._attach(existing.targetId);
|
|
1401
|
+
this.activeTargetId = existing.targetId;
|
|
1402
|
+
this.state.activeUrl = existing.url ?? url;
|
|
1403
|
+
this.state.activeTitle = existing.title ?? '';
|
|
1404
|
+
this._emit();
|
|
1405
|
+
return this.snapshot();
|
|
1406
|
+
}
|
|
1407
|
+
// New URL -> a NEW TAB in the SAME window (multi-tab, never a new window).
|
|
1408
|
+
const { targetId } = await this.cdp.send('Target.createTarget', {
|
|
1409
|
+
url,
|
|
1410
|
+
background: false,
|
|
1411
|
+
...(contextId ? { browserContextId: contextId } : {}),
|
|
1412
|
+
});
|
|
1413
|
+
await this._attach(targetId);
|
|
1414
|
+
this.activeTargetId = targetId;
|
|
1415
|
+
this.state.activeUrl = url;
|
|
1416
|
+
this._emit();
|
|
1417
|
+
await sleep(300);
|
|
1418
|
+
const sessionId = this.attached.get(targetId);
|
|
1419
|
+
if (sessionId) await this._syncActiveTarget(targetId, sessionId);
|
|
1420
|
+
return this.snapshot();
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
/** Bring an existing tab (from snapshot().tabs) to the front. */
|
|
1424
|
+
async activateTab(targetId) {
|
|
1425
|
+
if (!this.running || !targetId) return this.snapshot();
|
|
1426
|
+
await this.cdp.send('Target.activateTarget', { targetId }).catch(() => {});
|
|
1427
|
+
let sessionId = this.attached.get(targetId);
|
|
1428
|
+
if (!sessionId) sessionId = await this._attach(targetId).catch(() => null);
|
|
1429
|
+
if (sessionId) {
|
|
1430
|
+
this.activeTargetId = targetId;
|
|
1431
|
+
await this._syncActiveTarget(targetId, sessionId);
|
|
1432
|
+
}
|
|
1433
|
+
this._emit();
|
|
1434
|
+
return this.snapshot();
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
/** Bring the page back to a pick: activate its tab, then flash the overlay highlight. */
|
|
1438
|
+
async focusPick(id) {
|
|
1439
|
+
const pick = this.state.picks.find((item) => item.id === String(id ?? ''));
|
|
1440
|
+
if (!pick || !this.running) return { ...this.snapshot(), focused: false };
|
|
1441
|
+
let targetId = null;
|
|
1442
|
+
for (const [tid, sid] of this.attached) {
|
|
1443
|
+
if (sid === pick.sessionId) {
|
|
1444
|
+
targetId = tid;
|
|
1445
|
+
break;
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
if (!targetId) targetId = (this.state.tabs ?? []).find((tab) => tab.url === pick.url)?.targetId ?? null;
|
|
1449
|
+
if (!targetId) targetId = this.activeTargetId;
|
|
1450
|
+
if (!targetId) return { ...this.snapshot(), focused: false };
|
|
1451
|
+
if (targetId !== this.activeTargetId) await this.activateTab(targetId);
|
|
1452
|
+
const sessionId = this.attached.get(targetId);
|
|
1453
|
+
if (!sessionId) return { ...this.snapshot(), focused: false };
|
|
1454
|
+
const args = JSON.stringify({
|
|
1455
|
+
selector: pick.selector ?? '',
|
|
1456
|
+
pageRect: pick.pageRect ?? null,
|
|
1457
|
+
label: pick.comment || pick.selector || pick.tag || '',
|
|
1458
|
+
});
|
|
1459
|
+
const expression = `window.__DSH_VP__ && window.__DSH_VP__.highlight(${args})`;
|
|
1460
|
+
try {
|
|
1461
|
+
const result = await this._evaluate(expression, sessionId);
|
|
1462
|
+
return { ...this.snapshot(), focused: result?.result?.value !== false };
|
|
1463
|
+
} catch {
|
|
1464
|
+
return { ...this.snapshot(), focused: false };
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
async scrollTo(selector) {
|
|
1469
|
+
if (!this.running || !selector) return;
|
|
1470
|
+
const expression = `(function(){var el=document.querySelector(${JSON.stringify(selector)});if(el){el.scrollIntoView({block:'center',inline:'center'});return true}return false})()`;
|
|
1471
|
+
for (const sid of this.attached.values()) await this._evaluate(expression, sid).catch(() => {});
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
async removePick(id) {
|
|
1475
|
+
const index = this.state.picks.findIndex((pick) => pick.id === id);
|
|
1476
|
+
if (index < 0) return this.snapshot();
|
|
1477
|
+
const [removed] = this.state.picks.splice(index, 1);
|
|
1478
|
+
await this._afterRemoval([removed], false);
|
|
1479
|
+
return this.snapshot();
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
// 页面内 HUD 的「撤销」:只从尾部弹一条,复用面板同款单槽撤销语义。
|
|
1483
|
+
// 语义在 shared/pick-actions.js 的纯函数里(可单测),这里只做 host 侧副作用。
|
|
1484
|
+
async undoLastPick() {
|
|
1485
|
+
const popped = popLastPick(this.state.picks);
|
|
1486
|
+
if (!popped) return this.snapshot();
|
|
1487
|
+
this.state.picks = popped.remaining;
|
|
1488
|
+
await this._afterRemoval([popped.removed], false);
|
|
1489
|
+
return this.snapshot();
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
// 页面内 HUD 的「完成·插入」:这里只发单调递增 token,插哪个、插几次
|
|
1493
|
+
// 完全由面板凭 token 判重执行(面板忙时 token 保留,下一轮轮询补插)。
|
|
1494
|
+
_requestConsume() {
|
|
1495
|
+
const next = advancePendingConsume(this.state.pendingConsume, this.state.picks.length);
|
|
1496
|
+
if (next === this.state.pendingConsume) return;
|
|
1497
|
+
this.state.pendingConsume = next;
|
|
1498
|
+
this._emit();
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
/**
|
|
1502
|
+
* Consume picks the panel has already inserted into the composer. They leave the
|
|
1503
|
+
* list so the next annotation round starts clean, and stay restorable as one
|
|
1504
|
+
* batch. Their screenshots move to `retired` instead of being deleted: an
|
|
1505
|
+
* inserted chip still needs the file when the draft is submitted.
|
|
1506
|
+
*/
|
|
1507
|
+
async consumePicks(ids) {
|
|
1508
|
+
const wanted = new Set((Array.isArray(ids) ? ids : []).map(String));
|
|
1509
|
+
if (!wanted.size) return { ...this.snapshot(), consumed: 0 };
|
|
1510
|
+
const removed = [];
|
|
1511
|
+
this.state.picks = this.state.picks.filter((pick) => {
|
|
1512
|
+
if (!wanted.has(String(pick.id))) return true;
|
|
1513
|
+
removed.push(pick);
|
|
1514
|
+
return false;
|
|
1515
|
+
});
|
|
1516
|
+
if (!removed.length) return { ...this.snapshot(), consumed: 0 };
|
|
1517
|
+
await this._afterRemoval(removed, true);
|
|
1518
|
+
return { ...this.snapshot(), consumed: removed.length };
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
/**
|
|
1522
|
+
* Move picks into the read-only archive. The archive backs /pick, so a chip
|
|
1523
|
+
* pasted before the list was cleared still expands to its real content; the
|
|
1524
|
+
* oldest entries fall off past the cap and only then lose their screenshots.
|
|
1525
|
+
*/
|
|
1526
|
+
_archive(picks) {
|
|
1527
|
+
if (!picks.length) return;
|
|
1528
|
+
this.retired.push(...picks);
|
|
1529
|
+
const overflow = this.retired.length - MAX_RETIRED;
|
|
1530
|
+
if (overflow <= 0) return;
|
|
1531
|
+
for (const pick of this.retired.splice(0, overflow)) this._deleteShot(pick);
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
/**
|
|
1535
|
+
* 移除类操作的固定收尾:整批换撤销槽 → 广播面板 → 同步页面 HUD 计数。
|
|
1536
|
+
* archive=true 先走 _archive(consume/clear 的截图要留给已插入的 chip 继续用),
|
|
1537
|
+
* 截图本身不删;否则(单条移除/撤销)截图留在槽里供 restore 用。被顶掉的旧槽
|
|
1538
|
+
* 截图无论如何都删——它已不可恢复。
|
|
1539
|
+
*/
|
|
1540
|
+
async _afterRemoval(removed, archive) {
|
|
1541
|
+
if (this.lastRemoved) for (const pick of this.lastRemoved.picks) this._deleteShot(pick);
|
|
1542
|
+
if (archive) {
|
|
1543
|
+
this._archive(removed);
|
|
1544
|
+
this.lastRemoved = { picks: removed };
|
|
1545
|
+
} else {
|
|
1546
|
+
this.lastRemoved = removed.length ? { picks: removed } : null;
|
|
1547
|
+
}
|
|
1548
|
+
this._emit();
|
|
1549
|
+
await this._applyMode(null);
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
/** Undo the last removePick(): splice the pick back in at its seq/id position. */
|
|
1553
|
+
restorePick() {
|
|
1554
|
+
const entry = this.lastRemoved;
|
|
1555
|
+
if (!entry) return { ...this.snapshot(), restored: 0 };
|
|
1556
|
+
this.lastRemoved = null;
|
|
1557
|
+
for (const pick of entry.picks) {
|
|
1558
|
+
let index = this.state.picks.length;
|
|
1559
|
+
for (let i = 0; i < this.state.picks.length; i++) {
|
|
1560
|
+
const item = this.state.picks[i];
|
|
1561
|
+
if (
|
|
1562
|
+
(item.seq ?? 0) > (pick.seq ?? 0) ||
|
|
1563
|
+
((item.seq ?? 0) === (pick.seq ?? 0) && String(item.id) > String(pick.id))
|
|
1564
|
+
) {
|
|
1565
|
+
index = i;
|
|
1566
|
+
break;
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
this.state.picks.splice(index, 0, pick);
|
|
1570
|
+
const retiredAt = this.retired.indexOf(pick);
|
|
1571
|
+
if (retiredAt >= 0) this.retired.splice(retiredAt, 1);
|
|
1572
|
+
}
|
|
1573
|
+
this._emit();
|
|
1574
|
+
void this._applyMode(null);
|
|
1575
|
+
return { ...this.snapshot(), restored: entry.picks.length };
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
_deleteShot(pick) {
|
|
1579
|
+
if (!pick?.shotPath) return;
|
|
1580
|
+
try {
|
|
1581
|
+
fs.rmSync(pick.shotPath, { force: true });
|
|
1582
|
+
} catch {
|
|
1583
|
+
/* best effort */
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
setComment(id, comment) {
|
|
1588
|
+
const pick = this.state.picks.find((item) => item.id === id);
|
|
1589
|
+
if (pick) pick.comment = String(comment ?? '');
|
|
1590
|
+
this._emit();
|
|
1591
|
+
return this.snapshot();
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
/**
|
|
1595
|
+
* One-click clear. Picks move to the archive (chips still resolve) and stay
|
|
1596
|
+
* restorable as one batch, screenshots included, until the undo slot is
|
|
1597
|
+
* replaced or the window closes. Nothing to clear leaves the archive alone.
|
|
1598
|
+
*/
|
|
1599
|
+
async clear() {
|
|
1600
|
+
const cleared = this.state.picks.slice();
|
|
1601
|
+
if (!cleared.length) return { ...this.snapshot(), cleared: 0 };
|
|
1602
|
+
this.state.picks = [];
|
|
1603
|
+
await this._afterRemoval(cleared, true);
|
|
1604
|
+
return { ...this.snapshot(), cleared: cleared.length };
|
|
1605
|
+
}
|
|
1606
|
+
|
|
1607
|
+
shotFile(id) {
|
|
1608
|
+
const pick = this.state.picks.find((item) => item.id === id);
|
|
1609
|
+
return pick?.shotPath && fs.existsSync(pick.shotPath) ? pick.shotPath : null;
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
/** Markdown the panel injects into the composer / the agent tool returns. */
|
|
1613
|
+
toMarkdown() {
|
|
1614
|
+
const picks = this.state.picks;
|
|
1615
|
+
if (!picks.length) return '(当前还没有页面元素标注)';
|
|
1616
|
+
const lines = [];
|
|
1617
|
+
lines.push(
|
|
1618
|
+
`页面元素标注(${picks.length} 处,来源 ${this.state.browser ?? 'browser'}:${this.state.activeUrl || '未知页面'})`,
|
|
1619
|
+
);
|
|
1620
|
+
picks.forEach((pick, index) => {
|
|
1621
|
+
lines.push('');
|
|
1622
|
+
lines.push(
|
|
1623
|
+
`${index + 1}. \`${pick.selector || pick.tag}\` — ${pick.tag}${pick.text ? `:${pick.text.replace(/\s+/g, ' ').slice(0, 80)}` : ''}`,
|
|
1624
|
+
);
|
|
1625
|
+
if (pick.url) lines.push(` 页面:${pick.url}`);
|
|
1626
|
+
if (pick.pageRect)
|
|
1627
|
+
lines.push(
|
|
1628
|
+
` 位置:x=${Math.round(pick.pageRect.x)} y=${Math.round(pick.pageRect.y)} ${Math.round(pick.pageRect.width)}×${Math.round(pick.pageRect.height)}`,
|
|
1629
|
+
);
|
|
1630
|
+
if (pick.comment) lines.push(` 评论:${pick.comment}`);
|
|
1631
|
+
if (pick.html) lines.push(' HTML:' + ' `' + pick.html.replace(/\s+/g, ' ').slice(0, 300) + '`');
|
|
1632
|
+
if (pick.shotPath) lines.push(` 截图:${pick.shotPath}`);
|
|
1633
|
+
});
|
|
1634
|
+
return lines.join('\n');
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
/** Delete the browser profile when policy allows; shared/reused profiles survive. */
|
|
1638
|
+
async _cleanupProfile() {
|
|
1639
|
+
const dir = this.profileDir;
|
|
1640
|
+
this.profileDir = null;
|
|
1641
|
+
if (!dir) return;
|
|
1642
|
+
if (this.cleanProfile === false) return;
|
|
1643
|
+
if (this.cleanProfile !== true && !this.profilePerLaunch) return;
|
|
1644
|
+
// Windows releases profile file locks a beat after process exit.
|
|
1645
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
1646
|
+
try {
|
|
1647
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
1648
|
+
return;
|
|
1649
|
+
} catch {
|
|
1650
|
+
await sleep(400);
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
/**
|
|
1656
|
+
* 结束这一轮标注。默认只断开 socket:浏览器窗口与它的 profile 都留着,下次
|
|
1657
|
+
* launch 按 profile 目录重连同一个实例,登录态与标签页都还在。只有
|
|
1658
|
+
* `terminate: true`(面板上的「关闭浏览器」、host 卸载)才真的关窗;profile
|
|
1659
|
+
* 仍然保留在临时目录里,重开时接着用。外部浏览器(显式 CDP_URL 连上的)永远
|
|
1660
|
+
* 只断开,不代替用户关掉它自己的窗口。
|
|
1661
|
+
*/
|
|
1662
|
+
async close({ terminate = false } = {}) {
|
|
1663
|
+
if (this.lastRemoved) {
|
|
1664
|
+
for (const pick of this.lastRemoved.picks) this._deleteShot(pick);
|
|
1665
|
+
this.lastRemoved = null;
|
|
1666
|
+
}
|
|
1667
|
+
for (const pick of this.retired) this._deleteShot(pick);
|
|
1668
|
+
this.retired = [];
|
|
1669
|
+
// 与运行期一致走 sync() 唯一同步口:先置 off,再由 _applyMode 广播
|
|
1670
|
+
// {mode,count};旧版 overlay 没有 sync,由 REV 接管机制兜底。
|
|
1671
|
+
this.state.mode = 'off';
|
|
1672
|
+
await this._applyMode(null);
|
|
1673
|
+
this.attached.clear();
|
|
1674
|
+
this.activeTargetId = null;
|
|
1675
|
+
const cdp = this.cdp;
|
|
1676
|
+
const proc = this.proc;
|
|
1677
|
+
const wasExternal = this.external;
|
|
1678
|
+
const profileDir = this.profileDir;
|
|
1679
|
+
this.cdp = null;
|
|
1680
|
+
this.adopted = false;
|
|
1681
|
+
this.external = false;
|
|
1682
|
+
this.attachBase = null;
|
|
1683
|
+
this.proc = null;
|
|
1684
|
+
if (terminate && !wasExternal) {
|
|
1685
|
+
if (cdp) {
|
|
1686
|
+
// 自己起的窗口、以及早先被接管的同一个 profile,都走同一条退出路径;
|
|
1687
|
+
// 竞一个短超时,别让卡住的连接把面板拖住。
|
|
1688
|
+
await Promise.race([cdp.send('Browser.close').catch(() => {}), sleep(2500)]);
|
|
1689
|
+
} else if (!proc && this.windowProfileDir) {
|
|
1690
|
+
// 上一轮已经断开:重连一次,才能把留在那儿的那个实例关掉。
|
|
1691
|
+
await this._closeDetachedWindow(this.windowProfileDir);
|
|
1692
|
+
}
|
|
1693
|
+
if (proc) {
|
|
1694
|
+
if (this.exitPromise) await this.exitPromise.catch(() => {});
|
|
1695
|
+
try {
|
|
1696
|
+
proc.kill();
|
|
1697
|
+
} catch {
|
|
1698
|
+
/* already gone */
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
this.windowKept = false;
|
|
1702
|
+
this.windowProfileDir = null;
|
|
1703
|
+
this.profileDir = profileDir;
|
|
1704
|
+
await this._cleanupProfile();
|
|
1705
|
+
this.profileDir = null;
|
|
1706
|
+
} else if (cdp && !wasExternal) {
|
|
1707
|
+
// 窗口留着:记下它的 profile 目录,下次 launch 直接接管,也能响应显式关闭。
|
|
1708
|
+
this.windowKept = true;
|
|
1709
|
+
this.windowProfileDir = profileDir ?? this.windowProfileDir;
|
|
1710
|
+
}
|
|
1711
|
+
try {
|
|
1712
|
+
cdp?.close();
|
|
1713
|
+
} catch {
|
|
1714
|
+
/* already gone */
|
|
1715
|
+
}
|
|
1716
|
+
this.exitPromise = null;
|
|
1717
|
+
this.profileDir = null;
|
|
1718
|
+
this.activeBaseDir = null;
|
|
1719
|
+
if (this.state.status !== 'error') this.state.status = 'idle';
|
|
1720
|
+
this.state.mode = 'off';
|
|
1721
|
+
this._emit();
|
|
1722
|
+
return this.snapshot();
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
/** Close a window left running by an earlier close(): reconnect, ask it to quit. */
|
|
1726
|
+
async _closeDetachedWindow(profileDir) {
|
|
1727
|
+
const endpoint = await this._readDevToolsEndpoint(profileDir, 0);
|
|
1728
|
+
if (!endpoint) return;
|
|
1729
|
+
let cdp = null;
|
|
1730
|
+
try {
|
|
1731
|
+
cdp = new CdpWs(endpoint, 'browser');
|
|
1732
|
+
await cdp.open;
|
|
1733
|
+
await cdp.send('Browser.getVersion');
|
|
1734
|
+
await Promise.race([cdp.send('Browser.close').catch(() => {}), sleep(2500)]);
|
|
1735
|
+
} catch {
|
|
1736
|
+
/* the window is already gone */
|
|
1737
|
+
} finally {
|
|
1738
|
+
try {
|
|
1739
|
+
cdp?.close();
|
|
1740
|
+
} catch {
|
|
1741
|
+
/* already gone */
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
}
|