-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathscript.ts
More file actions
40 lines (37 loc) · 1.29 KB
/
script.ts
File metadata and controls
40 lines (37 loc) · 1.29 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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import z from 'zod';
import {defineTool} from './ToolDefinition.js';
import {ToolCategories} from './categories.js';
import {waitForEventsAfterAction} from '../waitForHelpers.js';
export const evaluateScript = defineTool({
name: 'evaluate_script',
description: `Evaluate a JavaScript function inside the currently selected page. Returns the response as JSON.`,
annotations: {
category: ToolCategories.DEBUGGING,
readOnlyHint: false,
},
schema: {
function: z
.string()
.describe(
'A JavaScript function to run in the currently selected page. Example: `() => {return document.title}` or `async () => {return await fetch("example.com")}`',
),
},
handler: async (request, response, context) => {
const page = context.getSelectedPage();
const script = `(async () => {
return JSON.stringify(await (${request.params.function})());
})()`;
await waitForEventsAfterAction(page, async () => {
const result = await page.evaluate(script);
response.appendResponseLine('Script ran on page and returned:');
response.appendResponseLine('```json');
response.appendResponseLine(`${result}`);
response.appendResponseLine('```');
});
},
});