October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Dynamically Inject Functions for Puppeteer Evaluation

A practical guide to running functions in the page, injecting setup before scripts, and exposing Node.js callbacks with Puppeteer.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use page.evaluate(fn, ...args) to run a function in the current page, page.evaluateOnNewDocument(fn, ...args) to install setup before scripts run in new documents, and page.exposeFunction(name, fn) when page JavaScript needs to call back into Node.js. These APIs cross execution contexts: pass browser callbacks the values they need instead of expecting them to see Node.js variables.

Run a function in the current page with page.evaluate()

Puppeteer’s Page.evaluate() API evaluates a function in the page’s context and returns its result. Pass any inputs after the callback. If the callback returns a Promise, Puppeteer waits for it to resolve.

As an Amazon Associate I earn from qualifying purchases.

const result = await page.evaluate((selector) => {
  return document.querySelector(selector)?.textContent?.trim() ?? null;
}, '#headline');

The callback runs in the browser, not in the Node.js process. It cannot automatically read variables from the surrounding Node.js lexical scope. Supply those values as arguments:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const prefix = 'Result: ';
const result = await page.evaluate((selector, prefix) => {
  return prefix + (document.querySelector(selector)?.textContent ?? '');
}, '#headline', prefix);

Keep the callback self-contained with respect to browser APIs and pass data across the boundary explicitly. This makes it clear which values the page-side function receives.

Choose the API by timing and call direction

Need API Behavior
Run work now in the current page page.evaluate(fn, ...args) Runs in the page context; Puppeteer waits for a returned Promise to resolve. Puppeteer API
Install code before page scripts in new documents page.evaluateOnNewDocument(fn, ...args) Runs after document creation and before page scripts. It also runs on navigation and in child frames when they attach or navigate. Puppeteer API
Let browser JavaScript call Node.js page.exposeFunction(name, fn) Adds a function on window; its result is returned through a Promise, and the exposed function survives navigation. Puppeteer API

Install code before the page loads

Register evaluateOnNewDocument() before navigating if the code must affect that navigation. It is a lifecycle hook for new documents, not a way to retroactively alter a document that has already loaded.

await page.evaluateOnNewDocument((language) => {
  Object.defineProperty(navigator, 'language', { get: () => language });
}, 'en-US');

await page.goto(url);

The callback runs after the document is created but before its scripts execute. The supplied argument is available to the callback in the page context.

Let page code call a Node.js function

Use exposeFunction() when the direction of the call is from page JavaScript to Node.js. The exposed callback itself is implemented in Node; it is not serialized into the page.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
await page.exposeFunction('lookupRecord', async (id) => {
  return await getRecordFromNode(id);
});

const record = await page.evaluate(() => window.lookupRecord('item-42'));

Here, getRecordFromNode belongs to the Node.js process. Page code invokes the exposed window.lookupRecord, and the returned value can be awaited in the page.

Pass functions and arguments, not generated source strings

Puppeteer serializes function callbacks using Function.prototype.toString(). Its troubleshooting guidance warns that transpilers can change generated function output in ways that are incompatible with evaluation. That is a compatibility risk, not evidence that every transpiler fails.

For ordinary evaluation, prefer a callback plus explicit arguments to assembling source text. This avoids hand-interpolating data into code and makes the context boundary visible. If a transpiled callback fails, inspect the function form Puppeteer receives and try a simple, untransformed callback to isolate serialization as the cause.

Troubleshoot “Cannot evaluate a string with arguments”

This error means arguments are being supplied with source text rather than a function callback. Arguments belong to a function passed to evaluation; they are not automatically substituted into a string of JavaScript source. Use this pattern:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const value = await page.evaluate((name) => {
  return document.querySelector(name)?.textContent ?? null;
}, '#headline');

If the callback needs a Node.js value, pass it as an argument rather than referring to the outer variable from inside the callback.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When Content Security Policy is involved

Evaluation is the normal way to run a callback in page context; do not start by bypassing a site’s Content Security Policy. If your use case specifically requires a CSP bypass, Puppeteer’s Page documentation for setBypassCSP() says bypassing takes effect at CSP initialization. Set it before navigating to the domain, rather than after the page has loaded.

Or skip the browser setup

If the task is to capture a website rather than run custom Puppeteer logic, ScreenshotNeo offers a website screenshot API and MCP server for developers. A one-call request returns a screenshot or PDF; use the ScreenshotNeo API documentation for request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
  • Cookie banners are accepted and removed, along with known consent platforms, newsletter popups and chat widgets, before the shot; each step can be turned off.
  • Bot checks, blank pages, timeouts, failed loads and cache hits are not billed. The response identifies page verdict and billing status in headers.
  • An MCP server provides take_screenshot, get_page_info and capture_pdf tools for Claude, Cursor and other MCP clients.
  • The Free plan includes 1,000 screenshots per month with no card. Paid plans start at $5 for 3,000 screenshots.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.