evaluate_javascript
Execute JavaScript code directly in the browser to manipulate page content, extract data, or run custom scripts for automated testing and interaction.
Instructions
Execute JavaScript in the browser context
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- src/index.ts:55-57 (schema)Zod schema for evaluate_javascript input validation, defining a required 'script' string parameter.
const EvaluateJavaScriptSchema = z.object({ script: z.string() }); - src/index.ts:281-294 (registration)Tool registration for 'evaluate_javascript' in the ListToolsRequestSchema handler, declaring name, description, and input schema.
{ name: 'evaluate_javascript', description: 'Execute JavaScript in the browser context', inputSchema: { type: 'object', properties: { script: { type: 'string', description: 'JavaScript code to execute' } }, required: ['script'] } }, - src/index.ts:583-599 (handler)Handler for 'evaluate_javascript' tool call. Parses args with the schema, executes the script via Playwright's page.evaluate(), and returns the JSON-stringified result.
case 'evaluate_javascript': { if (!currentPage) { throw new Error('No browser page available. Launch a browser first.'); } const params = EvaluateJavaScriptSchema.parse(args); const result = await currentPage.evaluate(params.script); return { content: [ { type: 'text', text: `JavaScript result: ${JSON.stringify(result, null, 2)}` } ] }; }