@pyrokine/mcp-chrome 2.4.4 → 3.0.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/README.md +401 -102
- package/README_zh.md +319 -79
- package/dist/anti-detection/injection.js +3 -3
- package/dist/cdp/client.d.ts +1 -1
- package/dist/cdp/client.d.ts.map +1 -1
- package/dist/cdp/client.js +18 -7
- package/dist/cdp/client.js.map +1 -1
- package/dist/cdp/launcher.d.ts +2 -3
- package/dist/cdp/launcher.d.ts.map +1 -1
- package/dist/cdp/launcher.js +84 -24
- package/dist/cdp/launcher.js.map +1 -1
- package/dist/core/auto-wait.d.ts +3 -3
- package/dist/core/auto-wait.d.ts.map +1 -1
- package/dist/core/auto-wait.js +45 -20
- package/dist/core/auto-wait.js.map +1 -1
- package/dist/core/browser-driver.d.ts +101 -12
- package/dist/core/browser-driver.d.ts.map +1 -1
- package/dist/core/browser-driver.js.map +1 -1
- package/dist/core/errors.d.ts +16 -0
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +77 -0
- package/dist/core/errors.js.map +1 -1
- package/dist/core/locator.d.ts.map +1 -1
- package/dist/core/locator.js +6 -4
- package/dist/core/locator.js.map +1 -1
- package/dist/core/os-mouse.d.ts +15 -2
- package/dist/core/os-mouse.d.ts.map +1 -1
- package/dist/core/os-mouse.js +52 -8
- package/dist/core/os-mouse.js.map +1 -1
- package/dist/core/session.d.ts +34 -19
- package/dist/core/session.d.ts.map +1 -1
- package/dist/core/session.js +574 -130
- package/dist/core/session.js.map +1 -1
- package/dist/core/types.d.ts +2 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/unified-session.d.ts +95 -20
- package/dist/core/unified-session.d.ts.map +1 -1
- package/dist/core/unified-session.js +489 -117
- package/dist/core/unified-session.js.map +1 -1
- package/dist/core/utils.d.ts +5 -0
- package/dist/core/utils.d.ts.map +1 -1
- package/dist/core/utils.js +475 -54
- package/dist/core/utils.js.map +1 -1
- package/dist/extension/bridge.d.ts +46 -36
- package/dist/extension/bridge.d.ts.map +1 -1
- package/dist/extension/bridge.js +269 -112
- package/dist/extension/bridge.js.map +1 -1
- package/dist/extension/http-server.d.ts +3 -0
- package/dist/extension/http-server.d.ts.map +1 -1
- package/dist/extension/http-server.js +19 -2
- package/dist/extension/http-server.js.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/tools/browse.d.ts.map +1 -1
- package/dist/tools/browse.js +241 -171
- package/dist/tools/browse.js.map +1 -1
- package/dist/tools/challenge.d.ts +1 -1
- package/dist/tools/challenge.d.ts.map +1 -1
- package/dist/tools/challenge.js +75 -37
- package/dist/tools/challenge.js.map +1 -1
- package/dist/tools/cookies.d.ts.map +1 -1
- package/dist/tools/cookies.js +5 -3
- package/dist/tools/cookies.js.map +1 -1
- package/dist/tools/diagnostics.d.ts +3 -3
- package/dist/tools/diagnostics.d.ts.map +1 -1
- package/dist/tools/diagnostics.js +13 -12
- package/dist/tools/diagnostics.js.map +1 -1
- package/dist/tools/evaluate.d.ts.map +1 -1
- package/dist/tools/evaluate.js +57 -26
- package/dist/tools/evaluate.js.map +1 -1
- package/dist/tools/extract.d.ts.map +1 -1
- package/dist/tools/extract.js +107 -175
- package/dist/tools/extract.js.map +1 -1
- package/dist/tools/input.d.ts +1 -1
- package/dist/tools/input.d.ts.map +1 -1
- package/dist/tools/input.js +380 -158
- package/dist/tools/input.js.map +1 -1
- package/dist/tools/logs.d.ts.map +1 -1
- package/dist/tools/logs.js +5 -5
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/manage.d.ts.map +1 -1
- package/dist/tools/manage.js +380 -57
- package/dist/tools/manage.js.map +1 -1
- package/dist/tools/page-context.d.ts +17 -0
- package/dist/tools/page-context.d.ts.map +1 -0
- package/dist/tools/page-context.js +103 -0
- package/dist/tools/page-context.js.map +1 -0
- package/dist/tools/png.d.ts +5 -0
- package/dist/tools/png.d.ts.map +1 -1
- package/dist/tools/png.js +84 -19
- package/dist/tools/png.js.map +1 -1
- package/dist/tools/post-condition.d.ts.map +1 -1
- package/dist/tools/post-condition.js +5 -2
- package/dist/tools/post-condition.js.map +1 -1
- package/dist/tools/schema.d.ts +9 -0
- package/dist/tools/schema.d.ts.map +1 -1
- package/dist/tools/schema.js +15 -0
- package/dist/tools/schema.js.map +1 -1
- package/dist/tools/target-diagnostics.d.ts.map +1 -1
- package/dist/tools/target-diagnostics.js +32 -19
- package/dist/tools/target-diagnostics.js.map +1 -1
- package/dist/tools/wait.d.ts.map +1 -1
- package/dist/tools/wait.js +19 -25
- package/dist/tools/wait.js.map +1 -1
- package/extension/dist/assets/content.ts-C4imZ6A9.js +13 -0
- package/extension/dist/assets/content.ts-loader-CnJuCGhl.js +13 -0
- package/extension/dist/assets/{index.ts-Bqcu2qiN.js → index.ts-GeHNZBml.js} +2040 -2111
- package/extension/dist/assets/page-scripts-CMIY1zhd.js +1379 -0
- package/extension/dist/assets/{popup.html-B81xccfq.js → popup.html-CgtWL6Py.js} +48 -1
- package/extension/dist/manifest.json +4 -3
- package/extension/dist/service-worker-loader.js +1 -1
- package/extension/dist/src/popup/popup.html +11 -1
- package/package.json +9 -7
- package/scripts/start-chrome.ps1 +58 -0
- package/extension/dist/assets/content.ts-Cj_iiU2s.js +0 -25
package/dist/tools/extract.js
CHANGED
|
@@ -10,13 +10,14 @@
|
|
|
10
10
|
* - state: 页面状态(精简的可交互元素列表)
|
|
11
11
|
* - metadata: 页面元信息(title/og/jsonLd 等)
|
|
12
12
|
*/
|
|
13
|
-
import { readFile } from 'fs/promises';
|
|
13
|
+
import { readFile, stat } from 'fs/promises';
|
|
14
14
|
import { basename, extname, join } from 'path';
|
|
15
15
|
import { z } from 'zod';
|
|
16
16
|
import { CWD_PATH_PREFIX, formatErrorResponse, formatResponse, getSession, getUnifiedSession, resolveScopedOutputPath, TMP_PATH_PREFIX, writePrivateFile, } from '../core/index.js';
|
|
17
17
|
import { sanitizeUrlRecords } from './network-sanitizer.js';
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
18
|
+
import { withPageContext } from './page-context.js';
|
|
19
|
+
import { assertPngFileBudget, assertScreenshotBase64Budget, assertScreenshotBudget, comparePngImages, MAX_COMPARE_PIXELS, MAX_COMPARE_PNG_BYTES, readPngHeader, } from './png.js';
|
|
20
|
+
import { pageTargetIdSchema, resolvePageTargetId, targetToFindParams, targetZodSchema } from './schema.js';
|
|
20
21
|
import { buildTargetDiagnostics, TargetNotFoundError, TargetTimeoutError } from './target-diagnostics.js';
|
|
21
22
|
/** 无 output 时附录返回的最大图片数 */
|
|
22
23
|
const MAX_APPENDIX_IMAGES = 20;
|
|
@@ -30,7 +31,7 @@ const extractSchema = z
|
|
|
30
31
|
.describe('提取类型'),
|
|
31
32
|
target: targetZodSchema
|
|
32
33
|
.optional()
|
|
33
|
-
.describe('目标元素(attribute 必填;text/html 可选,省略则提取整个页面;' +
|
|
34
|
+
.describe('目标元素(attribute 必填;text/html 可选,省略则提取整个页面;text 对 HTML 使用 innerText,SVG/XML 使用 textContent;' +
|
|
34
35
|
'screenshot 可选用于元素截图;state 可选(仅 Extension)用于返回目标子树;metadata 不需要)'),
|
|
35
36
|
attribute: z.string().optional().describe('属性名(attribute)'),
|
|
36
37
|
images: z
|
|
@@ -67,10 +68,8 @@ const extractSchema = z
|
|
|
67
68
|
.string()
|
|
68
69
|
.optional()
|
|
69
70
|
.describe(`输出文件路径(可选),相对路径默认写入 ${TMP_PATH_PREFIX},持久化到仓库请显式写 ${CWD_PATH_PREFIX},images=data 时作为输出目录路径`),
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
.optional()
|
|
73
|
-
.describe('目标 Tab ID(可选,仅 Extension 模式),不指定则使用当前 attach 的 tab,可操作非当前 attach 的 tab,CDP 模式下不支持此参数'),
|
|
71
|
+
targetId: pageTargetIdSchema.describe('目标页面 ID(Extension 或 CDP),优先于默认操作目标'),
|
|
72
|
+
tabId: pageTargetIdSchema.describe('旧版目标 Tab ID,与 targetId 相同语义,保留兼容'),
|
|
74
73
|
depth: z
|
|
75
74
|
.number()
|
|
76
75
|
.int()
|
|
@@ -101,7 +100,7 @@ async function handleExtract(args) {
|
|
|
101
100
|
try {
|
|
102
101
|
const unifiedSession = getUnifiedSession();
|
|
103
102
|
// 多 tab 并行:临时切换到指定 tab
|
|
104
|
-
return await unifiedSession
|
|
103
|
+
return await withPageContext(unifiedSession, resolvePageTargetId(args), async () => {
|
|
105
104
|
return await unifiedSession.withFrame(args.frame, async () => {
|
|
106
105
|
const useExtension = unifiedSession.getMode() === 'extension';
|
|
107
106
|
const frameHtmlError = validateFrameHtmlArgs(args, useExtension);
|
|
@@ -171,7 +170,7 @@ async function handleExtractInFrame(context, args) {
|
|
|
171
170
|
}
|
|
172
171
|
async function handleTextExtract({ unifiedSession, session, useExtension }, args) {
|
|
173
172
|
const text = useExtension
|
|
174
|
-
? await extractTextExtension(unifiedSession, args.target)
|
|
173
|
+
? await extractTextExtension(unifiedSession, args.target, args.timeout)
|
|
175
174
|
: await extractText(session, args.target, args.timeout);
|
|
176
175
|
if (args.output) {
|
|
177
176
|
const outputPath = await writeOutputFile(args.output, text, 'utf-8');
|
|
@@ -185,7 +184,7 @@ async function handleHtmlExtract(context, args) {
|
|
|
185
184
|
return handleHtmlWithImages(unifiedSession, session, useExtension, args);
|
|
186
185
|
}
|
|
187
186
|
const html = useExtension
|
|
188
|
-
? await extractHtmlExtension(unifiedSession, args.target)
|
|
187
|
+
? await extractHtmlExtension(unifiedSession, args.target, true, args.timeout)
|
|
189
188
|
: await extractHTML(session, args.target, args.timeout);
|
|
190
189
|
if (args.output) {
|
|
191
190
|
const outputPath = await writeOutputFile(args.output, html, 'utf-8');
|
|
@@ -194,7 +193,7 @@ async function handleHtmlExtract(context, args) {
|
|
|
194
193
|
return formatResponse({ success: true, type: 'html', content: html });
|
|
195
194
|
}
|
|
196
195
|
async function handleFrameHtmlExtract({ unifiedSession }, args) {
|
|
197
|
-
const html = await extractHtmlExtension(unifiedSession, args.target);
|
|
196
|
+
const html = await extractHtmlExtension(unifiedSession, args.target, true, args.timeout);
|
|
198
197
|
if (args.output) {
|
|
199
198
|
const outputPath = await writeOutputFile(args.output, html, 'utf-8');
|
|
200
199
|
return formatResponse({
|
|
@@ -215,7 +214,7 @@ async function handleAttributeExtract({ unifiedSession, session, useExtension },
|
|
|
215
214
|
return formatErrorResponse(new Error('attribute 提取需要 attribute 参数'));
|
|
216
215
|
}
|
|
217
216
|
const value = useExtension
|
|
218
|
-
? await extractAttributeExtension(unifiedSession, args.target, args.attribute)
|
|
217
|
+
? await extractAttributeExtension(unifiedSession, args.target, args.attribute, args.timeout)
|
|
219
218
|
: await extractAttribute(session, args.target, args.attribute, args.timeout);
|
|
220
219
|
return formatResponse({ success: true, type: 'attribute', attribute: args.attribute, value });
|
|
221
220
|
}
|
|
@@ -224,8 +223,23 @@ async function handleScreenshotExtract({ unifiedSession, useExtension }, args) {
|
|
|
224
223
|
if (args.target && !args.clip) {
|
|
225
224
|
const { selector, text, xpath, nth: nthParam } = targetToFindParams(args.target);
|
|
226
225
|
const nth = nthParam ?? 0;
|
|
227
|
-
const
|
|
228
|
-
const
|
|
226
|
+
const pointTarget = 'x' in args.target && 'y' in args.target ? args.target : null;
|
|
227
|
+
const found = pointTarget
|
|
228
|
+
? useExtension
|
|
229
|
+
? await unifiedSession.findAtPoint(pointTarget.x, pointTarget.y, nth, args.timeout)
|
|
230
|
+
: await unifiedSession
|
|
231
|
+
.evaluate(`(() => {
|
|
232
|
+
const element = document.elementFromPoint(${pointTarget.x}, ${pointTarget.y});
|
|
233
|
+
if (!element) return null;
|
|
234
|
+
const rect = element.getBoundingClientRect();
|
|
235
|
+
return {
|
|
236
|
+
refId: '', tag: element.tagName.toLowerCase(), text: '',
|
|
237
|
+
rect: { x: rect.x, y: rect.y, width: rect.width, height: rect.height }
|
|
238
|
+
};
|
|
239
|
+
})()`, undefined, args.timeout)
|
|
240
|
+
.then((element) => (element && nth === 0 ? [element] : []))
|
|
241
|
+
: await unifiedSession.find(selector, text, xpath, args.timeout);
|
|
242
|
+
const target = found[pointTarget ? 0 : nth];
|
|
229
243
|
if (!target || target.rect.width <= 0 || target.rect.height <= 0) {
|
|
230
244
|
throw new TargetNotFoundError(await buildTargetDiagnostics(unifiedSession, args.target, {
|
|
231
245
|
nth,
|
|
@@ -237,10 +251,9 @@ async function handleScreenshotExtract({ unifiedSession, useExtension }, args) {
|
|
|
237
251
|
}));
|
|
238
252
|
}
|
|
239
253
|
const scrollOffset = args.frame === undefined ? await getPageScrollOffset(unifiedSession, args.timeout) : { x: 0, y: 0 };
|
|
240
|
-
const frameOffset = unifiedSession.getFrameOffset();
|
|
241
254
|
clip = {
|
|
242
|
-
x: target.rect.x + scrollOffset.x
|
|
243
|
-
y: target.rect.y + scrollOffset.y
|
|
255
|
+
x: target.rect.x + scrollOffset.x,
|
|
256
|
+
y: target.rect.y + scrollOffset.y,
|
|
244
257
|
width: target.rect.width,
|
|
245
258
|
height: target.rect.height,
|
|
246
259
|
};
|
|
@@ -248,6 +261,9 @@ async function handleScreenshotExtract({ unifiedSession, useExtension }, args) {
|
|
|
248
261
|
clip = args.clip ?? clip;
|
|
249
262
|
const fullPage = clip ? false : (args.fullPage ?? false);
|
|
250
263
|
const scale = args.scale ?? 1;
|
|
264
|
+
if (clip) {
|
|
265
|
+
assertScreenshotBudget(clip.width, clip.height, scale);
|
|
266
|
+
}
|
|
251
267
|
const screenshot = await unifiedSession.screenshot({
|
|
252
268
|
fullPage,
|
|
253
269
|
scale: args.scale,
|
|
@@ -255,6 +271,7 @@ async function handleScreenshotExtract({ unifiedSession, useExtension }, args) {
|
|
|
255
271
|
quality: args.quality,
|
|
256
272
|
clip,
|
|
257
273
|
});
|
|
274
|
+
assertScreenshotBase64Budget(screenshot.data);
|
|
258
275
|
const screenshotBuffer = Buffer.from(screenshot.data, 'base64');
|
|
259
276
|
const encodedDimensions = readImageDimensions(screenshotBuffer, screenshot.format);
|
|
260
277
|
const fallbackDimensions = encodedDimensions
|
|
@@ -271,14 +288,16 @@ async function handleScreenshotExtract({ unifiedSession, useExtension }, args) {
|
|
|
271
288
|
clip,
|
|
272
289
|
size: screenshotBuffer.length,
|
|
273
290
|
byteSize: screenshotBuffer.length,
|
|
274
|
-
capabilities: screenshotCapabilities(
|
|
291
|
+
capabilities: screenshotCapabilities(),
|
|
275
292
|
};
|
|
276
293
|
if (args.compareWith) {
|
|
277
294
|
if (screenshot.format !== 'png') {
|
|
278
295
|
throw new Error('screenshot compare 仅支持 png 格式');
|
|
279
296
|
}
|
|
280
297
|
const baselinePath = await resolveScopedOutputPath(args.compareWith, 'mcp-chrome');
|
|
298
|
+
assertPngFileBudget((await stat(baselinePath.absolutePath)).size);
|
|
281
299
|
const baseline = await readFile(baselinePath.absolutePath);
|
|
300
|
+
assertPngFileBudget(baseline.length);
|
|
282
301
|
const comparison = comparePngImages(baseline, screenshotBuffer);
|
|
283
302
|
metadata.comparison = {
|
|
284
303
|
pixelDiffRatio: comparison.pixelDiffRatio,
|
|
@@ -313,11 +332,20 @@ async function handleStateExtract({ unifiedSession, useExtension }, args) {
|
|
|
313
332
|
}
|
|
314
333
|
let refId;
|
|
315
334
|
if (args.target && useExtension) {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
335
|
+
refId = await findTargetRefExtension(unifiedSession, args.target, args.timeout);
|
|
336
|
+
if (!refId) {
|
|
337
|
+
const { selector, text, xpath, nth: nthParam, } = targetToFindParams(args.target);
|
|
338
|
+
const candidates = 'x' in args.target && 'y' in args.target
|
|
339
|
+
? await unifiedSession.findAtPoint(args.target.x, args.target.y)
|
|
340
|
+
: await unifiedSession.find(selector, text, xpath, args.timeout);
|
|
341
|
+
throw new TargetNotFoundError(await buildTargetDiagnostics(unifiedSession, args.target, {
|
|
342
|
+
nth: nthParam ?? 0,
|
|
343
|
+
timeout: args.timeout,
|
|
344
|
+
frame: args.frame,
|
|
345
|
+
matchCount: candidates.length,
|
|
346
|
+
lastState: 'not-found',
|
|
347
|
+
candidates,
|
|
348
|
+
}));
|
|
321
349
|
}
|
|
322
350
|
}
|
|
323
351
|
const readPageOptions = {};
|
|
@@ -362,7 +390,7 @@ async function handleDomSnapshotState(unifiedSession, useExtension, args) {
|
|
|
362
390
|
}
|
|
363
391
|
return formatResponse({ success: true, type: 'state', mode: 'domsnapshot', snapshot });
|
|
364
392
|
}
|
|
365
|
-
async function handleDiagnosticBundleExtract({ unifiedSession
|
|
393
|
+
async function handleDiagnosticBundleExtract({ unifiedSession }, args) {
|
|
366
394
|
const state = await unifiedSession.getLiveState().catch(() => null);
|
|
367
395
|
const metadata = await unifiedSession.getMetadata();
|
|
368
396
|
const frames = await unifiedSession.getFrames();
|
|
@@ -390,7 +418,7 @@ async function handleDiagnosticBundleExtract({ unifiedSession, useExtension }, a
|
|
|
390
418
|
},
|
|
391
419
|
capabilities: {
|
|
392
420
|
screenshot: true,
|
|
393
|
-
hiddenTabScreenshot:
|
|
421
|
+
hiddenTabScreenshot: false,
|
|
394
422
|
frames: true,
|
|
395
423
|
},
|
|
396
424
|
};
|
|
@@ -473,13 +501,13 @@ async function getScreenshotFallbackDimensions(unifiedSession, clip, fullPage, s
|
|
|
473
501
|
return undefined;
|
|
474
502
|
}
|
|
475
503
|
}
|
|
476
|
-
function screenshotCapabilities(
|
|
504
|
+
function screenshotCapabilities() {
|
|
477
505
|
return {
|
|
478
506
|
formats: ['png', 'jpeg', 'webp'],
|
|
479
507
|
clip: true,
|
|
480
508
|
fullPage: true,
|
|
481
509
|
scale: true,
|
|
482
|
-
hiddenTabScreenshot:
|
|
510
|
+
hiddenTabScreenshot: false,
|
|
483
511
|
compare: {
|
|
484
512
|
formats: ['png'],
|
|
485
513
|
maxBytes: MAX_COMPARE_PNG_BYTES,
|
|
@@ -587,17 +615,35 @@ async function handleHtmlWithImages(unifiedSession, session, useExtension, args)
|
|
|
587
615
|
if (args.target) {
|
|
588
616
|
const { selector, text, xpath, nth: nthParam } = targetToFindParams(args.target);
|
|
589
617
|
const nth = nthParam ?? 0;
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
618
|
+
if (useExtension) {
|
|
619
|
+
const refId = await findTargetRefExtension(unifiedSession, args.target, args.timeout);
|
|
620
|
+
if (!refId) {
|
|
621
|
+
const matches = 'x' in args.target && 'y' in args.target
|
|
622
|
+
? await unifiedSession.findAtPoint(args.target.x, args.target.y)
|
|
623
|
+
: await unifiedSession.find(selector, text, xpath, args.timeout);
|
|
624
|
+
throw new TargetNotFoundError(await buildTargetDiagnostics(unifiedSession, args.target, {
|
|
625
|
+
nth,
|
|
626
|
+
timeout: args.timeout,
|
|
627
|
+
matchCount: matches.length,
|
|
628
|
+
lastState: 'not-found',
|
|
629
|
+
candidates: matches,
|
|
630
|
+
}));
|
|
631
|
+
}
|
|
632
|
+
result = await unifiedSession.getHtmlWithImagesByRef(refId);
|
|
633
|
+
}
|
|
634
|
+
else {
|
|
635
|
+
const matches = await unifiedSession.find(selector, text, xpath, args.timeout);
|
|
636
|
+
if (matches.length <= nth) {
|
|
637
|
+
throw new TargetNotFoundError(await buildTargetDiagnostics(unifiedSession, args.target, {
|
|
638
|
+
nth,
|
|
639
|
+
timeout: args.timeout,
|
|
640
|
+
matchCount: matches.length,
|
|
641
|
+
lastState: 'not-found',
|
|
642
|
+
candidates: matches,
|
|
643
|
+
}));
|
|
644
|
+
}
|
|
645
|
+
result = await extractHtmlWithImagesByTarget(unifiedSession, selector, text, xpath, nth, args.timeout);
|
|
599
646
|
}
|
|
600
|
-
result = await extractHtmlWithImagesByTarget(unifiedSession, selector, text, xpath, nth, args.timeout);
|
|
601
647
|
}
|
|
602
648
|
else {
|
|
603
649
|
result = useExtension
|
|
@@ -948,7 +994,7 @@ async function extractText(session, target, timeout) {
|
|
|
948
994
|
if (target) {
|
|
949
995
|
const locator = session.createLocator(target, timeout !== undefined ? { timeout } : undefined);
|
|
950
996
|
const text = await locator.evaluateOn(`function() {
|
|
951
|
-
return this.textContent ?? '';
|
|
997
|
+
return this instanceof HTMLElement ? this.innerText : (this.textContent ?? '');
|
|
952
998
|
}`);
|
|
953
999
|
return text ?? '';
|
|
954
1000
|
}
|
|
@@ -991,162 +1037,47 @@ async function extractAttribute(session, target, attribute, timeout) {
|
|
|
991
1037
|
return this.getAttribute(${JSON.stringify(attribute)});
|
|
992
1038
|
}`);
|
|
993
1039
|
}
|
|
1040
|
+
async function findTargetRefExtension(unifiedSession, target, timeout) {
|
|
1041
|
+
const { selector, text, xpath, nth } = targetToFindParams(target);
|
|
1042
|
+
const matches = 'x' in target && 'y' in target
|
|
1043
|
+
? await unifiedSession.findAtPoint(target.x, target.y, nth ?? 0, timeout)
|
|
1044
|
+
: await unifiedSession.find(selector, text, xpath, timeout, nth ?? 0);
|
|
1045
|
+
return matches[0]?.refId;
|
|
1046
|
+
}
|
|
994
1047
|
/**
|
|
995
1048
|
* Extension 模式:提取文本
|
|
996
1049
|
* 支持所有 Target 形式(css/xpath/text/role/label 等)
|
|
997
1050
|
*/
|
|
998
|
-
async function extractTextExtension(unifiedSession, target) {
|
|
1051
|
+
async function extractTextExtension(unifiedSession, target, timeout) {
|
|
999
1052
|
if (!target) {
|
|
1000
1053
|
return unifiedSession.getText();
|
|
1001
1054
|
}
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
"return el ? (el.textContent || '') : '' })";
|
|
1005
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [target.x, target.y]);
|
|
1006
|
-
}
|
|
1007
|
-
const { selector, text, xpath, nth: nthParam } = targetToFindParams(target);
|
|
1008
|
-
const nth = nthParam ?? 0;
|
|
1009
|
-
if (selector) {
|
|
1010
|
-
if (text) {
|
|
1011
|
-
const expr = '(function(s, t, n) { var els = Array.from(document.querySelectorAll(s))' +
|
|
1012
|
-
'.filter(function(e) { return (e.textContent || "").includes(t); }); ' +
|
|
1013
|
-
"return n < els.length ? (els[n].textContent || '') : '' })";
|
|
1014
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [selector, text, nth]);
|
|
1015
|
-
}
|
|
1016
|
-
if (nth > 0) {
|
|
1017
|
-
const expr = '(function(s, n) { var els = document.querySelectorAll(s); ' +
|
|
1018
|
-
"return n < els.length ? (els[n].textContent || '') : '' })";
|
|
1019
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [selector, nth]);
|
|
1020
|
-
}
|
|
1021
|
-
return unifiedSession.getText(selector);
|
|
1022
|
-
}
|
|
1023
|
-
// xpath/text 定位:通过 evaluate 在页面上下文中查找
|
|
1024
|
-
if (xpath) {
|
|
1025
|
-
const expr = '(function(xp, n) { var r = document.evaluate(xp, document, null, ' +
|
|
1026
|
-
'XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); ' +
|
|
1027
|
-
"return n < r.snapshotLength ? (r.snapshotItem(n).textContent || '') : '' })";
|
|
1028
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [xpath, nth]);
|
|
1029
|
-
}
|
|
1030
|
-
if (text) {
|
|
1031
|
-
const expr = '(function(t, n) { var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT); ' +
|
|
1032
|
-
'var found = []; var seen = new WeakSet(); var node; ' +
|
|
1033
|
-
'while ((node = walker.nextNode())) { if (node.textContent && node.textContent.includes(t) ' +
|
|
1034
|
-
'&& node.parentElement && !seen.has(node.parentElement)) { ' +
|
|
1035
|
-
'seen.add(node.parentElement); found.push(node.parentElement); } } ' +
|
|
1036
|
-
"return n < found.length ? (found[n].textContent || '') : '' })";
|
|
1037
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [text, nth]);
|
|
1038
|
-
}
|
|
1039
|
-
return unifiedSession.getText();
|
|
1055
|
+
const refId = await findTargetRefExtension(unifiedSession, target, timeout);
|
|
1056
|
+
return refId ? unifiedSession.getTextByRef(refId) : '';
|
|
1040
1057
|
}
|
|
1041
1058
|
/**
|
|
1042
1059
|
* Extension 模式:提取 HTML
|
|
1043
1060
|
* 支持所有 Target 形式(css/xpath/text/role/label 等)
|
|
1044
1061
|
*/
|
|
1045
|
-
async function extractHtmlExtension(unifiedSession, target, outer = true) {
|
|
1062
|
+
async function extractHtmlExtension(unifiedSession, target, outer = true, timeout) {
|
|
1046
1063
|
if (!target) {
|
|
1047
1064
|
return unifiedSession.getHtml(undefined, outer);
|
|
1048
1065
|
}
|
|
1049
|
-
const
|
|
1050
|
-
|
|
1051
|
-
const expr = '(function(x, y, p) { var el = document.elementFromPoint(x, y); ' + "return el ? (el[p] || '') : '' })";
|
|
1052
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [target.x, target.y, prop]);
|
|
1053
|
-
}
|
|
1054
|
-
const { selector, text, xpath, nth: nthParam } = targetToFindParams(target);
|
|
1055
|
-
const nth = nthParam ?? 0;
|
|
1056
|
-
if (selector) {
|
|
1057
|
-
if (text) {
|
|
1058
|
-
const expr = '(function(s, t, n, p) { var els = Array.from(document.querySelectorAll(s))' +
|
|
1059
|
-
'.filter(function(e) { return (e.textContent || "").includes(t); }); ' +
|
|
1060
|
-
"return n < els.length ? (els[n][p] || '') : '' })";
|
|
1061
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [selector, text, nth, prop]);
|
|
1062
|
-
}
|
|
1063
|
-
if (nth > 0) {
|
|
1064
|
-
const expr = '(function(s, n, p) { var els = document.querySelectorAll(s); ' +
|
|
1065
|
-
"return n < els.length ? (els[n][p] || '') : '' })";
|
|
1066
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [selector, nth, prop]);
|
|
1067
|
-
}
|
|
1068
|
-
return unifiedSession.getHtml(selector, outer);
|
|
1069
|
-
}
|
|
1070
|
-
if (xpath) {
|
|
1071
|
-
const expr = '(function(xp, n, p) { var r = document.evaluate(xp, document, null, ' +
|
|
1072
|
-
'XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); ' +
|
|
1073
|
-
"return n < r.snapshotLength ? (r.snapshotItem(n)[p] || '') : '' })";
|
|
1074
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [xpath, nth, prop]);
|
|
1075
|
-
}
|
|
1076
|
-
if (text) {
|
|
1077
|
-
const expr = '(function(t, n, p) { var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT); ' +
|
|
1078
|
-
'var found = []; var seen = new WeakSet(); var node; ' +
|
|
1079
|
-
'while ((node = walker.nextNode())) { if (node.textContent && node.textContent.includes(t) ' +
|
|
1080
|
-
'&& node.parentElement && !seen.has(node.parentElement)) { ' +
|
|
1081
|
-
'seen.add(node.parentElement); found.push(node.parentElement); } } ' +
|
|
1082
|
-
"return n < found.length ? (found[n][p] || '') : '' })";
|
|
1083
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [text, nth, prop]);
|
|
1084
|
-
}
|
|
1085
|
-
return unifiedSession.getHtml(undefined, outer);
|
|
1066
|
+
const refId = await findTargetRefExtension(unifiedSession, target, timeout);
|
|
1067
|
+
return refId ? unifiedSession.getHtmlByRef(refId, outer) : '';
|
|
1086
1068
|
}
|
|
1087
1069
|
/**
|
|
1088
1070
|
* Extension 模式:提取属性
|
|
1089
1071
|
*/
|
|
1090
|
-
async function extractAttributeExtension(unifiedSession, target, attribute) {
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
const prop = attribute.slice('computed:'.length);
|
|
1094
|
-
const expr = '(function(x, y, p) { var el = document.elementFromPoint(x, y); ' +
|
|
1095
|
-
'return el ? window.getComputedStyle(el).getPropertyValue(p) : null })';
|
|
1096
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [target.x, target.y, prop]);
|
|
1097
|
-
}
|
|
1098
|
-
const expr = '(function(x, y, a) { var el = document.elementFromPoint(x, y); ' +
|
|
1099
|
-
'return el ? el.getAttribute(a) : null })';
|
|
1100
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [target.x, target.y, attribute]);
|
|
1101
|
-
}
|
|
1102
|
-
const { selector, text, xpath, nth: nthParam } = targetToFindParams(target);
|
|
1103
|
-
// computed style: computed:color → getComputedStyle(el)
|
|
1104
|
-
if (attribute.startsWith('computed:')) {
|
|
1105
|
-
const prop = attribute.slice('computed:'.length);
|
|
1106
|
-
return extractComputedStyleExtension(unifiedSession, selector, text, xpath, nthParam ?? 0, prop);
|
|
1107
|
-
}
|
|
1108
|
-
// xpath 定位(含 text+xpath)或 text 且无 selector 时:先 find 得到 refId,再获取属性
|
|
1109
|
-
if (xpath || (text && !selector)) {
|
|
1110
|
-
const elements = await unifiedSession.find(selector, text, xpath);
|
|
1111
|
-
if (elements.length > 0) {
|
|
1112
|
-
const nth = nthParam ?? 0;
|
|
1113
|
-
if (nth >= elements.length) {
|
|
1114
|
-
throw new Error(`第 ${nth} 个匹配元素不存在(共 ${elements.length} 个)`);
|
|
1115
|
-
}
|
|
1116
|
-
return unifiedSession.getAttribute(undefined, elements[nth].refId, attribute);
|
|
1117
|
-
}
|
|
1072
|
+
async function extractAttributeExtension(unifiedSession, target, attribute, timeout) {
|
|
1073
|
+
const refId = await findTargetRefExtension(unifiedSession, target, timeout);
|
|
1074
|
+
if (!refId) {
|
|
1118
1075
|
return null;
|
|
1119
1076
|
}
|
|
1120
|
-
if (
|
|
1121
|
-
|
|
1122
|
-
if (text) {
|
|
1123
|
-
// selector + text 组合:find 已实现 AND 过滤
|
|
1124
|
-
const elements = await unifiedSession.find(selector, text, undefined);
|
|
1125
|
-
if (nth >= elements.length) {
|
|
1126
|
-
return null;
|
|
1127
|
-
}
|
|
1128
|
-
return unifiedSession.getAttribute(undefined, elements[nth].refId, attribute);
|
|
1129
|
-
}
|
|
1130
|
-
if (nth > 0) {
|
|
1131
|
-
const expr = '(function(s, n, a) { var els = document.querySelectorAll(s); ' +
|
|
1132
|
-
'return n < els.length ? els[n].getAttribute(a) : null })';
|
|
1133
|
-
return unifiedSession.evaluate(expr, undefined, undefined, [selector, nth, attribute]);
|
|
1134
|
-
}
|
|
1135
|
-
return unifiedSession.getAttribute(selector, undefined, attribute);
|
|
1136
|
-
}
|
|
1137
|
-
return null;
|
|
1138
|
-
}
|
|
1139
|
-
/**
|
|
1140
|
-
* Extension 模式:提取 computed style
|
|
1141
|
-
*/
|
|
1142
|
-
async function extractComputedStyleExtension(unifiedSession, selector, text, xpath, nth, prop) {
|
|
1143
|
-
const elements = await unifiedSession.find(selector, text, xpath);
|
|
1144
|
-
if (elements.length === 0 || nth >= elements.length) {
|
|
1145
|
-
return null;
|
|
1077
|
+
if (attribute.startsWith('computed:')) {
|
|
1078
|
+
return unifiedSession.getComputedStyleByRef(refId, attribute.slice('computed:'.length));
|
|
1146
1079
|
}
|
|
1147
|
-
|
|
1148
|
-
// 通过 Extension ISOLATED 世界执行(访问 __mcpElementMap),避免 MAIN 世界找不到 refId
|
|
1149
|
-
return unifiedSession.getComputedStyle(refId, prop);
|
|
1080
|
+
return unifiedSession.getAttributeByRef(refId, attribute);
|
|
1150
1081
|
}
|
|
1151
1082
|
/**
|
|
1152
1083
|
* Extension 模式:等待目标元素出现
|
|
@@ -1185,10 +1116,11 @@ async function waitForTargetExtension(unifiedSession, target, timeout, frame) {
|
|
|
1185
1116
|
continue;
|
|
1186
1117
|
}
|
|
1187
1118
|
try {
|
|
1188
|
-
const
|
|
1119
|
+
const indexed = nth >= 50;
|
|
1120
|
+
const elements = await unifiedSession.find(selector, text, xpath, remaining, indexed ? nth : undefined);
|
|
1189
1121
|
matchCount = elements.length;
|
|
1190
1122
|
candidates = elements;
|
|
1191
|
-
if (elements.length > nth) {
|
|
1123
|
+
if (indexed ? elements.length > 0 : elements.length > nth) {
|
|
1192
1124
|
return;
|
|
1193
1125
|
}
|
|
1194
1126
|
}
|