browser-ref-mcp 0.0.0-stage → 0.1.1
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/LICENSE +202 -0
- package/README.md +174 -2
- package/README.zh-CN.md +157 -0
- package/dist/actions.js +381 -0
- package/dist/browserManager.js +410 -0
- package/dist/config.js +125 -0
- package/dist/errors.js +52 -0
- package/dist/index.js +77 -0
- package/dist/session.js +176 -0
- package/dist/snapshot.js +707 -0
- package/dist/tools.js +498 -0
- package/dist/util.js +113 -0
- package/package.json +53 -4
package/dist/snapshot.js
ADDED
|
@@ -0,0 +1,707 @@
|
|
|
1
|
+
import { truncate } from "./util.js";
|
|
2
|
+
export const WALKER_FN = function walkerInPage(options) {
|
|
3
|
+
const maxInteractive = options.maxInteractive ?? 400;
|
|
4
|
+
const maxText = options.maxText ?? 250;
|
|
5
|
+
const maxDepth = options.maxDepth ?? 40;
|
|
6
|
+
const INTERACTIVE_TAGS = new Set([
|
|
7
|
+
"a", "button", "input", "select", "textarea", "summary", "label", "option",
|
|
8
|
+
"details", "audio", "video", "embed", "object", "menuitem", "area", "canvas",
|
|
9
|
+
]);
|
|
10
|
+
const INTERACTIVE_ROLES = new Set([
|
|
11
|
+
"button", "link", "checkbox", "radio", "textbox", "searchbox", "combobox",
|
|
12
|
+
"listbox", "option", "slider", "spinbutton", "tab", "menuitem", "menuitemcheckbox",
|
|
13
|
+
"menuitemradio", "switch", "treeitem", "gridcell", "cell",
|
|
14
|
+
]);
|
|
15
|
+
const SKIP_TAGS = new Set([
|
|
16
|
+
"script", "style", "noscript", "template", "head", "meta", "link", "title",
|
|
17
|
+
"br", "hr", "source", "track", "datalist", "param", "base", "wbr",
|
|
18
|
+
]);
|
|
19
|
+
const TEXT_TAGS = new Set(["p", "li", "td", "th", "dd", "dt", "blockquote", "figcaption", "pre", "code", "span", "em", "strong", "small", "div"]);
|
|
20
|
+
const LANDMARK_MAP = {
|
|
21
|
+
main: "main", nav: "nav", header: "header", footer: "footer",
|
|
22
|
+
aside: "aside", section: "section", form: "form", article: "article", search: "search",
|
|
23
|
+
};
|
|
24
|
+
const LANDMARK_ROLES = new Set(["main", "navigation", "banner", "contentinfo", "complementary", "region", "form", "search", "article", "dialog"]);
|
|
25
|
+
let interactiveEmitted = 0;
|
|
26
|
+
let textEmitted = 0;
|
|
27
|
+
let visited = 0;
|
|
28
|
+
let truncated = false;
|
|
29
|
+
const roleCounters = new Map();
|
|
30
|
+
function collapse(s, max) {
|
|
31
|
+
if (!s)
|
|
32
|
+
return undefined;
|
|
33
|
+
const t = s.replace(/\s+/g, " ").trim();
|
|
34
|
+
if (!t)
|
|
35
|
+
return undefined;
|
|
36
|
+
return t.length > max ? t.slice(0, max) + "…" : t;
|
|
37
|
+
}
|
|
38
|
+
function ownText(el) {
|
|
39
|
+
let out = "";
|
|
40
|
+
Array.from(el.childNodes).forEach((n) => {
|
|
41
|
+
if (n.nodeType === 3)
|
|
42
|
+
out += n.textContent ?? "";
|
|
43
|
+
});
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
function accName(el, tag) {
|
|
47
|
+
const doc = el.ownerDocument;
|
|
48
|
+
const ariaLabel = el.getAttribute("aria-label");
|
|
49
|
+
if (ariaLabel && ariaLabel.trim())
|
|
50
|
+
return ariaLabel;
|
|
51
|
+
const labelledby = el.getAttribute("aria-labelledby");
|
|
52
|
+
if (labelledby) {
|
|
53
|
+
const root = el.getRootNode();
|
|
54
|
+
const parts = labelledby
|
|
55
|
+
.split(/\s+/)
|
|
56
|
+
.map((id) => root.getElementById ? (root.getElementById(id)?.textContent ?? "") : "")
|
|
57
|
+
.join(" ");
|
|
58
|
+
const j = collapse(parts, 120);
|
|
59
|
+
if (j)
|
|
60
|
+
return j;
|
|
61
|
+
}
|
|
62
|
+
if (tag === "img" || tag === "area" || tag === "image") {
|
|
63
|
+
const alt = el.getAttribute("alt");
|
|
64
|
+
if (alt && alt.trim())
|
|
65
|
+
return alt;
|
|
66
|
+
}
|
|
67
|
+
if (tag === "input") {
|
|
68
|
+
const type = (el.getAttribute("type") || "text").toLowerCase();
|
|
69
|
+
if (type === "button" || type === "submit" || type === "reset") {
|
|
70
|
+
const v = el.value || el.getAttribute("value");
|
|
71
|
+
if (v)
|
|
72
|
+
return String(v);
|
|
73
|
+
}
|
|
74
|
+
if (el.id) {
|
|
75
|
+
try {
|
|
76
|
+
const lb = doc.querySelector('label[for="' + (window.CSS && CSS.escape ? CSS.escape(el.id) : el.id) + '"]');
|
|
77
|
+
if (lb && lb.textContent)
|
|
78
|
+
return collapse(lb.textContent, 120) || "";
|
|
79
|
+
}
|
|
80
|
+
catch { /* ignore */ }
|
|
81
|
+
}
|
|
82
|
+
const wrap = el.closest("label");
|
|
83
|
+
if (wrap && wrap !== el && wrap.textContent)
|
|
84
|
+
return collapse(wrap.textContent, 120) || "";
|
|
85
|
+
const ph = el.getAttribute("placeholder");
|
|
86
|
+
if (ph)
|
|
87
|
+
return ph;
|
|
88
|
+
const nm = el.getAttribute("name");
|
|
89
|
+
if (nm)
|
|
90
|
+
return nm;
|
|
91
|
+
return "";
|
|
92
|
+
}
|
|
93
|
+
const title = el.getAttribute("title");
|
|
94
|
+
if (title && title.trim())
|
|
95
|
+
return title;
|
|
96
|
+
if (tag === "button" || tag === "a" || tag === "summary" || tag === "label" || tag === "select" || tag === "textarea") {
|
|
97
|
+
const t = collapse(el.textContent, 120);
|
|
98
|
+
if (t)
|
|
99
|
+
return t;
|
|
100
|
+
}
|
|
101
|
+
return "";
|
|
102
|
+
}
|
|
103
|
+
function cssEscape(s) {
|
|
104
|
+
try {
|
|
105
|
+
return window.CSS && CSS.escape ? CSS.escape(s) : s.replace(/([^\w-])/g, "\\$1");
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return s;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** container = Document 或 ShadowRoot;返回容器内的唯一 css 路径(失败返回 null) */
|
|
112
|
+
function cssPathWithin(container, el) {
|
|
113
|
+
const segs = [];
|
|
114
|
+
let node = el;
|
|
115
|
+
while (node && node !== container) {
|
|
116
|
+
if (node.nodeType !== 1) {
|
|
117
|
+
node = node.parentNode;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const elCur = node;
|
|
121
|
+
if (elCur.id) {
|
|
122
|
+
segs.unshift("#" + cssEscape(elCur.id));
|
|
123
|
+
return segs.join(" ");
|
|
124
|
+
}
|
|
125
|
+
let seg = elCur.localName;
|
|
126
|
+
const parent = elCur.parentElement;
|
|
127
|
+
if (parent) {
|
|
128
|
+
const same = Array.from(parent.children).filter((c) => c.localName === elCur.localName);
|
|
129
|
+
if (same.length > 1) {
|
|
130
|
+
seg += ":nth-of-type(" + (same.indexOf(elCur) + 1) + ")";
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
segs.unshift(seg);
|
|
134
|
+
node = elCur.parentNode;
|
|
135
|
+
}
|
|
136
|
+
if (node !== container)
|
|
137
|
+
return null; // 不在容器内(detached)
|
|
138
|
+
return segs.join(" ") || "*";
|
|
139
|
+
}
|
|
140
|
+
function roleFor(el, tag) {
|
|
141
|
+
const roleAttr = el.getAttribute("role");
|
|
142
|
+
if (roleAttr)
|
|
143
|
+
return roleAttr;
|
|
144
|
+
if (tag === "a")
|
|
145
|
+
return "link";
|
|
146
|
+
if (tag === "button")
|
|
147
|
+
return "button";
|
|
148
|
+
if (tag === "select")
|
|
149
|
+
return "combobox";
|
|
150
|
+
if (tag === "textarea")
|
|
151
|
+
return "textbox";
|
|
152
|
+
if (tag === "img")
|
|
153
|
+
return "img";
|
|
154
|
+
if (tag === "option")
|
|
155
|
+
return "option";
|
|
156
|
+
if (tag === "nav")
|
|
157
|
+
return "navigation";
|
|
158
|
+
if (tag === "main")
|
|
159
|
+
return "main";
|
|
160
|
+
if (tag === "header")
|
|
161
|
+
return "banner";
|
|
162
|
+
if (tag === "footer")
|
|
163
|
+
return "contentinfo";
|
|
164
|
+
if (tag === "aside")
|
|
165
|
+
return "complementary";
|
|
166
|
+
if (tag === "form")
|
|
167
|
+
return "form";
|
|
168
|
+
if (tag === "section")
|
|
169
|
+
return "region";
|
|
170
|
+
if (tag === "article")
|
|
171
|
+
return "article";
|
|
172
|
+
if (/^h[1-6]$/.test(tag))
|
|
173
|
+
return "heading";
|
|
174
|
+
if (tag === "input") {
|
|
175
|
+
const type = (el.getAttribute("type") || "text").toLowerCase();
|
|
176
|
+
if (type === "checkbox")
|
|
177
|
+
return "checkbox";
|
|
178
|
+
if (type === "radio")
|
|
179
|
+
return "radio";
|
|
180
|
+
if (type === "button" || type === "submit" || type === "reset" || type === "image")
|
|
181
|
+
return "button";
|
|
182
|
+
return "textbox";
|
|
183
|
+
}
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
function isInteractive(el, tag, role, st) {
|
|
187
|
+
const roleAttr = el.getAttribute("role");
|
|
188
|
+
if (INTERACTIVE_TAGS.has(tag)) {
|
|
189
|
+
if (tag === "a")
|
|
190
|
+
return !!(el.getAttribute("href") || roleAttr);
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
if (roleAttr && INTERACTIVE_ROLES.has(roleAttr))
|
|
194
|
+
return true;
|
|
195
|
+
if (el.isContentEditable)
|
|
196
|
+
return true;
|
|
197
|
+
if (el.getAttribute("onclick") || el.getAttribute("ondblclick") || el.getAttribute("onmousedown"))
|
|
198
|
+
return true;
|
|
199
|
+
if (st.cursor === "pointer") {
|
|
200
|
+
const t = (el.textContent || "").replace(/\s+/g, " ").trim();
|
|
201
|
+
if (t.length < 200)
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
if (role && INTERACTIVE_ROLES.has(role))
|
|
205
|
+
return true;
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
function makeNode(el, container, prefix, st) {
|
|
209
|
+
const tag = el.localName;
|
|
210
|
+
const cssRel = cssPathWithin(container, el);
|
|
211
|
+
if (!cssRel)
|
|
212
|
+
return null;
|
|
213
|
+
const role = roleFor(el, tag);
|
|
214
|
+
const name = collapse(accName(el, tag), 120);
|
|
215
|
+
const rect = el.getBoundingClientRect();
|
|
216
|
+
const node = { t: "el", tag, cssPath: prefix ? prefix + " " + cssRel : cssRel };
|
|
217
|
+
if (role)
|
|
218
|
+
node.role = role;
|
|
219
|
+
if (name)
|
|
220
|
+
node.name = name;
|
|
221
|
+
const w = Math.round(rect.width);
|
|
222
|
+
const h = Math.round(rect.height);
|
|
223
|
+
if (w > 0 || h > 0)
|
|
224
|
+
node.box = [Math.round(rect.left), Math.round(rect.top), w, h];
|
|
225
|
+
// 状态
|
|
226
|
+
if (tag === "input" || tag === "textarea") {
|
|
227
|
+
const type = (el.getAttribute("type") || "text").toLowerCase();
|
|
228
|
+
const input = el;
|
|
229
|
+
if (type === "checkbox" || type === "radio")
|
|
230
|
+
node.checked = input.checked;
|
|
231
|
+
else if (type !== "password" && input.value)
|
|
232
|
+
node.value = collapse(input.value, 80);
|
|
233
|
+
if (type === "password")
|
|
234
|
+
node.value = "•••";
|
|
235
|
+
}
|
|
236
|
+
if (tag === "textarea" && el.value)
|
|
237
|
+
node.value = collapse(el.value, 80);
|
|
238
|
+
if (tag === "select") {
|
|
239
|
+
const sel = el;
|
|
240
|
+
const opt = sel.selectedOptions;
|
|
241
|
+
if (opt && opt.length)
|
|
242
|
+
node.selected = collapse(Array.from(opt).map((o) => o.label || o.value).join(", "), 60);
|
|
243
|
+
node.optionsCount = sel.options ? sel.options.length : undefined;
|
|
244
|
+
}
|
|
245
|
+
if (tag === "a") {
|
|
246
|
+
const href = el.href;
|
|
247
|
+
if (href)
|
|
248
|
+
node.href = href.length > 120 ? href.slice(0, 120) + "…" : href;
|
|
249
|
+
}
|
|
250
|
+
const ph = el.getAttribute("placeholder");
|
|
251
|
+
if (ph)
|
|
252
|
+
node.placeholder = collapse(ph, 80);
|
|
253
|
+
if (el.getAttribute("aria-disabled") === "true")
|
|
254
|
+
node.disabled = true;
|
|
255
|
+
if (tag === "input" || tag === "button" || tag === "select" || tag === "textarea" || tag === "option") {
|
|
256
|
+
if (el.disabled)
|
|
257
|
+
node.disabled = true;
|
|
258
|
+
}
|
|
259
|
+
if (tag === "img") {
|
|
260
|
+
const alt = el.getAttribute("alt");
|
|
261
|
+
if (alt)
|
|
262
|
+
node.alt = collapse(alt, 80);
|
|
263
|
+
}
|
|
264
|
+
// 可滚动容器
|
|
265
|
+
if ((st.overflowY === "auto" || st.overflowY === "scroll") && el.scrollHeight > el.clientHeight + 8)
|
|
266
|
+
node.scrollable = true;
|
|
267
|
+
else if ((st.overflowX === "auto" || st.overflowX === "scroll") && el.scrollWidth > el.clientWidth + 8)
|
|
268
|
+
node.scrollable = true;
|
|
269
|
+
if (tag === "img" || tag === "svg" || tag === "canvas") {
|
|
270
|
+
node.role = node.role ?? "img";
|
|
271
|
+
}
|
|
272
|
+
const interactive = isInteractive(el, tag, node.role ?? undefined, st);
|
|
273
|
+
const heading = /^h[1-6]$/.test(tag) ? tag : undefined;
|
|
274
|
+
const landmark = LANDMARK_MAP[tag] ?? (el.getAttribute("role") && LANDMARK_ROLES.has(el.getAttribute("role")) ? el.getAttribute("role") : undefined);
|
|
275
|
+
const own = ownText(el);
|
|
276
|
+
const textblock = !interactive && !heading && !landmark && collapse(own, 200) !== undefined && TEXT_TAGS.has(tag) && el.children.length === 0;
|
|
277
|
+
if (interactive) {
|
|
278
|
+
if (interactiveEmitted >= maxInteractive) {
|
|
279
|
+
truncated = true;
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
interactiveEmitted++;
|
|
283
|
+
node.interactive = true;
|
|
284
|
+
const key = (node.role ?? tag) + "\u0000" + (name ?? "");
|
|
285
|
+
const occ = roleCounters.get(key) ?? 0;
|
|
286
|
+
node.roleOccurrence = occ;
|
|
287
|
+
roleCounters.set(key, occ + 1);
|
|
288
|
+
}
|
|
289
|
+
else if (heading) {
|
|
290
|
+
node.heading = heading;
|
|
291
|
+
node.text = collapse(el.textContent, 200);
|
|
292
|
+
}
|
|
293
|
+
else if (landmark) {
|
|
294
|
+
node.landmark = landmark;
|
|
295
|
+
if (name)
|
|
296
|
+
node.name = name;
|
|
297
|
+
}
|
|
298
|
+
else if (textblock) {
|
|
299
|
+
if (textEmitted >= maxText) {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
textEmitted++;
|
|
303
|
+
node.textblock = true;
|
|
304
|
+
node.text = collapse(own, 200);
|
|
305
|
+
}
|
|
306
|
+
else if (node.scrollable) {
|
|
307
|
+
node.interactive = true; // 可滚动容器也需要 ref(供 browser_scroll 用)
|
|
308
|
+
node.role = node.role ?? "scrollable";
|
|
309
|
+
interactiveEmitted++;
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
return null; // 无语义的普通容器:不发射,但继续走子树
|
|
313
|
+
}
|
|
314
|
+
return node;
|
|
315
|
+
}
|
|
316
|
+
function walk(el, container, prefix, depth, parentChildren) {
|
|
317
|
+
if (visited >= 20000 || depth > maxDepth) {
|
|
318
|
+
truncated = true;
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
visited++;
|
|
322
|
+
const tag = el.localName;
|
|
323
|
+
if (!tag || SKIP_TAGS.has(tag))
|
|
324
|
+
return;
|
|
325
|
+
if (el.getAttribute("aria-hidden") === "true")
|
|
326
|
+
return;
|
|
327
|
+
let st;
|
|
328
|
+
try {
|
|
329
|
+
st = getComputedStyle(el);
|
|
330
|
+
}
|
|
331
|
+
catch {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (st.display === "none" || st.visibility === "hidden" || Number(st.opacity) === 0)
|
|
335
|
+
return;
|
|
336
|
+
// iframe:发射占位节点,不下钻(由 Node 侧按 URL 拼接)
|
|
337
|
+
if (tag === "iframe" || tag === "frame") {
|
|
338
|
+
const src = el.src || el.getAttribute("src") || "";
|
|
339
|
+
const node = {
|
|
340
|
+
t: "iframe",
|
|
341
|
+
tag,
|
|
342
|
+
cssPath: prefix ? prefix + " " + cssPathWithin(container, el) : cssPathWithin(container, el) || "",
|
|
343
|
+
src: src || "about:blank",
|
|
344
|
+
frameName: el.getAttribute("name") ?? undefined,
|
|
345
|
+
};
|
|
346
|
+
try {
|
|
347
|
+
const a = new URL(src, location.href);
|
|
348
|
+
node.src = a.href;
|
|
349
|
+
}
|
|
350
|
+
catch { /* keep raw */ }
|
|
351
|
+
parentChildren.push(node);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (tag === "svg") {
|
|
355
|
+
// svg 当整体,不下钻内部
|
|
356
|
+
const node = makeNode(el, container, prefix, st);
|
|
357
|
+
if (node)
|
|
358
|
+
parentChildren.push(node);
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
// shadow host:透明处理(不发射 host 本身),改走 shadow 内部
|
|
362
|
+
const shadow = el.shadowRoot;
|
|
363
|
+
if (shadow) {
|
|
364
|
+
const hostRel = cssPathWithin(container, el);
|
|
365
|
+
if (hostRel) {
|
|
366
|
+
const hostPrefix = prefix ? prefix + " " + hostRel : hostRel;
|
|
367
|
+
Array.from(shadow.childNodes).forEach((n) => {
|
|
368
|
+
if (n.nodeType === 1)
|
|
369
|
+
walk(n, shadow, hostPrefix, depth + 1, parentChildren);
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
const node = makeNode(el, container, prefix, st);
|
|
375
|
+
if (node) {
|
|
376
|
+
parentChildren.push(node);
|
|
377
|
+
// 继续下钻(在已发射节点内找更多语义节点)
|
|
378
|
+
if (tag !== "select" && tag !== "option" && tag !== "textarea" && tag !== "input") {
|
|
379
|
+
Array.from(el.children).forEach((c) => walk(c, container, prefix, depth + 1, (node.children ??= [])));
|
|
380
|
+
}
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
// 未发射:继续走子树(挂在同一层)
|
|
384
|
+
if (tag !== "select" && tag !== "option" && tag !== "textarea") {
|
|
385
|
+
Array.from(el.children).forEach((c) => walk(c, container, prefix, depth + 1, parentChildren));
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
let rootContainer = null;
|
|
389
|
+
let rootEl = null;
|
|
390
|
+
if (options.rootSelector) {
|
|
391
|
+
try {
|
|
392
|
+
rootEl = document.querySelector(options.rootSelector);
|
|
393
|
+
}
|
|
394
|
+
catch { /* 无效选择器 */ }
|
|
395
|
+
if (!rootEl)
|
|
396
|
+
return { roots: [], truncated: false, visited: 0 };
|
|
397
|
+
}
|
|
398
|
+
const roots = [];
|
|
399
|
+
const startEls = [];
|
|
400
|
+
if (rootEl) {
|
|
401
|
+
rootContainer = document;
|
|
402
|
+
startEls.push(rootEl);
|
|
403
|
+
// 根元素本身可能是语义节点
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
rootContainer = document;
|
|
407
|
+
const body = document.body ?? document.documentElement;
|
|
408
|
+
if (!body)
|
|
409
|
+
return { roots: [], truncated: false, visited: 0 };
|
|
410
|
+
startEls.push(body);
|
|
411
|
+
}
|
|
412
|
+
for (const start of startEls) {
|
|
413
|
+
const st = getComputedStyle(start);
|
|
414
|
+
const node = rootEl ? makeNode(start, rootContainer, "", st) : null;
|
|
415
|
+
if (node) {
|
|
416
|
+
roots.push(node);
|
|
417
|
+
Array.from(start.children).forEach((c) => walk(c, rootContainer, "", 1, (node.children ??= [])));
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
// 根为透明容器:走子树
|
|
421
|
+
if (start instanceof HTMLElement && start.shadowRoot) {
|
|
422
|
+
const shadow = start.shadowRoot;
|
|
423
|
+
const hostRel = cssPathWithin(document, start);
|
|
424
|
+
const prefix2 = hostRel ?? "";
|
|
425
|
+
Array.from(shadow.childNodes).forEach((n) => {
|
|
426
|
+
if (n.nodeType === 1)
|
|
427
|
+
walk(n, shadow, prefix2, 1, roots);
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
Array.from(start.children).forEach((c) => walk(c, rootContainer, "", 1, roots));
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return { roots, truncated, visited };
|
|
436
|
+
};
|
|
437
|
+
export class RefRegistry {
|
|
438
|
+
byRef = new Map();
|
|
439
|
+
counter = 0;
|
|
440
|
+
maxEntries = 8000;
|
|
441
|
+
assign(entry) {
|
|
442
|
+
this.counter++;
|
|
443
|
+
const ref = "e" + this.counter;
|
|
444
|
+
this.byRef.set(ref, { ...entry, ref });
|
|
445
|
+
while (this.byRef.size > this.maxEntries) {
|
|
446
|
+
const first = this.byRef.keys().next();
|
|
447
|
+
if (first.done)
|
|
448
|
+
break;
|
|
449
|
+
this.byRef.delete(first.value);
|
|
450
|
+
}
|
|
451
|
+
return ref;
|
|
452
|
+
}
|
|
453
|
+
get(ref) {
|
|
454
|
+
return this.byRef.get(ref.trim());
|
|
455
|
+
}
|
|
456
|
+
/** 页面导航后该页旧 ref 作废 */
|
|
457
|
+
clearPage(pageId) {
|
|
458
|
+
for (const [k, v] of this.byRef) {
|
|
459
|
+
if (v.pageId === pageId)
|
|
460
|
+
this.byRef.delete(k);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
get size() {
|
|
464
|
+
return this.byRef.size;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
function normalizeUrl(u) {
|
|
468
|
+
try {
|
|
469
|
+
const parsed = new URL(u);
|
|
470
|
+
parsed.hash = "";
|
|
471
|
+
return parsed.href;
|
|
472
|
+
}
|
|
473
|
+
catch {
|
|
474
|
+
return u;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
/** 把子 frame 的树拼进 iframe 占位节点;返回未能匹配的 frame */
|
|
478
|
+
function stitchInto(roots, parentFrame, frameTrees, usedFrames) {
|
|
479
|
+
// DFS 按文档序找 iframe 占位
|
|
480
|
+
const placeholders = [];
|
|
481
|
+
const dfs = (nodes) => {
|
|
482
|
+
for (const n of nodes) {
|
|
483
|
+
if (n.t === "iframe")
|
|
484
|
+
placeholders.push(n);
|
|
485
|
+
if (n.children)
|
|
486
|
+
dfs(n.children);
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
dfs(roots);
|
|
490
|
+
const children = parentFrame.childFrames();
|
|
491
|
+
const unmatched = [];
|
|
492
|
+
for (const ph of placeholders) {
|
|
493
|
+
let matched;
|
|
494
|
+
// 1. URL 精确匹配(忽略 hash)
|
|
495
|
+
matched = children.find((f) => !usedFrames.has(f) && normalizeUrl(f.url()) === normalizeUrl(ph.src ?? ""));
|
|
496
|
+
// 2. name 匹配
|
|
497
|
+
if (!matched && ph.frameName) {
|
|
498
|
+
matched = children.find((f) => !usedFrames.has(f) && f.name() === ph.frameName);
|
|
499
|
+
}
|
|
500
|
+
// 3. about:blank 顺序匹配
|
|
501
|
+
if (!matched && (ph.src === "about:blank" || !ph.src)) {
|
|
502
|
+
matched = children.find((f) => !usedFrames.has(f) && f.url() === "about:blank");
|
|
503
|
+
}
|
|
504
|
+
if (matched) {
|
|
505
|
+
usedFrames.add(matched);
|
|
506
|
+
const sub = (frameTrees.get(matched) ?? []);
|
|
507
|
+
// 子 frame 根节点的 framePath = 父路径 + 子 frame 描述符
|
|
508
|
+
const childPath = [...(ph.framePath ?? []), { url: matched.url(), name: matched.name() }];
|
|
509
|
+
ph.framePath = childPath;
|
|
510
|
+
for (const n of sub)
|
|
511
|
+
n.framePath = childPath;
|
|
512
|
+
ph.children = sub.length > 0 ? sub : [{ t: "el", tag: "em", text: "(iframe 内容为空或不可访问)", cssPath: "" }];
|
|
513
|
+
// 递归拼更深一层
|
|
514
|
+
const deeper = stitchInto(ph.children, matched, frameTrees, usedFrames);
|
|
515
|
+
unmatched.push(...deeper);
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
void ph;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
// 没匹配上任何占位的 frame,追加到末尾(不丢信息)
|
|
522
|
+
for (const f of children) {
|
|
523
|
+
if (usedFrames.has(f))
|
|
524
|
+
continue;
|
|
525
|
+
const sub = (frameTrees.get(f) ?? []);
|
|
526
|
+
const ph = {
|
|
527
|
+
t: "iframe",
|
|
528
|
+
tag: "iframe",
|
|
529
|
+
cssPath: "",
|
|
530
|
+
src: f.url() || "about:blank",
|
|
531
|
+
frameName: f.name() || undefined,
|
|
532
|
+
children: sub.length > 0 ? sub : [{ t: "el", tag: "em", text: "(iframe 内容为空)", cssPath: "" }],
|
|
533
|
+
};
|
|
534
|
+
const childPath = [...(roots[0]?.framePath ?? []), { url: f.url(), name: f.name() }];
|
|
535
|
+
for (const n of sub)
|
|
536
|
+
n.framePath = childPath;
|
|
537
|
+
ph.framePath = childPath;
|
|
538
|
+
roots.push(ph);
|
|
539
|
+
unmatched.push(...stitchInto(ph.children, f, frameTrees, usedFrames));
|
|
540
|
+
usedFrames.add(f);
|
|
541
|
+
}
|
|
542
|
+
return unmatched;
|
|
543
|
+
}
|
|
544
|
+
function framePathOf(frame) {
|
|
545
|
+
const chain = [];
|
|
546
|
+
let cur = frame;
|
|
547
|
+
while (cur && cur.parentFrame()) {
|
|
548
|
+
chain.unshift({ url: cur.url(), name: cur.name() });
|
|
549
|
+
cur = cur.parentFrame();
|
|
550
|
+
}
|
|
551
|
+
return chain;
|
|
552
|
+
}
|
|
553
|
+
async function walkFrame(frame, opts) {
|
|
554
|
+
try {
|
|
555
|
+
const result = (await frame.evaluate(WALKER_FN, opts));
|
|
556
|
+
return result.roots;
|
|
557
|
+
}
|
|
558
|
+
catch {
|
|
559
|
+
// frame 可能已销毁/跨域被销毁重建
|
|
560
|
+
return [];
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
/** 对整页(含 iframe)做快照,登记 ref,返回渲染后的文本 */
|
|
564
|
+
export async function snapshotPage(page, pageId, registry, opts = {}) {
|
|
565
|
+
const filter = opts.filter ?? "balanced";
|
|
566
|
+
const maxElements = opts.maxElements ?? 400;
|
|
567
|
+
const maxChars = opts.maxChars ?? 30000;
|
|
568
|
+
let rootSelector = opts.rootSelector;
|
|
569
|
+
let targetFrame = page.mainFrame();
|
|
570
|
+
if (opts.rootRef) {
|
|
571
|
+
const entry = registry.get(opts.rootRef);
|
|
572
|
+
if (!entry) {
|
|
573
|
+
return { text: `ref ${opts.rootRef} 不存在或已过期,请重新 browser_snapshot。`, refCount: 0, truncated: false };
|
|
574
|
+
}
|
|
575
|
+
if (entry.framePath.length > 0) {
|
|
576
|
+
const found = findFrameByPath(page, entry.framePath);
|
|
577
|
+
if (!found)
|
|
578
|
+
return { text: `ref ${opts.rootRef} 所在的 frame 已不存在,请重新 browser_snapshot。`, refCount: 0, truncated: false };
|
|
579
|
+
targetFrame = found;
|
|
580
|
+
}
|
|
581
|
+
rootSelector = entry.cssPath;
|
|
582
|
+
}
|
|
583
|
+
const walkOpts = {
|
|
584
|
+
maxInteractive: 400,
|
|
585
|
+
maxText: 250,
|
|
586
|
+
maxDepth: 40,
|
|
587
|
+
rootSelector,
|
|
588
|
+
};
|
|
589
|
+
// 并行走所有 frame
|
|
590
|
+
const frames = page.frames();
|
|
591
|
+
const results = await Promise.all(frames.map((f) => walkFrame(f, walkOpts)));
|
|
592
|
+
const frameTrees = new Map();
|
|
593
|
+
frames.forEach((f, i) => frameTrees.set(f, results[i]));
|
|
594
|
+
// 主 frame 根部继承空 framePath
|
|
595
|
+
const mainRoots = (frameTrees.get(page.mainFrame()) ?? []);
|
|
596
|
+
for (const n of mainRoots)
|
|
597
|
+
n.framePath = [];
|
|
598
|
+
frameTrees.set(page.mainFrame(), mainRoots);
|
|
599
|
+
// 拼接子 frame
|
|
600
|
+
const used = new Set([page.mainFrame()]);
|
|
601
|
+
stitchInto(mainRoots, page.mainFrame(), frameTrees, used);
|
|
602
|
+
// DFS:登记 ref + 渲染
|
|
603
|
+
const lines = [];
|
|
604
|
+
let refCount = 0;
|
|
605
|
+
let hitCap = false;
|
|
606
|
+
const renderExtras = (n) => {
|
|
607
|
+
const parts = [];
|
|
608
|
+
if (n.value !== undefined)
|
|
609
|
+
parts.push(`value=${JSON.stringify(n.value)}`);
|
|
610
|
+
if (n.placeholder)
|
|
611
|
+
parts.push(`placeholder=${JSON.stringify(n.placeholder)}`);
|
|
612
|
+
if (n.checked !== undefined)
|
|
613
|
+
parts.push(n.checked ? "(已勾选)" : "(未勾选)");
|
|
614
|
+
if (n.selected)
|
|
615
|
+
parts.push(`selected=${JSON.stringify(n.selected)}`);
|
|
616
|
+
if (n.optionsCount !== undefined)
|
|
617
|
+
parts.push(`(${n.optionsCount}个选项)`);
|
|
618
|
+
if (n.disabled)
|
|
619
|
+
parts.push("(禁用)");
|
|
620
|
+
if (n.scrollable)
|
|
621
|
+
parts.push("[可滚动]");
|
|
622
|
+
if (n.href) {
|
|
623
|
+
let short = n.href;
|
|
624
|
+
try {
|
|
625
|
+
const u = new URL(n.href);
|
|
626
|
+
short = u.host + u.pathname + (u.search ? u.search.slice(0, 40) : "");
|
|
627
|
+
}
|
|
628
|
+
catch { /* keep */ }
|
|
629
|
+
parts.push(`→ ${short}`);
|
|
630
|
+
}
|
|
631
|
+
return parts.length ? " " + parts.join(" ") : "";
|
|
632
|
+
};
|
|
633
|
+
const assignRef = (n) => {
|
|
634
|
+
const framePath = n.framePath ?? [];
|
|
635
|
+
return registry.assign({
|
|
636
|
+
pageId,
|
|
637
|
+
pageUrl: page.url(),
|
|
638
|
+
framePath,
|
|
639
|
+
cssPath: n.cssPath,
|
|
640
|
+
tag: n.tag,
|
|
641
|
+
role: n.role ?? null,
|
|
642
|
+
name: n.name ?? "",
|
|
643
|
+
occurrence: n.roleOccurrence ?? 0,
|
|
644
|
+
box: n.box,
|
|
645
|
+
});
|
|
646
|
+
};
|
|
647
|
+
const emit = (n, depth) => {
|
|
648
|
+
if (hitCap || lines.length >= maxElements) {
|
|
649
|
+
hitCap = true;
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
const indent = " ".repeat(depth);
|
|
653
|
+
const isInteractive = n.interactive === true;
|
|
654
|
+
if (filter !== "interactive" || isInteractive || n.t === "iframe") {
|
|
655
|
+
if (n.t === "iframe") {
|
|
656
|
+
lines.push(`${indent}- iframe → ${n.src ?? ""}${n.frameName ? ` (name=${n.frameName})` : ""}`);
|
|
657
|
+
}
|
|
658
|
+
else if (isInteractive) {
|
|
659
|
+
const role = n.role ?? n.tag;
|
|
660
|
+
const name = n.name ? ` "${n.name}"` : n.text ? ` "${truncate(n.text, 40, "…")}"` : "";
|
|
661
|
+
const ref = assignRef(n);
|
|
662
|
+
lines.push(`${indent}- [${ref}] ${role}${name}${renderExtras(n)}`);
|
|
663
|
+
refCount++;
|
|
664
|
+
}
|
|
665
|
+
else if (n.heading) {
|
|
666
|
+
lines.push(`${indent}- ${n.heading}: ${n.text ?? n.name ?? ""}`);
|
|
667
|
+
}
|
|
668
|
+
else if (n.landmark) {
|
|
669
|
+
lines.push(`${indent}- [${n.landmark}${n.name ? ` ${JSON.stringify(n.name)}` : ""}]`);
|
|
670
|
+
}
|
|
671
|
+
else if (n.textblock && filter !== "interactive") {
|
|
672
|
+
lines.push(`${indent}- text: ${n.text ?? ""}`);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
if (n.children) {
|
|
676
|
+
for (const c of n.children)
|
|
677
|
+
emit(c, depth + 1);
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
for (const n of mainRoots)
|
|
681
|
+
emit(n, 0);
|
|
682
|
+
const title = opts.title ?? "";
|
|
683
|
+
const header = `# 快照: ${title || "(无标题)"} — ${page.url()}`;
|
|
684
|
+
const body = lines.join("\n").slice(0, maxChars);
|
|
685
|
+
const footers = ["(带 [eN] 的行可交互/可滚动,ref 可直接用于 browser_click/type 等工具)"];
|
|
686
|
+
if (hitCap) {
|
|
687
|
+
footers.push(`⚠️ 已截断(超 ${maxElements} 行)—— 用 rootRef/selector 只看相关区域,或 filter:"interactive" 只看交互元素`);
|
|
688
|
+
}
|
|
689
|
+
return { text: `${header}\n${body}\n${footers.join(";")}。`, refCount, truncated: hitCap };
|
|
690
|
+
}
|
|
691
|
+
export function findFrameByPath(page, path) {
|
|
692
|
+
let cur = page.mainFrame();
|
|
693
|
+
for (const desc of path) {
|
|
694
|
+
const children = cur.childFrames();
|
|
695
|
+
let next;
|
|
696
|
+
next = children.find((f) => normalizeUrl(f.url()) === normalizeUrl(desc.url));
|
|
697
|
+
if (!next && desc.name)
|
|
698
|
+
next = children.find((f) => f.name() === desc.name);
|
|
699
|
+
if (!next && (!desc.url || desc.url === "about:blank")) {
|
|
700
|
+
next = children.find((f) => f.url() === "about:blank");
|
|
701
|
+
}
|
|
702
|
+
if (!next)
|
|
703
|
+
return null;
|
|
704
|
+
cur = next;
|
|
705
|
+
}
|
|
706
|
+
return cur;
|
|
707
|
+
}
|