@chendaleiq/dsh-chrome 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.en.md +97 -0
- package/README.md +84 -2
- package/cordis.patch.yml +23 -0
- package/docs/DETAIL.md +21 -0
- package/docs/TESTING.md +75 -0
- package/extension/_locales/en/messages.json +31 -0
- package/extension/_locales/zh_CN/messages.json +31 -0
- package/extension/manifest.json +16 -0
- package/extension/popup.html +29 -0
- package/extension/popup.js +63 -0
- package/extension/service_worker.js +217 -0
- package/icon.svg +9 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +61 -4
- package/scripts/bridge.mjs +23 -0
- package/scripts/setup.mjs +94 -0
- package/scripts/verify.mjs +50 -0
- package/src/auto-bridge.js +29 -0
- package/src/bridge-client.js +63 -0
- package/src/bridge-server.js +245 -0
- package/src/index.js +117 -0
- package/src/onboard.js +70 -0
- package/src/screenshot.js +35 -0
- package/src/tools.js +410 -0
package/src/tools.js
ADDED
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-chrome — 工具定义(纯模块,不依赖任何 DSH 包,可在普通 node 下单测)。
|
|
3
|
+
* Host 入口负责用 defineTool 包装后注册;这里只描述契约与执行逻辑。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export function truncate(text, maxChars) {
|
|
7
|
+
if (typeof text !== "string") return text;
|
|
8
|
+
if (text.length <= maxChars) return { text, truncated: 0 };
|
|
9
|
+
return { text: `${text.slice(0, maxChars)}[truncated ${text.length - maxChars} chars]`, truncated: text.length - maxChars };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function isBlockedUrl(url, blockPrivateIPs) {
|
|
13
|
+
try {
|
|
14
|
+
const u = new URL(url);
|
|
15
|
+
if (!["http:", "https:"].includes(u.protocol)) return `scheme ${u.protocol} not allowed`;
|
|
16
|
+
if (!blockPrivateIPs) return null;
|
|
17
|
+
const host = u.hostname;
|
|
18
|
+
if (host === "localhost" || host.endsWith(".localhost")) return "private host blocked";
|
|
19
|
+
if (/^127\./.test(host) || host === "::1" || host === "[::1]") return "loopback blocked";
|
|
20
|
+
if (/^10\./.test(host) || /^192\.168\./.test(host) || /^172\.(1[6-9]|2\d|3[01])\./.test(host)) return "private IP blocked";
|
|
21
|
+
return null;
|
|
22
|
+
} catch {
|
|
23
|
+
return "invalid URL";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const str = (v) => (typeof v === "string" ? v : undefined);
|
|
28
|
+
const int = (v) => (Number.isSafeInteger(v) ? v : undefined);
|
|
29
|
+
const textBlock = (text) => [{ type: "text", text: String(text) }];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 把 per-property DSL({ type, required, description, ... },与 DSH
|
|
33
|
+
* defineTool 的 ParameterSchemaSpec/ValueSchemaSpec 同形)编译成注册器要的
|
|
34
|
+
* 标准 JSON Schema:required:true 收拢为父级 required 数组,其余原样透传。
|
|
35
|
+
* 只用得到 type/required/description/minimum/maximum/default/const/oneOf/
|
|
36
|
+
* items/properties/additionalProperties —— 够覆盖本包全部 9 个工具。
|
|
37
|
+
*/
|
|
38
|
+
function compileNode(spec) {
|
|
39
|
+
if (spec === null || typeof spec !== "object" || Array.isArray(spec)) return spec;
|
|
40
|
+
const out = {};
|
|
41
|
+
for (const [k, v] of Object.entries(spec)) {
|
|
42
|
+
if (k === "required" || k === "properties" || k === "items" || k === "oneOf") continue;
|
|
43
|
+
out[k] = v;
|
|
44
|
+
}
|
|
45
|
+
if (spec.properties && typeof spec.properties === "object") {
|
|
46
|
+
const compiled = compilePropertyMap(spec.properties);
|
|
47
|
+
out.properties = compiled.properties;
|
|
48
|
+
if (compiled.required !== undefined) out.required = compiled.required;
|
|
49
|
+
}
|
|
50
|
+
if (spec.items !== undefined) {
|
|
51
|
+
out.items = Array.isArray(spec.items) ? spec.items.map(compileNode) : compileNode(spec.items);
|
|
52
|
+
}
|
|
53
|
+
if (Array.isArray(spec.oneOf)) out.oneOf = spec.oneOf.map(compileNode);
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function compilePropertyMap(propMap) {
|
|
58
|
+
const properties = {};
|
|
59
|
+
const required = [];
|
|
60
|
+
for (const [k, v] of Object.entries(propMap)) {
|
|
61
|
+
if (v !== null && typeof v === "object" && !Array.isArray(v)) {
|
|
62
|
+
const { required: req, ...rest } = v;
|
|
63
|
+
properties[k] = compileNode(rest);
|
|
64
|
+
if (req === true) required.push(k);
|
|
65
|
+
} else {
|
|
66
|
+
properties[k] = compileNode(v);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return { properties, ...(required.length > 0 ? { required } : {}) };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 编译单个工具定义为注册器可直接吃的形态(含参数校验包装)。 */
|
|
73
|
+
export function compileToolDefinition(def) {
|
|
74
|
+
const compiledParams = {
|
|
75
|
+
type: "object",
|
|
76
|
+
properties: compilePropertyMap(def.parameters || {}).properties,
|
|
77
|
+
...(() => {
|
|
78
|
+
const { required } = compilePropertyMap(def.parameters || {});
|
|
79
|
+
return required !== undefined ? { required } : {};
|
|
80
|
+
})(),
|
|
81
|
+
};
|
|
82
|
+
const compiledSchema = compileNode(def.output.schema);
|
|
83
|
+
const checkType = (t, v) => {
|
|
84
|
+
if (t === "string") return typeof v === "string";
|
|
85
|
+
if (t === "boolean") return typeof v === "boolean";
|
|
86
|
+
if (t === "integer") return Number.isInteger(v);
|
|
87
|
+
if (t === "number") return typeof v === "number";
|
|
88
|
+
return true;
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
name: def.name,
|
|
92
|
+
description: def.description,
|
|
93
|
+
parameters: compiledParams,
|
|
94
|
+
output: { schema: compiledSchema, render: def.output.render },
|
|
95
|
+
async execute(args, exec) {
|
|
96
|
+
const a = args === null || typeof args !== "object" ? {} : args;
|
|
97
|
+
const violations = [];
|
|
98
|
+
for (const [k, spec] of Object.entries(def.parameters || {})) {
|
|
99
|
+
const s = spec !== null && typeof spec === "object" && !Array.isArray(spec) ? spec : {};
|
|
100
|
+
if (a[k] === undefined) {
|
|
101
|
+
if (s.required === true) violations.push(`${k}: required`);
|
|
102
|
+
} else if (s.type !== undefined && !checkType(s.type, a[k])) {
|
|
103
|
+
violations.push(`${k}: expected ${s.type}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (violations.length > 0) throw new Error(`invalid arguments: ${violations.join("; ")}`);
|
|
107
|
+
return def.execute(a, exec);
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @param deps.send (action, params, timeoutMs, exec) => Promise<result>
|
|
114
|
+
* @param deps.bridgeStatus () => Promise<status>
|
|
115
|
+
* @param deps.config resolved config
|
|
116
|
+
* @param deps.saveScreenshot (dataDir, sessionKey, dataUrl) => { path, bytes, width, height }
|
|
117
|
+
* @param deps.sessionKey stable session key
|
|
118
|
+
* @param deps.doOnboard () => Promise<onboard result>
|
|
119
|
+
* @returns plain tool definitions ({ name, description, parameters, output, execute })
|
|
120
|
+
*/
|
|
121
|
+
export function buildTools({ send, bridgeStatus, config, saveScreenshot, sessionKey, doOnboard }) {
|
|
122
|
+
return [
|
|
123
|
+
{
|
|
124
|
+
name: "chrome_onboard",
|
|
125
|
+
description: "引导安装 Chrome 扩展:桥不通或扩展没连上时调它,会打开文件管理器定位扩展目录并返回步骤。Guide extension setup: opens the file manager at the extension folder and returns steps.",
|
|
126
|
+
parameters: {},
|
|
127
|
+
output: {
|
|
128
|
+
schema: {
|
|
129
|
+
type: "object",
|
|
130
|
+
additionalProperties: false,
|
|
131
|
+
properties: {
|
|
132
|
+
connected: { type: "boolean", required: true },
|
|
133
|
+
bridgeUp: { type: "boolean", required: true },
|
|
134
|
+
extensionDir: { type: "string", required: true },
|
|
135
|
+
opened: { type: "boolean", required: true },
|
|
136
|
+
steps: { type: "array", required: true, items: { type: "string" } },
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
render: (_args, value) => textBlock([`connected: ${value.connected}`, `extensionDir: ${value.extensionDir}`, ...value.steps].join("\n")),
|
|
140
|
+
},
|
|
141
|
+
async execute(args, exec) {
|
|
142
|
+
void args;
|
|
143
|
+
void exec;
|
|
144
|
+
return doOnboard();
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
name: "chrome_status",
|
|
149
|
+
description: "检查本地 Chrome 桥连接状态;其他 chrome_* 出错时先调它。Check the local Chrome bridge connection; call first when other chrome_* tools fail.",
|
|
150
|
+
parameters: {},
|
|
151
|
+
output: {
|
|
152
|
+
schema: {
|
|
153
|
+
type: "object",
|
|
154
|
+
additionalProperties: false,
|
|
155
|
+
properties: {
|
|
156
|
+
connected: { type: "boolean", required: true },
|
|
157
|
+
url: { type: "string" },
|
|
158
|
+
mode: { type: "string" },
|
|
159
|
+
lastSeenAt: { type: "integer" },
|
|
160
|
+
clientName: { type: "string" },
|
|
161
|
+
queuedCommands: { type: "integer" },
|
|
162
|
+
pendingCommands: { type: "integer" },
|
|
163
|
+
httpStatus: { type: "integer" },
|
|
164
|
+
error: { type: "string" },
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
render: (_args, value) => textBlock(JSON.stringify(value)),
|
|
168
|
+
},
|
|
169
|
+
async execute(args, exec) {
|
|
170
|
+
void args;
|
|
171
|
+
void exec;
|
|
172
|
+
const s = await bridgeStatus();
|
|
173
|
+
return {
|
|
174
|
+
connected: s.connected === true,
|
|
175
|
+
...(str(s.url) !== undefined ? { url: s.url } : {}),
|
|
176
|
+
...(str(s.mode) !== undefined ? { mode: s.mode } : {}),
|
|
177
|
+
...(int(s.lastSeenAt) !== undefined ? { lastSeenAt: s.lastSeenAt } : {}),
|
|
178
|
+
...(str(s.clientName) !== undefined ? { clientName: s.clientName } : {}),
|
|
179
|
+
...(int(s.queuedCommands) !== undefined ? { queuedCommands: s.queuedCommands } : {}),
|
|
180
|
+
...(int(s.pendingCommands) !== undefined ? { pendingCommands: s.pendingCommands } : {}),
|
|
181
|
+
...(int(s.httpStatus) !== undefined ? { httpStatus: s.httpStatus } : {}),
|
|
182
|
+
...(str(s.error) !== undefined ? { error: s.error } : {}),
|
|
183
|
+
};
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
name: "chrome_tabs_list",
|
|
188
|
+
description: "列出已打开的标签页(有界输出)。List open tabs with bounded output.",
|
|
189
|
+
parameters: {
|
|
190
|
+
limit: { type: "integer", description: "最多返回几个(1-50,默认 20)。Max tabs to return (1-50, default 20)." },
|
|
191
|
+
query: { type: "string", description: "按标题或 URL 子串过滤。Substring filter on title or url." },
|
|
192
|
+
},
|
|
193
|
+
output: {
|
|
194
|
+
schema: {
|
|
195
|
+
type: "object",
|
|
196
|
+
additionalProperties: false,
|
|
197
|
+
properties: {
|
|
198
|
+
total: { type: "integer", required: true },
|
|
199
|
+
tabs: {
|
|
200
|
+
type: "array",
|
|
201
|
+
required: true,
|
|
202
|
+
items: {
|
|
203
|
+
type: "object",
|
|
204
|
+
additionalProperties: false,
|
|
205
|
+
properties: {
|
|
206
|
+
id: { type: "string", required: true },
|
|
207
|
+
title: { type: "string", required: true },
|
|
208
|
+
url: { type: "string", required: true },
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
render: (_args, value) =>
|
|
215
|
+
textBlock([`tabs: ${value.total}`, ...value.tabs.map((t) => ` - [${t.id}] ${t.title} ${t.url}`)].join("\n")),
|
|
216
|
+
},
|
|
217
|
+
async execute(args, exec) {
|
|
218
|
+
const result = await send("tab.list", {}, 15000, exec);
|
|
219
|
+
const tabs = Array.isArray(result) ? result : Array.isArray(result?.tabs) ? result.tabs : [];
|
|
220
|
+
const q = typeof args.query === "string" && args.query.length > 0 ? args.query.toLowerCase() : null;
|
|
221
|
+
const filtered = q ? tabs.filter((t) => `${t.title || ""} ${t.url || ""}`.toLowerCase().includes(q)) : tabs;
|
|
222
|
+
const limit = Math.max(1, Math.min(50, Number(args.limit) || 20));
|
|
223
|
+
return {
|
|
224
|
+
total: filtered.length,
|
|
225
|
+
tabs: filtered.slice(0, limit).map((t) => ({
|
|
226
|
+
id: String(t.id ?? ""),
|
|
227
|
+
title: String(t.title ?? ""),
|
|
228
|
+
url: String(t.url ?? ""),
|
|
229
|
+
})),
|
|
230
|
+
};
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
name: "chrome_tab_select",
|
|
235
|
+
description: "按 id 激活一个标签页;不导航、不关闭。Activate one tab by id; no navigation or close.",
|
|
236
|
+
parameters: {
|
|
237
|
+
tabId: { type: "string", required: true, description: "chrome_tabs_list 给出的 id。Tab id from chrome_tabs_list." },
|
|
238
|
+
},
|
|
239
|
+
output: {
|
|
240
|
+
schema: {
|
|
241
|
+
type: "object",
|
|
242
|
+
additionalProperties: false,
|
|
243
|
+
properties: {
|
|
244
|
+
ok: { type: "boolean", required: true },
|
|
245
|
+
activeTabId: { type: "string", required: true },
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
render: (_args, value) => textBlock(JSON.stringify(value)),
|
|
249
|
+
},
|
|
250
|
+
async execute(args, exec) {
|
|
251
|
+
const r = await send("tab.activate", { tabId: args.tabId }, 15000, exec);
|
|
252
|
+
return { ok: r?.ok !== false, activeTabId: String(r?.activeTabId ?? args.tabId) };
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
name: "chrome_tab_new",
|
|
257
|
+
description: "后台开一个新标签页(可选 URL),记为自有标签待清理。Open a new background tab, optionally with a URL; tracked as owned.",
|
|
258
|
+
parameters: {
|
|
259
|
+
url: { type: "string", description: "要打开的 https URL,默认 about:blank。URL to open; defaults to about:blank." },
|
|
260
|
+
},
|
|
261
|
+
output: {
|
|
262
|
+
schema: {
|
|
263
|
+
type: "object",
|
|
264
|
+
additionalProperties: false,
|
|
265
|
+
properties: {
|
|
266
|
+
id: { type: "string", required: true },
|
|
267
|
+
title: { type: "string", required: true },
|
|
268
|
+
url: { type: "string", required: true },
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
render: (_args, value) => textBlock(JSON.stringify(value)),
|
|
272
|
+
},
|
|
273
|
+
async execute(args, exec) {
|
|
274
|
+
if (args.url !== undefined) {
|
|
275
|
+
const blocked = isBlockedUrl(args.url, config.blockPrivateIPs);
|
|
276
|
+
if (blocked) throw new Error(`navigation blocked: ${blocked}`);
|
|
277
|
+
}
|
|
278
|
+
const r = await send("tab.new", { url: args.url }, 15000, exec);
|
|
279
|
+
return { id: String(r?.id ?? ""), title: String(r?.title ?? ""), url: String(r?.url ?? args.url ?? "about:blank") };
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
{
|
|
283
|
+
name: "chrome_tab_close",
|
|
284
|
+
description: "按 id 关闭一个标签页。Close one tab by id.",
|
|
285
|
+
parameters: {
|
|
286
|
+
tabId: { type: "string", required: true, description: "chrome_tabs_list 给出的 id。Tab id from chrome_tabs_list." },
|
|
287
|
+
},
|
|
288
|
+
output: {
|
|
289
|
+
schema: {
|
|
290
|
+
type: "object",
|
|
291
|
+
additionalProperties: false,
|
|
292
|
+
properties: {
|
|
293
|
+
closed: { type: "string", required: true },
|
|
294
|
+
},
|
|
295
|
+
},
|
|
296
|
+
render: (_args, value) => textBlock(JSON.stringify(value)),
|
|
297
|
+
},
|
|
298
|
+
async execute(args, exec) {
|
|
299
|
+
await send("tab.close", { tabId: args.tabId }, 15000, exec);
|
|
300
|
+
return { closed: String(args.tabId) };
|
|
301
|
+
},
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
name: "chrome_snapshot",
|
|
305
|
+
description: "抓取标签页无障碍快照,返回 [ref=N] 供后续操作;阅读优先用它而不是截图。Capture an accessibility snapshot with [ref=N] handles; prefer this over screenshots for reading.",
|
|
306
|
+
parameters: {
|
|
307
|
+
tabId: { type: "string", description: "默认当前活动标签页。Defaults to the active tab." },
|
|
308
|
+
maxChars: { type: "integer", description: "快照最大字符数(1000-20000)。Max snapshot chars (1000-20000)." },
|
|
309
|
+
},
|
|
310
|
+
output: {
|
|
311
|
+
schema: {
|
|
312
|
+
type: "object",
|
|
313
|
+
additionalProperties: false,
|
|
314
|
+
properties: {
|
|
315
|
+
snapshot: { type: "string", required: true },
|
|
316
|
+
truncated: { type: "integer", required: true },
|
|
317
|
+
},
|
|
318
|
+
},
|
|
319
|
+
render: (_args, value) => textBlock(value.snapshot),
|
|
320
|
+
},
|
|
321
|
+
async execute(args, exec) {
|
|
322
|
+
const result = await send("page.snapshot", { tabId: args.tabId }, 20000, exec);
|
|
323
|
+
const out = truncate(String(result?.snapshot ?? ""), Number(args.maxChars) || config.snapshotMaxChars);
|
|
324
|
+
return { snapshot: out.text, truncated: out.truncated };
|
|
325
|
+
},
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
name: "chrome_navigate",
|
|
329
|
+
description: "导航到 https URL;私有地址直接拒绝。Navigate to an https URL; private hosts are rejected.",
|
|
330
|
+
parameters: {
|
|
331
|
+
url: { type: "string", required: true, description: "要打开的 https URL。https URL to open." },
|
|
332
|
+
tabId: { type: "string", description: "默认当前活动标签页。Defaults to the active tab." },
|
|
333
|
+
},
|
|
334
|
+
output: {
|
|
335
|
+
schema: {
|
|
336
|
+
type: "object",
|
|
337
|
+
additionalProperties: false,
|
|
338
|
+
properties: {
|
|
339
|
+
ok: { type: "boolean", required: true },
|
|
340
|
+
url: { type: "string", required: true },
|
|
341
|
+
title: { type: "string", required: true },
|
|
342
|
+
},
|
|
343
|
+
},
|
|
344
|
+
render: (_args, value) => textBlock(JSON.stringify(value)),
|
|
345
|
+
},
|
|
346
|
+
async execute(args, exec) {
|
|
347
|
+
const blocked = isBlockedUrl(args.url, config.blockPrivateIPs);
|
|
348
|
+
if (blocked) throw new Error(`navigation blocked: ${blocked}`);
|
|
349
|
+
const r = await send("page.navigate", { url: args.url, tabId: args.tabId }, 30000, exec);
|
|
350
|
+
return { ok: r?.ok !== false, url: String(r?.url ?? args.url), title: String(r?.title ?? "") };
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
{
|
|
354
|
+
name: "chrome_screenshot",
|
|
355
|
+
description: "截屏落盘,只返回 path 和尺寸,不回图片字节。Screenshot to a file; returns path and size, not image bytes.",
|
|
356
|
+
parameters: {
|
|
357
|
+
tabId: { type: "string", description: "默认当前活动标签页。Defaults to the active tab." },
|
|
358
|
+
fullPage: { type: "boolean", description: "整页更耗资源;默认只截可视区。Full page costs more; default viewport only." },
|
|
359
|
+
},
|
|
360
|
+
output: {
|
|
361
|
+
schema: {
|
|
362
|
+
type: "object",
|
|
363
|
+
additionalProperties: false,
|
|
364
|
+
properties: {
|
|
365
|
+
path: { type: "string", required: true },
|
|
366
|
+
bytes: { type: "integer", required: true },
|
|
367
|
+
width: { required: true, oneOf: [{ type: "integer" }, { type: "null" }] },
|
|
368
|
+
height: { required: true, oneOf: [{ type: "integer" }, { type: "null" }] },
|
|
369
|
+
},
|
|
370
|
+
},
|
|
371
|
+
render: (_args, value) => textBlock(JSON.stringify(value)),
|
|
372
|
+
},
|
|
373
|
+
async execute(args, exec) {
|
|
374
|
+
if (args.fullPage === true) throw new Error("fullPage is not supported in 0.1.0 (viewport only)");
|
|
375
|
+
const shot = await send("page.screenshot", { tabId: args.tabId }, 30000, exec);
|
|
376
|
+
return saveScreenshot(config.dataDir, sessionKey, shot?.dataUrl);
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
{
|
|
380
|
+
name: "chrome_evaluate",
|
|
381
|
+
description: "在页面里执行只读 JS 表达式,返回截断后的 JSON。Evaluate a read-only JS expression; returns truncated JSON.",
|
|
382
|
+
parameters: {
|
|
383
|
+
expression: { type: "string", required: true, description: "单个 JS 表达式,如 document.title。Single JS expression, e.g. document.title." },
|
|
384
|
+
tabId: { type: "string", description: "默认当前活动标签页。Defaults to the active tab." },
|
|
385
|
+
},
|
|
386
|
+
output: {
|
|
387
|
+
schema: {
|
|
388
|
+
type: "object",
|
|
389
|
+
additionalProperties: false,
|
|
390
|
+
properties: {
|
|
391
|
+
value: { type: "string", required: true },
|
|
392
|
+
truncated: { type: "integer", required: true },
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
render: (_args, value) => textBlock(value.value),
|
|
396
|
+
},
|
|
397
|
+
async execute(args, exec) {
|
|
398
|
+
const result = await send(
|
|
399
|
+
"page.evaluate",
|
|
400
|
+
{ expression: args.expression, tabId: args.tabId },
|
|
401
|
+
20000,
|
|
402
|
+
exec,
|
|
403
|
+
);
|
|
404
|
+
const text = JSON.stringify(result?.value ?? result);
|
|
405
|
+
const out = truncate(text, config.evaluateMaxChars);
|
|
406
|
+
return { value: out.text, truncated: out.truncated };
|
|
407
|
+
},
|
|
408
|
+
},
|
|
409
|
+
];
|
|
410
|
+
}
|