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/actions.js
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { findFrameByPath } from "./snapshot.js";
|
|
4
|
+
import { ToolError } from "./errors.js";
|
|
5
|
+
import { withTimeout, truncate } from "./util.js";
|
|
6
|
+
function sameUrl(a, b) {
|
|
7
|
+
try {
|
|
8
|
+
const ua = new URL(a);
|
|
9
|
+
const ub = new URL(b);
|
|
10
|
+
ua.hash = "";
|
|
11
|
+
ub.hash = "";
|
|
12
|
+
return ua.href === ub.href;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return a === b;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 解析目标元素。
|
|
20
|
+
* - ref:按注册表配方解析;cssPath 找不到时降级 getByRole;页面 URL 已变时仍尽力解析(带警告)
|
|
21
|
+
* - selector / text:直接构造
|
|
22
|
+
*/
|
|
23
|
+
export async function resolveTarget(manager, page, registry, params, opts = {}) {
|
|
24
|
+
const notes = [];
|
|
25
|
+
const textAsLocator = opts.textAsLocator !== false;
|
|
26
|
+
const hasRef = nonEmpty(params.ref);
|
|
27
|
+
const hasSelector = nonEmpty(params.selector);
|
|
28
|
+
// click 等工具的 text 可作为定位方式;type 的 text 是输入内容,不参与定位
|
|
29
|
+
const hasText = textAsLocator && nonEmpty(params.text);
|
|
30
|
+
if (!hasRef && !hasSelector && !hasText) {
|
|
31
|
+
throw new ToolError("missing_target", "需要提供 ref / selector" + (textAsLocator ? " / text 之一" : " 之一"), textAsLocator
|
|
32
|
+
? "先调用 browser_snapshot 获取元素的 [eN] 编号,再用 ref 操作"
|
|
33
|
+
: "text 参数是要输入的内容;请用 ref 或 selector 指定输入目标");
|
|
34
|
+
}
|
|
35
|
+
if (Number(hasRef) + Number(hasSelector) + Number(hasText) > 1) {
|
|
36
|
+
notes.push("同时提供了多种定位方式,按 ref > selector > text 优先级取其一。");
|
|
37
|
+
}
|
|
38
|
+
// ---------- ref 路径 ----------
|
|
39
|
+
if (params.ref) {
|
|
40
|
+
const entry = registry.get(params.ref);
|
|
41
|
+
if (!entry) {
|
|
42
|
+
throw new ToolError("unknown_ref", `ref ${params.ref} 不存在(可能来自更早的会话或已超出注册表容量)`, "请先 browser_snapshot 获取当前页面的最新 ref 列表");
|
|
43
|
+
}
|
|
44
|
+
const targetPage = manager.pageById(entry.pageId);
|
|
45
|
+
if (!targetPage || targetPage.isClosed()) {
|
|
46
|
+
throw new ToolError("stale_ref", `ref ${params.ref} 所属的标签页已关闭`, "用 browser_tabs list 查看现存标签,或重新 browser_snapshot");
|
|
47
|
+
}
|
|
48
|
+
if (!sameUrl(entry.pageUrl, targetPage.url())) {
|
|
49
|
+
notes.push(`⚠️ ref ${params.ref} 摄取自旧页面(${truncate(entry.pageUrl, 60)}),当前页面已变为 ${truncate(targetPage.url(), 60)} —— 优先按语义重解析,若结果不符预期请重新快照。`);
|
|
50
|
+
}
|
|
51
|
+
let frame = targetPage.mainFrame();
|
|
52
|
+
if (entry.framePath.length > 0) {
|
|
53
|
+
const f = findFrameByPath(targetPage, entry.framePath);
|
|
54
|
+
if (f) {
|
|
55
|
+
frame = f;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
notes.push("元素所在 iframe 已不存在,尝试在主文档解析。");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// 主定位:cssPath(Playwright css 引擎自动穿透 open shadow DOM)
|
|
62
|
+
try {
|
|
63
|
+
const byCss = frame.locator(entry.cssPath);
|
|
64
|
+
const n = await withTimeout(byCss.count(), 5000, "cssPath 计数");
|
|
65
|
+
if (n === 1) {
|
|
66
|
+
return { locator: byCss.first(), frame, entry, notes };
|
|
67
|
+
}
|
|
68
|
+
if (n > 1) {
|
|
69
|
+
notes.push(`cssPath 命中 ${n} 个元素,取第 1 个。`);
|
|
70
|
+
return { locator: byCss.first(), frame, entry, notes };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
notes.push("cssPath 解析失败(结构已变),改用语义定位。");
|
|
75
|
+
}
|
|
76
|
+
// 兜底 1:getByRole + name(精确)
|
|
77
|
+
if (entry.role && entry.name) {
|
|
78
|
+
try {
|
|
79
|
+
const byRole = frame.getByRole(entry.role, { name: entry.name, exact: true });
|
|
80
|
+
const n = await withTimeout(byRole.count(), 5000, "role 计数");
|
|
81
|
+
if (n >= 1) {
|
|
82
|
+
if (n > 1) {
|
|
83
|
+
notes.push(`语义定位命中 ${n} 个同名元素,取第 ${entry.occurrence + 1} 个。`);
|
|
84
|
+
return { locator: byRole.nth(Math.min(entry.occurrence, n - 1)), frame, entry, notes };
|
|
85
|
+
}
|
|
86
|
+
notes.push("ref 已按语义(role+name)自动重解析 —— 结构变了但元素还在。");
|
|
87
|
+
return { locator: byRole.first(), frame, entry, notes };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch { /* 继续下一个兜底 */ }
|
|
91
|
+
}
|
|
92
|
+
// 兜底 2:getByRole + name(包含匹配)
|
|
93
|
+
if (entry.role && entry.name) {
|
|
94
|
+
try {
|
|
95
|
+
const byRole = frame.getByRole(entry.role, { name: entry.name });
|
|
96
|
+
const n = await withTimeout(byRole.count(), 5000, "role 模糊计数");
|
|
97
|
+
if (n >= 1) {
|
|
98
|
+
notes.push("ref 已按语义(模糊匹配)自动重解析。");
|
|
99
|
+
return { locator: byRole.nth(Math.min(entry.occurrence, n - 1)), frame, entry, notes };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch { /* 忽略 */ }
|
|
103
|
+
}
|
|
104
|
+
// 兜底 3:文本
|
|
105
|
+
if (entry.name) {
|
|
106
|
+
const byText = frame.getByText(entry.name, { exact: false });
|
|
107
|
+
const n = await withTimeout(byText.count(), 5000, "text 计数").catch(() => 0);
|
|
108
|
+
if (n >= 1) {
|
|
109
|
+
notes.push("ref 已按文本自动重解析。");
|
|
110
|
+
return { locator: byText.first(), frame, entry, notes };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
throw new ToolError("stale_ref", `ref ${params.ref} 的元素在页面上已找不到(结构已变化且语义重解析失败)`, "请重新 browser_snapshot 获取最新 ref;如该区域内容是动态渲染的,可先 browser_wait 等待内容出现");
|
|
114
|
+
}
|
|
115
|
+
// ---------- selector / text 路径 ----------
|
|
116
|
+
if (hasSelector) {
|
|
117
|
+
let frame = page.mainFrame();
|
|
118
|
+
const loc = frame.locator(params.selector);
|
|
119
|
+
const n = await withTimeout(loc.count(), 5000, "selector 计数").catch(() => -1);
|
|
120
|
+
if (n === 0) {
|
|
121
|
+
throw new ToolError("not_found", `selector 未命中任何元素: ${params.selector}`, "用 browser_snapshot 查看页面结构后再定位");
|
|
122
|
+
}
|
|
123
|
+
if (n > 1)
|
|
124
|
+
notes.push(`selector 命中 ${n} 个元素,取第 1 个(可用更精确的 selector 或 :nth-child 控制)。`);
|
|
125
|
+
return { locator: loc.first(), frame, notes };
|
|
126
|
+
}
|
|
127
|
+
// text 定位(仅 click 等把 text 当定位方式的工具会走到这里)
|
|
128
|
+
{
|
|
129
|
+
let frame = page.mainFrame();
|
|
130
|
+
const loc = frame.getByText(params.text, { exact: false });
|
|
131
|
+
const n = await withTimeout(loc.count(), 5000, "text 计数").catch(() => -1);
|
|
132
|
+
if (n === 0) {
|
|
133
|
+
throw new ToolError("not_found", `未找到包含文本的元素: ${params.text}`, "文本匹配区分大小写且要求连续;用 browser_snapshot 查看元素实际文本");
|
|
134
|
+
}
|
|
135
|
+
if (n > 1)
|
|
136
|
+
notes.push(`文本命中 ${n} 个元素,取第 1 个。`);
|
|
137
|
+
return { locator: loc.first(), frame, notes };
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function nonEmpty(v) {
|
|
141
|
+
return v !== undefined && v !== null && v !== "";
|
|
142
|
+
}
|
|
143
|
+
/** 动作执行后的环境变化采集(跳转/弹窗/对话框/下载) */
|
|
144
|
+
export async function collectAfter(manager, page, tsBefore, urlBefore) {
|
|
145
|
+
const notes = {
|
|
146
|
+
notes: [],
|
|
147
|
+
urlChanged: false,
|
|
148
|
+
popups: [],
|
|
149
|
+
dialogs: [],
|
|
150
|
+
downloads: [],
|
|
151
|
+
};
|
|
152
|
+
const urlAfter = page.url();
|
|
153
|
+
if (urlAfter !== urlBefore) {
|
|
154
|
+
notes.urlChanged = true;
|
|
155
|
+
notes.newUrl = urlAfter;
|
|
156
|
+
// 等新页面基本就绪
|
|
157
|
+
await page.waitForLoadState("domcontentloaded", { timeout: 8000 }).catch(() => { });
|
|
158
|
+
}
|
|
159
|
+
notes.popups = manager.popupsSince(tsBefore);
|
|
160
|
+
notes.dialogs = manager.dialogsSince(tsBefore).map((d) => ({ type: d.type, message: d.message }));
|
|
161
|
+
notes.downloads = manager.downloadsSince(tsBefore).map((d) => ({ path: d.path, error: d.error }));
|
|
162
|
+
return notes;
|
|
163
|
+
}
|
|
164
|
+
export function formatAfter(notes) {
|
|
165
|
+
const lines = [];
|
|
166
|
+
if (notes.urlChanged)
|
|
167
|
+
lines.push(`↗️ 页面跳转 → ${notes.newUrl}`);
|
|
168
|
+
for (const p of notes.popups)
|
|
169
|
+
lines.push(`🪟 新标签页打开 → ${p.url}`);
|
|
170
|
+
for (const d of notes.dialogs)
|
|
171
|
+
lines.push(`💬 页面对话框(${d.type},已按策略处理):${d.message}`);
|
|
172
|
+
for (const d of notes.downloads) {
|
|
173
|
+
lines.push(d.error ? `⬇️ 下载失败: ${d.error}` : `⬇️ 已下载 → ${d.path}`);
|
|
174
|
+
}
|
|
175
|
+
return lines.join("\n");
|
|
176
|
+
}
|
|
177
|
+
export async function doClick(manager, page, registry, params) {
|
|
178
|
+
const target = await resolveTarget(manager, page, registry, params);
|
|
179
|
+
const tsBefore = Date.now();
|
|
180
|
+
const urlBefore = page.url();
|
|
181
|
+
const timeout = params.timeoutMs ?? manager.cfg.actionTimeoutMs;
|
|
182
|
+
try {
|
|
183
|
+
await withTimeout(target.locator.click({
|
|
184
|
+
button: params.button ?? "left",
|
|
185
|
+
clickCount: params.doubleClick ? 2 : 1,
|
|
186
|
+
timeout,
|
|
187
|
+
}), timeout + 8000, "click");
|
|
188
|
+
}
|
|
189
|
+
catch (e) {
|
|
190
|
+
// 二次尝试:重解析一次(元素可能在等待期间被重渲染)
|
|
191
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
192
|
+
if (/timeout/i.test(msg) && target.entry) {
|
|
193
|
+
const retry = await resolveTarget(manager, page, registry, { ref: params.ref }).catch(() => null);
|
|
194
|
+
if (retry) {
|
|
195
|
+
target.notes.push("首次点击超时,已重解析 ref 并重试一次。");
|
|
196
|
+
await withTimeout(retry.locator.click({ timeout: Math.min(timeout, 8000) }), timeout + 5000, "click retry");
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
throw e;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
throw e;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
await page.waitForTimeout(150);
|
|
207
|
+
const after = await collectAfter(manager, page, tsBefore, urlBefore);
|
|
208
|
+
return { target, after };
|
|
209
|
+
}
|
|
210
|
+
export async function doType(manager, page, registry, params) {
|
|
211
|
+
// text 是要输入的内容而非定位方式
|
|
212
|
+
const target = await resolveTarget(manager, page, registry, params, { textAsLocator: false });
|
|
213
|
+
const tsBefore = Date.now();
|
|
214
|
+
const urlBefore = page.url();
|
|
215
|
+
const timeout = params.timeoutMs ?? manager.cfg.actionTimeoutMs;
|
|
216
|
+
if (params.clear === false) {
|
|
217
|
+
await withTimeout(target.locator.focus(), timeout, "focus");
|
|
218
|
+
await page.keyboard.insertText(params.text);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
await withTimeout(target.locator.fill(params.text, { timeout }), timeout + 8000, "fill");
|
|
222
|
+
}
|
|
223
|
+
if (params.submit) {
|
|
224
|
+
await withTimeout(target.locator.press("Enter", { timeout }), timeout, "Enter");
|
|
225
|
+
}
|
|
226
|
+
await page.waitForTimeout(150);
|
|
227
|
+
const after = await collectAfter(manager, page, tsBefore, urlBefore);
|
|
228
|
+
return { target, after };
|
|
229
|
+
}
|
|
230
|
+
export async function doPressKey(manager, page, registry, params) {
|
|
231
|
+
const tsBefore = Date.now();
|
|
232
|
+
const urlBefore = page.url();
|
|
233
|
+
const timeout = params.timeoutMs ?? manager.cfg.actionTimeoutMs;
|
|
234
|
+
if (params.ref) {
|
|
235
|
+
const target = await resolveTarget(manager, page, registry, { ref: params.ref });
|
|
236
|
+
await withTimeout(target.locator.press(params.key, { timeout }), timeout + 5000, "press");
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
await page.keyboard.press(params.key);
|
|
240
|
+
}
|
|
241
|
+
await page.waitForTimeout(120);
|
|
242
|
+
const after = await collectAfter(manager, page, tsBefore, urlBefore);
|
|
243
|
+
return { after };
|
|
244
|
+
}
|
|
245
|
+
export async function doSelectOption(manager, page, registry, params) {
|
|
246
|
+
const target = await resolveTarget(manager, page, registry, params);
|
|
247
|
+
const tsBefore = Date.now();
|
|
248
|
+
const urlBefore = page.url();
|
|
249
|
+
const timeout = params.timeoutMs ?? manager.cfg.actionTimeoutMs;
|
|
250
|
+
let selected = [];
|
|
251
|
+
try {
|
|
252
|
+
selected = (await withTimeout(target.locator.selectOption(params.values, { timeout }), timeout + 5000, "selectOption"));
|
|
253
|
+
}
|
|
254
|
+
catch {
|
|
255
|
+
// 按 label 再试一轮
|
|
256
|
+
selected = (await withTimeout(target.locator.selectOption(params.values.map((v) => ({ label: v })), { timeout }), timeout + 5000, "selectOption by label"));
|
|
257
|
+
}
|
|
258
|
+
const after = await collectAfter(manager, page, tsBefore, urlBefore);
|
|
259
|
+
return { target, selected, after };
|
|
260
|
+
}
|
|
261
|
+
export async function doHover(manager, page, registry, params) {
|
|
262
|
+
const target = await resolveTarget(manager, page, registry, params);
|
|
263
|
+
const timeout = params.timeoutMs ?? manager.cfg.actionTimeoutMs;
|
|
264
|
+
await withTimeout(target.locator.hover({ timeout }), timeout + 5000, "hover");
|
|
265
|
+
const after = await collectAfter(manager, page, Date.now(), page.url());
|
|
266
|
+
return { target, after };
|
|
267
|
+
}
|
|
268
|
+
export async function doScroll(manager, page, registry, params) {
|
|
269
|
+
const amount = params.amount ?? 600;
|
|
270
|
+
const tsBefore = Date.now();
|
|
271
|
+
const urlBefore = page.url();
|
|
272
|
+
const dx = params.direction === "left" ? -amount : params.direction === "right" ? amount : 0;
|
|
273
|
+
const dy = params.direction === "up" ? -amount : params.direction === "down" ? amount : 0;
|
|
274
|
+
if (params.ref) {
|
|
275
|
+
const target = await resolveTarget(manager, page, registry, { ref: params.ref });
|
|
276
|
+
await target.locator.evaluate((el, arg) => {
|
|
277
|
+
el.scrollBy({ left: arg.dx, top: arg.dy, behavior: "instant" });
|
|
278
|
+
}, { dx, dy });
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
await page.mouse.wheel(dx, dy);
|
|
282
|
+
}
|
|
283
|
+
await page.waitForTimeout(200);
|
|
284
|
+
const after = await collectAfter(manager, page, tsBefore, urlBefore);
|
|
285
|
+
return { scrolled: true, after };
|
|
286
|
+
}
|
|
287
|
+
export async function doScreenshot(manager, page, registry, params) {
|
|
288
|
+
const dir = path.join(manager.cfg.downloadDir, "screenshots");
|
|
289
|
+
await mkdir(dir, { recursive: true });
|
|
290
|
+
const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
|
|
291
|
+
const name = (params.filename ? params.filename.replace(/[^\w\u4e00-\u9fa5.-]/g, "_") : `shot-${ts}`) + ".png";
|
|
292
|
+
const file = path.join(dir, name);
|
|
293
|
+
let buf;
|
|
294
|
+
if (params.ref) {
|
|
295
|
+
const target = await resolveTarget(manager, page, registry, { ref: params.ref });
|
|
296
|
+
buf = await withTimeout(target.locator.screenshot({ timeout: manager.cfg.actionTimeoutMs }), manager.cfg.actionTimeoutMs + 8000, "元素截图");
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
buf = await withTimeout(page.screenshot({ fullPage: params.fullPage === true, timeout: manager.cfg.actionTimeoutMs }), manager.cfg.actionTimeoutMs + 15000, "页面截图");
|
|
300
|
+
}
|
|
301
|
+
await writeFile(file, buf);
|
|
302
|
+
return { base64: buf.toString("base64"), file, mimeType: "image/png" };
|
|
303
|
+
}
|
|
304
|
+
/** 把用户传入的 js 字符串转为可执行的函数对象。
|
|
305
|
+
* Playwright 对字符串按表达式求值:`() => document.title` 会得到函数本身(序列化为 undefined)而非调用结果,
|
|
306
|
+
* 因此凡是函数形态的字符串一律先在 Node 侧转成真正的函数再传给 evaluate。 */
|
|
307
|
+
function toPageFunction(js) {
|
|
308
|
+
const trimmed = js.trim();
|
|
309
|
+
const looksLikeFunction = /^\(.*\)\s*=>/.test(trimmed) ||
|
|
310
|
+
/^[A-Za-z_$][\w$]*\s*=>/.test(trimmed) ||
|
|
311
|
+
/^(async\s+)?function\b/.test(trimmed) ||
|
|
312
|
+
/^async\s*\(/.test(trimmed);
|
|
313
|
+
if (!looksLikeFunction)
|
|
314
|
+
return js; // 普通表达式,原样交给 Playwright
|
|
315
|
+
try {
|
|
316
|
+
return new Function(`return (${trimmed})`)();
|
|
317
|
+
}
|
|
318
|
+
catch {
|
|
319
|
+
return js; // 解析失败按表达式处理,错误信息由页面侧给出
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
export async function doEvaluate(manager, page, registry, params) {
|
|
323
|
+
const fn = toPageFunction(params.js);
|
|
324
|
+
if (params.ref) {
|
|
325
|
+
const target = await resolveTarget(manager, page, registry, { ref: params.ref });
|
|
326
|
+
const result = await withTimeout(target.locator.evaluate(fn), manager.cfg.actionTimeoutMs + 8000, "evaluate(ref)");
|
|
327
|
+
return { result };
|
|
328
|
+
}
|
|
329
|
+
const result = await withTimeout(page.evaluate(fn), manager.cfg.actionTimeoutMs + 8000, "evaluate");
|
|
330
|
+
return { result };
|
|
331
|
+
}
|
|
332
|
+
export async function doWait(manager, page, params) {
|
|
333
|
+
const timeout = params.timeoutMs ?? manager.cfg.actionTimeoutMs;
|
|
334
|
+
const notes = [];
|
|
335
|
+
const done = [];
|
|
336
|
+
if (params.timeMs) {
|
|
337
|
+
const ms = Math.min(params.timeMs, 60000);
|
|
338
|
+
await page.waitForTimeout(ms);
|
|
339
|
+
done.push(`等待 ${ms}ms`);
|
|
340
|
+
}
|
|
341
|
+
if (params.url) {
|
|
342
|
+
try {
|
|
343
|
+
await page.waitForURL(params.url, { timeout });
|
|
344
|
+
done.push(`等待 URL 匹配 ${params.url}`);
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
notes.push(`URL 在 ${timeout}ms 内未变为 ${params.url}(当前 ${page.url()})`);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (params.text) {
|
|
351
|
+
try {
|
|
352
|
+
await page.getByText(params.text, { exact: false }).first().waitFor({ state: "visible", timeout });
|
|
353
|
+
done.push(`等待文本出现 "${truncate(params.text, 50)}"`);
|
|
354
|
+
}
|
|
355
|
+
catch {
|
|
356
|
+
notes.push(`文本 "${truncate(params.text, 50)}" 在 ${timeout}ms 内未出现 —— 内容可能还没加载,可加大 timeoutMs 或检查是否在 iframe 中`);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (params.selector) {
|
|
360
|
+
try {
|
|
361
|
+
await page.locator(params.selector).first().waitFor({ state: "visible", timeout });
|
|
362
|
+
done.push(`等待选择器 ${params.selector}`);
|
|
363
|
+
}
|
|
364
|
+
catch {
|
|
365
|
+
notes.push(`选择器 ${params.selector} 在 ${timeout}ms 内未出现`);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (params.idle) {
|
|
369
|
+
try {
|
|
370
|
+
await page.waitForLoadState("networkidle", { timeout: Math.min(timeout, 15000) });
|
|
371
|
+
done.push("等待网络空闲");
|
|
372
|
+
}
|
|
373
|
+
catch {
|
|
374
|
+
notes.push("网络在 15s 内未完全空闲(页面可能有长轮询),继续执行");
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (done.length === 0 && notes.length === 0) {
|
|
378
|
+
throw new ToolError("missing_params", "browser_wait 需要至少一个等待条件", "支持 timeMs / text / selector / url / idle");
|
|
379
|
+
}
|
|
380
|
+
return { waited: done.join("; "), notes };
|
|
381
|
+
}
|