Skip to content

feat(chromium): add createPagesInBackground launch option to avoid stealing focus#41282

Open
Andrew-Chen-Wang wants to merge 2 commits into
microsoft:mainfrom
Andrew-Chen-Wang:fix-4822
Open

feat(chromium): add createPagesInBackground launch option to avoid stealing focus#41282
Andrew-Chen-Wang wants to merge 2 commits into
microsoft:mainfrom
Andrew-Chen-Wang:fix-4822

Conversation

@Andrew-Chen-Wang

@Andrew-Chen-Wang Andrew-Chen-Wang commented Jun 12, 2026

Copy link
Copy Markdown

Summary

  • New browserType.launch({ createPagesInBackground: true }) option (default false, no behavior change otherwise).
  • When enabled, headed Chromium passes background: true to Target.createTarget, so newPage() no longer activates the browser app and steals OS focus from the user (verified on macOS).
  • Pages remain normal visible windows: document.hasFocus() / visibilityState are unaffected thanks to focus emulation, and page.bringToFront() still raises the window. Skipped for headless (headless shell does not support the parameter) and Android.

Why Chromium-only

  • Firefox and WebKit do not reproduce the problem on current builds: headed Firefox never stole focus in testing even though -foreground is passed (older builds did), and WebKit never steals.
  • Firefox's (historical) steal was a one-time app activation at launch caused by the -foreground CLI arg — a different mechanism than Chromium's per-newPage() foreground target creation, and there is no juggler equivalent of background: true. Affected Firefox users can already opt out via ignoreDefaultArgs: ['-foreground'].
  • Extending the option to Firefox would mean skipping -foreground in defaultArgs() 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

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
@Andrew-Chen-Wang

Copy link
Copy Markdown
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.
@Andrew-Chen-Wang Andrew-Chen-Wang changed the title fix(chromium): create headed pages in background to avoid stealing focus feat(chromium): add createPagesInBackground launch option to avoid stealing focus Jun 12, 2026
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.

[Question] How to disable bringing page to front during launching.

1 participant