Skip to content

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

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

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

@dcrousso
dcrousso requested a review from pavelfeldman July 17, 2026 22:00
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Comment thread docs/src/api/class-browsercontext.md Outdated

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?

Comment thread docs/src/api/class-page.md Outdated

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

any function in the `arg` for `page.addInitScript`/`browserContext.addInitScript` is now exposed as a callback the page can call, just like `evaluate(fn, arg, { exposeFunctions })` already does

the `arg` is serialized with the evaluate call argument serializer and rebuilt in the page on every navigation through a new `parseArgument` on the bindings controller

as a result, any callbacks survive navigations and reach all current/future pages

disposing the returned `Disposable` removes every callback binding
@dcrousso
dcrousso force-pushed the feat-addInitScript-callback branch from 2646731 to cb06032 Compare July 20, 2026 19:31
@dcrousso
dcrousso requested a review from pavelfeldman July 20, 2026 19:32
@github-actions

Copy link
Copy Markdown
Contributor

Test results for "MCP"

3 failed
❌ [chromium] › mcp/cli-killall.spec.ts:42 › kill-all kills filtered dashboard pid @mcp-macos-latest-chromium
❌ [firefox] › mcp/annotate.spec.ts:57 › should capture multiple screenshots in one annotation @mcp-ubuntu-latest-firefox
❌ [msedge] › mcp/devtools.spec.ts:21 › browser_highlight @mcp-windows-latest-msedge

7757 passed, 1249 skipped


Merge workflow run.

@github-actions

Copy link
Copy Markdown
Contributor

Test results for "tests 1"

7 flaky ⚠️ [chromium-library] › library/chromium/oopif.spec.ts:282 › should click `@chromium-ubuntu-22.04-arm-node20`
⚠️ [chromium-library] › library/video.spec.ts:736 › screencast › should work with video+trace `@chromium-ubuntu-22.04-arm-node20`
⚠️ [chromium-library] › library/video.spec.ts:699 › screencast › should capture full viewport on hidpi `@realtime-time-library-chromium-linux`
⚠️ [chromium-library] › library/video.spec.ts:356 › screencast › should work for popups `@chromium-ubuntu-22.04-node24`
⚠️ [chromium-library] › library/video.spec.ts:495 › screencast › should capture static page in persistent context @smoke `@chromium-ubuntu-22.04-node24`
⚠️ [chromium-library] › library/video.spec.ts:664 › screencast › should capture full viewport `@chromium-ubuntu-22.04-node24`
⚠️ [firefox-page] › page/page-goto.spec.ts:81 › should work with Cross-Origin-Opener-Policy `@firefox-ubuntu-22.04-node20`

50050 passed, 1188 skipped


Merge workflow run.

@github-actions

Copy link
Copy Markdown
Contributor

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

🟡 PR looks clear — two confirmed flakes, one msedge failure I can't fully prove

The two MCP failures I can trace history for are established flakes on unrelated PRs and main. The third (browser_highlight on Windows msedge) has no prior history, so I can't call it a proven flake — but the diff can't plausibly reach it and every other engine passed, so it leans flake/infra.

Details

This PR only adds an addInitScript callback path. The shared injected changes it makes — bindingsController.ts (a new, unused parseArgument method) and utilityScriptSerializers.ts (a type-only union addition) — are additive and don't touch the normal binding/evaluate or highlight rendering paths. Nothing here reaches the MCP annotate/killall/devtools flows.

Pre-existing flake / infra

  • [chromium] › mcp/cli-killall.spec.ts:42 › kill-all kills filtered dashboard pid (mcp-macos-latest-chromium) — known flake. In the aggregated CI DB it fails across every engine (chromium 16/667, firefox 13/656, webkit 10/667, chrome 12/667). On this exact macOS-chromium bot it failed 11 times on SHAs this PR can't own — PR41851, PR41842, PR41821, PR41728, PR41680, PR41673, and plain main pushes (also 5 more on mcp-ubuntu-latest-chromium: PR41780, PR41701, main).

  • [firefox] › mcp/annotate.spec.ts:57 › should capture multiple screenshots in one annotation (mcp-ubuntu-latest-firefox) — pre-existing flake. On this exact bot it failed on PR41704 and a main push, unrelated to this PR (firefox 3/656 overall).

Uncertain

  • [msedge] › mcp/devtools.spec.ts:21 › browser_highlight (mcp-windows-latest-msedge) — expect(locator('x-pw-highlight')).toBeVisible() timed out at 5s, element(s) not found — the highlight overlay didn't paint in time. This test has never failed in the DB on any engine, so I can't cite a prior flake. But it leans flake/infra: it failed only on Windows msedge and passed on chromium/firefox/webkit/chrome in the same run, and the PR's injected-bundle changes are additive and would break the overlay on every engine if they were the cause, not msedge alone. To be certain I'd want a re-run of the msedge job — a green rerun would settle it.

Triaged by the Playwright bot

Triaged by the Playwright bot - agent run

@dcrousso

Copy link
Copy Markdown
Contributor Author

this is handled by #41921

@dcrousso dcrousso closed this Jul 21, 2026
@dcrousso
dcrousso deleted the feat-addInitScript-callback branch July 21, 2026 22:11
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