executeJavaScript
Run JavaScript code directly on web pages to extract data, manipulate content, or automate interactions during browser automation tasks.
Instructions
Execute arbitrary JavaScript code on the current page and return the result
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | The JavaScript code to execute on the page. Can be expressions or statements. |
Implementation Reference
- src/controllers/playwright.ts:489-517 (handler)The core handler function in PlaywrightController that executes arbitrary JavaScript on the current page using page.evaluate, handling both expressions and statements, with logging and error handling.async executeJavaScript(script: string): Promise<any> { try { if (!this.isInitialized()) { throw new Error('Browser not initialized'); } this.log('Executing JavaScript:', script); const result = await this.state.page?.evaluate((scriptToExecute) => { // Create a function wrapper to handle different types of JavaScript code try { // If the script is an expression, return its value // If the script is statements, execute them and return undefined const wrappedScript = ` (function() { ${scriptToExecute} })() `; return eval(wrappedScript); } catch (error) { // If wrapping fails, try executing directly return eval(scriptToExecute); } }, script); this.log('JavaScript execution completed:', result); return result; } catch (error: any) { console.error('Execute JavaScript error:', error); throw new BrowserError('Failed to execute JavaScript', 'Check if the JavaScript syntax is valid'); } }
- src/server.ts:195-208 (schema)Defines the input schema, name, and description for the executeJavaScript tool used for MCP validation.const EXECUTE_JAVASCRIPT_TOOL: Tool = { name: "executeJavaScript", description: "Execute arbitrary JavaScript code on the current page and return the result", inputSchema: { type: "object", properties: { script: { type: "string", description: "The JavaScript code to execute on the page. Can be expressions or statements." } }, required: ["script"] } };
- src/server.ts:555-565 (registration)Registers all tools, including executeJavaScript, by passing the 'tools' object to the MCP Server's capabilities.const server = new Server( { name: "playmcp-browser", version: "1.0.0", }, { capabilities: { tools, }, } );
- src/server.ts:772-786 (helper)The dispatch logic in the MCP 'callTool' request handler that validates input, calls the controller's executeJavaScript method, and formats the response.case 'executeJavaScript': { if (!args.script || typeof args.script !== 'string') { return { content: [{ type: "text", text: "JavaScript script is required" }], isError: true }; } const result = await playwrightController.executeJavaScript(args.script); return { content: [{ type: "text", text: result !== undefined ? JSON.stringify(result, null, 2) : "Script executed successfully (no return value)" }] }; }
- src/server.ts:534-534 (registration)Adds the executeJavaScript tool to the central 'tools' dictionary used for MCP tool listing and capabilities.executeJavaScript: EXECUTE_JAVASCRIPT_TOOL,