Skip to content

Commit dc2bff8

Browse files
committed
delay window.focus() when needed
On Chrome, if an extension's content script call window.focus() during Port.onMessage handlers, then Chrome will activate the tab. Therefore there's a race condition causing philc#3242. In my tests, a `setTimeout` is just enough to avoid this activating.
1 parent e281bc9 commit dc2bff8

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

content_scripts/ui_component.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class UIComponent
9393
handler: "sendMessageToFrames",
9494
message: name: "focusFrame", frameId: @options.sourceFrameId, forceFocusThisFrame: true
9595
else
96-
window.focus()
96+
Utils.setTimeout 0, -> window.focus()
9797
@options = null
9898
@postMessage "hidden" # Inform the UI component that it is hidden.
9999

content_scripts/vimium_frontend.coffee

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,12 @@ Frame =
232232
setScrollPosition = ({ scrollX, scrollY }) ->
233233
DomUtils.documentReady ->
234234
if DomUtils.isTopFrame()
235-
window.focus()
236-
document.body.focus()
237-
if 0 < scrollX or 0 < scrollY
238-
Marks.setPreviousPosition()
239-
window.scrollTo scrollX, scrollY
235+
Utils.setTimeout 0, ->
236+
window.focus()
237+
document.body.focus()
238+
if 0 < scrollX or 0 < scrollY
239+
Marks.setPreviousPosition()
240+
window.scrollTo scrollX, scrollY
240241

241242
flashFrame = do ->
242243
highlightedFrameElement = null
@@ -275,11 +276,12 @@ focusThisFrame = (request) ->
275276
# next frame instead. This affects sites like Google Inbox, which have many tiny iframes. See #1317.
276277
chrome.runtime.sendMessage handler: "nextFrame"
277278
return
278-
window.focus()
279-
# On Firefox, window.focus doesn't always draw focus back from a child frame (bug 554039).
280-
# We blur the active element if it is an iframe, which gives the window back focus as intended.
281-
document.activeElement.blur() if document.activeElement.tagName.toLowerCase() == "iframe"
282-
flashFrame() if request.highlight
279+
Utils.setTimeout 0, ->
280+
window.focus()
281+
# On Firefox, window.focus doesn't always draw focus back from a child frame (bug 554039).
282+
# We blur the active element if it is an iframe, which gives the window back focus as intended.
283+
document.activeElement.blur() if document.activeElement.tagName.toLowerCase() == "iframe"
284+
flashFrame() if request.highlight
283285

284286
# Used by focusInput command.
285287
root.lastFocusedInput = do ->

0 commit comments

Comments
 (0)