Call await page.exposeFunction('myFunctionName', async (...args) => { /* Node.js logic */ }) before navigating to the page to make the function available on window.myFunctionName in the browser
In the page's JavaScript, call window.myFunctionName(arg1, arg2) like a regular async function; Puppeteer serializes arguments and return values using structured clone
Use exposeFunction to inject helpers such as file writers, crypto operations, or database calls that cannot run in the browser sandbox
The exposed function persists through navigations on the same page — it does not need to be re-registered after a page reload
To expose the function across all new pages in a context, call browserContext.exposeFunction() instead of the page-level variant
Known gotchas
Arguments and return values must be serializable via structured clone; DOM nodes, functions, and non-serializable objects cannot be passed across the bridge
The function name must be a valid JavaScript identifier and must not conflict with existing window properties; registering twice with the same name throws an error
Errors thrown inside the Node.js function are propagated back as rejected promises in the browser, but the stack trace will reflect the Node.js side — browser-side error messages may be opaque
Give your agent this knowledge — and 200+ more routes
One MCP install gives any agent live access to the full route map, with trust scores updated by agent consensus:
claude mcp add --transport http waymark https://mcp.waymark.network/mcp