fix(macos): stop stealing OS focus when opening/switching tabs - #519
Closed
daniel771277 wants to merge 1 commit into
Closed
fix(macos): stop stealing OS focus when opening/switching tabs#519daniel771277 wants to merge 1 commit into
daniel771277 wants to merge 1 commit into
Conversation
✅ Skill review passedReviewed 1 file(s) — no findings. |
On macOS, driving a headful Chrome repeatedly yanks the user's focus (and Space) over to the browser. It fires on tab creation, so it happens on essentially every task. Two CDP calls are responsible: - Target.activateTarget raises the browser window, which activates the app. switch_tab() called it on every switch, and new_tab() calls switch_tab(), so every tab creation stole focus. - Target.createTarget in the foreground (the default) has to open a new window when the browser has zero windows, and macOS raises + activates it. Fix: new_tab() creates targets with background=True, and switch_tab() no longer activates unless the caller asks (activate=True) — only needed when a human has to look at the tab (e.g. login). Screenshots, coordinate clicks and key input all work on a non-selected tab, since they go through the CDP session rather than the OS window. Verified on macOS 15 / Chrome 150 by tracking the frontmost app's pid across each CDP call: createTarget(background=True) and attachToTarget leave it alone, activateTarget was the thief. End-to-end with zero windows open, new_tab -> click_at_xy -> type_text no longer brings Chrome forward, and the click, the typed text and captureScreenshot all work on the non-selected tab. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
daniel771277
force-pushed
the
fix/macos-focus-steal
branch
from
July 14, 2026 13:14
ac9b20f to
e529bc1
Compare
Author
|
Closing this — handling the fix downstream instead. Thanks! |
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.
Problem
On macOS, driving a headful Chrome with browser-harness repeatedly yanks the user's focus (and Space) over to the browser window. It happens on essentially every task, because it fires on tab creation.
Two CDP calls are responsible:
Target.activateTarget— raises the browser window, which on macOS activates the app.switch_tab()called it on every switch, andnew_tab()callsswitch_tab(), so every tab creation stole focus.Target.createTargetin the foreground (the default) — when the browser has zero windows, it has to open a new one, and macOS raises + activates that window.Fix
new_tab()creates targets withbackground=True.switch_tab(target, activate=False)no longer activates unless the caller explicitly asks. Activation is only needed when a human has to look at the tab (e.g. logging in), not for automation.Screenshots, coordinate clicks and key input all work on a non-selected tab — they go through the CDP session, not the OS window — so nothing is lost.
Verification (macOS 15, Chrome 150)
Tracked the frontmost app (
System Events) across each individual CDP call:Target.createTarget(background=True)Target.attachToTargetTarget.activateTargetEnd-to-end in the worst case (browser with zero windows open):
new_tab→wait_for_load→click_at_xy→type_textno longer brings Chrome forward, and the click registered, the typed text landed in the input, andPage.captureScreenshotreturned the correct content of the non-selected tab in 0.2s.🤖 Generated with Claude Code
Summary by cubic
Stop macOS from stealing focus when creating or switching tabs in headful Chrome.
switch_tab()no longer activates the window by default, andnew_tab()creates background targets so Chrome stays in the background.new_tab()and daemonattach_first_page()callTarget.createTargetwithbackground=Trueto avoid activating Chrome when no windows exist.switch_tab(target, activate=False)only callsTarget.activateTargetwhen explicitly requested, keeping automation in the background while clicks, typing, and screenshots still work.Written for commit e529bc1. Summary will update on new commits.