Skip to content

fix: run extensions before page scripts in popups and out-of-process iframes - #663

Merged
route merged 4 commits into
mainfrom
fix/extensions-in-popups
Oct 20, 2026
Merged

route merged 4 commits into
mainfrom
fix/extensions-in-popups

Conversation

@route

@route route commented Oct 20, 2026

Copy link
Copy Markdown
Member

Extensions didn't run before a page's own scripts everywhere:

  • Popups: a new page was resumed on attach before Page.addScriptToEvaluateOnNewDocument was sent. Its first document loaded without extensions, and the Runtime.evaluate fallback applied them only after load.
  • Out-of-process iframes: never got them.
  • Every page: ran them once more on about:blank, through the same Runtime.evaluate fallback.

Changes

  • Registration on attach: extensions are registered in the event thread when a page or iframe attaches, while Chrome still has it paused, and only then is it resumed. An out-of-process iframe ignores the scripts until Page is enabled on its session, so Page.enable is sent first when there are extensions. The Runtime.evaluate fallback is gone.
  • Page#evaluate_on_new_document(source, world_name:, run_immediately:) is now a plain Page.addScriptToEvaluateOnNewDocument call on that page. It returns the script id, and Page#remove_script_on_new_document removes it. browser.evaluate_on_new_document goes to the current page like other page methods.
  • browser.add_extension adds an extension for pages and iframes attached afterwards. That's what browser.evaluate_on_new_document used to do.
  • Extension files are read in Browser.new and add_extension, so a missing file raises Errno::ENOENT there instead of on first navigation.

Breaking

Code that relied on browser.evaluate_on_new_document reaching pages opened later should use browser.add_extension.

route added 4 commits October 20, 2026 10:58
…wser.add_extension

Browser#evaluate_on_new_document only appended to the extensions list, so it reached pages built afterwards and not the
page you called it on. It's now a Page method sending Page.addScriptToEvaluateOnNewDocument, with world_name: and
run_immediately:, returning the id for remove_script_on_new_document. browser.add_extension takes over adding to the
list. Extension files are read in Browser.new and add_extension, so a missing file raises right there.
Pages were resumed on attach before Page.addScriptToEvaluateOnNewDocument was sent, so a popup's first document loaded
without extensions and Runtime.evaluate applied them only after load, out-of-process iframes never got them. Register
them on the attach session before resuming and drop the Runtime.evaluate fallback. An out-of-process iframe ignores the
scripts until Page is enabled on its session, so it's enabled first when there are extensions.
When Target.targetCreated for an iframe arrived before Target.attachedToTarget, the target took its frame owner's
session and the attached one was ignored, so commands sent through the iframe target reached the parent page. The
session from Target.attachedToTarget now always wins.
@route
route merged commit 082de04 into main Oct 20, 2026
7 checks passed
@route
route deleted the fix/extensions-in-popups branch October 20, 2026 19:43
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.

1 participant