Run JavaScript
execute_javascriptRun JavaScript in the page's own context to read globals, call page functions, or extract structured data in one pass, returning the result as JSON.
Instructions
Execute JavaScript in the page's own context and return its value as JSON.
The escape hatch for anything the dedicated tools do not cover: reading
page globals, calling page functions, extracting structured data in one
pass. Takes an expression, or a statement block that uses return (run
as the body of an async function, so await works too). It runs in the
page's main world, so it sees the page's variables and the page could
in principle observe it; prefer the dedicated tools where they apply.
Nothing is rolled back: whatever the script changed before it threw
stays, and a script that times out may still be running in the page.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"script": "<script>"
},
"name": "execute_javascript"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| tab | No | Tab id from list_tabs or new_tab to act on, instead of the active tab. The active tab does not change. Omit to use the active tab. Example: 'tab_2' | |
| script | Yes | JavaScript to run in the page: an expression ('document.title'), or statements that end in a return ('const n = document.links.length; return n'). Promises are awaited. Example: 'return [...document.querySelectorAll("h2")].map(h => h.textContent)' | |
| timeout | No | Seconds to wait for the result, promises included, between 1 and 115. Raise it for scripts that poll or retry. Example: 45 |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| value | No | The expression's value, as JSON (null if none). | |
| summary | Yes | One line saying what happened. | |
| has_value | Yes | False when the expression produced undefined/null. |