feat(page): support a callback in addInitScript#41861
Conversation
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
Test results for "tests 1"3 flaky50064 passed, 1186 skipped Merge workflow run. |
Test results for "MCP"2 failed 7758 passed, 1249 skipped Merge workflow run. |
|
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
DetailsThis PR only touches client-side Pre-existing flake / infra
Flaky (no action)
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 |
There was a problem hiding this comment.
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 |
add an optional
callbacktopage.addInitScriptandbrowserContext.addInitScript(js only) so the injected script can pass data back to the runnera function script receives the callback as an argument (after
arg, or as its only argument whenargis omitted), and a string or file script can call it ascallbackthe in-page callback returns a
Promiseresolving to whatevercallbackreturns, runs on every navigation and in child frames, and for abrowserContextcovers all current and future pagesthe returned
Disposableremoves the init script, the binding, and the client binding entrythe client composes the init script source and registers
callbackthrough the existing evaluate binding machinery