Skip to content

feat(page): support a callback in addInitScript#41861

Open
dcrousso wants to merge 1 commit into
microsoft:mainfrom
dcrousso:feat-addInitScript-callback
Open

feat(page): support a callback in addInitScript#41861
dcrousso wants to merge 1 commit into
microsoft:mainfrom
dcrousso:feat-addInitScript-callback

Conversation

@dcrousso

Copy link
Copy Markdown
Contributor

add an optional callback to page.addInitScript and browserContext.addInitScript (js only) so the injected script can pass data back to the runner

a function script receives the callback as an argument (after arg, or as its only argument when arg is omitted), and a string or file script can call it as callback

the in-page callback returns a Promise resolving to whatever callback returns, runs on every navigation and in child frames, and for a browserContext covers all current and future pages

the returned Disposable removes the init script, the binding, and the client binding entry

the client composes the init script source and registers callback through the existing evaluate binding machinery

add an optional `callback` to `page.addInitScript` and `browserContext.addInitScript` (js only) so the injected script can pass data back to the runner

a function script receives the callback as an argument (after `arg`, or as its only argument when `arg` is omitted), and a string or file script can call it as `callback`

the in-page callback returns a `Promise` resolving to whatever `callback` returns, runs on every navigation and in child frames, and for a `browserContext` covers all current and future pages

the returned `Disposable` removes the init script, the binding, and the client binding entry

the client composes the init script source and registers `callback` through the existing evaluate binding machinery
@dcrousso
dcrousso requested a review from pavelfeldman July 17, 2026 22:00
@github-actions

Copy link
Copy Markdown
Contributor

Test results for "tests 1"

3 flaky ⚠️ [chromium-library] › library/chromium/chromium.spec.ts:434 › should produce network events, routing, and annotations for Service Worker (advanced) `@frozen-time-library-chromium-linux`
⚠️ [chromium-library] › library/video.spec.ts:356 › screencast › should work for popups `@frozen-time-library-chromium-linux`
⚠️ [chromium-library] › library/chromium/chromium.spec.ts:299 › should report intercepted service worker requests in HAR `@chromium-ubuntu-22.04-node24`

50064 passed, 1186 skipped


Merge workflow run.

@github-actions

Copy link
Copy Markdown
Contributor

Test results for "MCP"

2 failed
❌ [firefox] › mcp/annotate.spec.ts:447 › should switch screencast to -s session on show --annotate @mcp-ubuntu-latest-firefox
❌ [firefox] › mcp/annotate.spec.ts:447 › should switch screencast to -s session on show --annotate @mcp-macos-latest-firefox

7758 passed, 1249 skipped


Merge workflow run.

@github-actions

Copy link
Copy Markdown
Contributor

Hi, I'm the Playwright bot and I took a first look at the failing CI.

🟢 CI is clear — the one failure is a pre-existing Firefox flake

mcp/annotate.spec.ts:447 › should switch screencast to -s session on show --annotate failed on mcp-ubuntu-latest-firefox and mcp-macos-latest-firefox, but it's a known flake unrelated to this PR. Everything in "tests 1" was flaky-passed-on-retry, nothing to triage there.

Details

This PR only touches client-side addInitScript callback plumbing (clientHelper.ts, page.ts, browserContext.ts, types, docs, and its own tests). The MCP annotate/screencast flow doesn't exercise that path, so there's no way the diff reaches this test.

Pre-existing flake / infra

Flaky (no action)

  • tests 1 reported 3 flaky specs (chromium/chromium.spec.ts:434, video.spec.ts:356, chromium/chromium.spec.ts:299) — all passed on retry, and none touch addInitScript.

So: the diff doesn't reach the failing test, and the same test flakes all over CI independently of this PR. I'd call the PR clear on CI. I'm a first pass, though — if you want certainty, a re-run of the MCP Firefox job should come back green.

Triaged by the Playwright bot - agent run


Optional argument to pass to [`param: script`] (only supported when passing a function).

### param: BrowserContext.addInitScript.callback

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be consistent with evaluate where we support function values in arguments?


Optional argument to pass to [`param: script`] (only supported when passing a function).

### param: Page.addInitScript.callback

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants