forked from ChromeDevTools/chrome-devtools-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshot.ts
More file actions
86 lines (74 loc) · 2.47 KB
/
snapshot.ts
File metadata and controls
86 lines (74 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {Locator} from 'puppeteer-core';
import z from 'zod';
import {formatA11ySnapshot} from '../formatters/snapshotFormatter.js';
import {ToolCategories} from './categories.js';
import {defineTool, timeoutSchema} from './ToolDefinition.js';
export const takeSnapshot = defineTool({
name: 'take_snapshot',
description: `Take a text snapshot of the currently selected page. The snapshot lists page elements along with a unique
identifier (uid). Always use the latest snapshot. Prefer taking a snapshot over taking a screenshot.`,
annotations: {
category: ToolCategories.DEBUGGING,
readOnlyHint: true,
},
schema: {
filePath: z
.string()
.optional()
.describe(
'The absolute path, or a path relative to the current working directory, to save the snapshot to instead of including it in the response.',
),
},
handler: async (request, response, context) => {
await context.createTextSnapshot();
const snapshot = context.getTextSnapshot();
if (!snapshot) {
response.appendResponseLine('No snapshot data available.');
return;
}
const formattedSnapshot = formatA11ySnapshot(snapshot.root);
if (request.params.filePath) {
const encoder = new TextEncoder();
const data = encoder.encode(formattedSnapshot);
const file = await context.saveFile(data, request.params.filePath);
response.appendResponseLine(`Saved snapshot to ${file.filename}.`);
} else {
response.setIncludeSnapshot(true);
}
},
});
export const waitFor = defineTool({
name: 'wait_for',
description: `Wait for the specified text to appear on the selected page.`,
annotations: {
category: ToolCategories.NAVIGATION_AUTOMATION,
readOnlyHint: true,
},
schema: {
text: z.string().describe('Text to appear on the page'),
...timeoutSchema,
},
handler: async (request, response, context) => {
const page = context.getSelectedPage();
const frames = page.frames();
const locator = Locator.race(
frames.flatMap(frame => [
frame.locator(`aria/${request.params.text}`),
frame.locator(`text/${request.params.text}`),
]),
);
if (request.params.timeout) {
locator.setTimeout(request.params.timeout);
}
await locator.wait();
response.appendResponseLine(
`Element with text "${request.params.text}" found.`,
);
response.setIncludeSnapshot(true);
},
});