feat(chromium): add createPagesInBackground launch option to avoid stealing focus#41282
Open
Andrew-Chen-Wang wants to merge 2 commits into
Open
feat(chromium): add createPagesInBackground launch option to avoid stealing focus#41282Andrew-Chen-Wang wants to merge 2 commits into
Andrew-Chen-Wang wants to merge 2 commits into
Conversation
Pass `background: true` to Target.createTarget for headed Chromium so that opening a new page does not activate the browser app and steal OS-level focus from the user. Playwright already emulates focus, so pages behave as foreground. The parameter is not supported by the headless shell or Android, where it is omitted. References: microsoft#4822
Author
|
@microsoft-github-policy-service agree |
…Background Replace the unconditional behavior change with a new browserType.launch() option. When enabled, headed Chromium creates pages with Target.createTarget background:true so the browser window does not activate and steal OS focus. Defaults to false.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
browserType.launch({ createPagesInBackground: true })option (defaultfalse, no behavior change otherwise).background: truetoTarget.createTarget, sonewPage()no longer activates the browser app and steals OS focus from the user (verified on macOS).document.hasFocus()/visibilityStateare unaffected thanks to focus emulation, andpage.bringToFront()still raises the window. Skipped for headless (headless shell does not support the parameter) and Android.Why Chromium-only
-foregroundis passed (older builds did), and WebKit never steals.-foregroundCLI arg — a different mechanism than Chromium's per-newPage()foreground target creation, and there is no juggler equivalent ofbackground: true. Affected Firefox users can already opt out viaignoreDefaultArgs: ['-foreground'].-foregroundindefaultArgs()when the option is set; left out for now since it is untestable against current builds and changes launch activation rather than page creation. Happy to generalize if preferred.fixes #4822