Skip to content

Commit 1539f47

Browse files
authored
Merge pull request philc#3277 from gdh1995/fix-focus-of-hud
fix broken copy, paste and showFindMode since Chrome 74
2 parents 3ed13fa + 0fa114f commit 1539f47

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

content_scripts/hud.coffee

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ HUD =
3333
showFindMode: (@findMode = null) ->
3434
DomUtils.documentComplete =>
3535
@init()
36+
@hudUI.toggleIframeElementClasses "vimiumUIComponentHidden", "vimiumUIComponentVisible"
3637
@hudUI.activate name: "showFindMode"
3738
@tween.fade 1.0, 150
3839

@@ -92,7 +93,9 @@ HUD =
9293
copyToClipboard: (text) ->
9394
DomUtils.documentComplete =>
9495
@init()
95-
@hudUI?.postMessage {name: "copyToClipboard", data: text}
96+
# Chrome 74 only acknowledges text selection when a frame has been visible. See more in #3277 .
97+
@hudUI.toggleIframeElementClasses "vimiumUIComponentHidden", "vimiumUIComponentVisible"
98+
@hudUI.postMessage {name: "copyToClipboard", data: text}
9699

97100
pasteFromClipboard: (@pasteListener) ->
98101
DomUtils.documentComplete =>

content_scripts/ui_component.coffee

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ class UIComponent
88

99
toggleIframeElementClasses: (removeClass, addClass) ->
1010
@iframeElement.classList.remove removeClass
11-
@iframeElement.classList.add addClass
11+
unless @iframeElement.classList.contains addClass
12+
@iframeElement.classList.add addClass
13+
if addClass == "vimiumUIComponentVisible"
14+
# Force to re-compute styles, so Chrome sends a visibility change message to the child frame at once.
15+
# This block is to solve focusing issues since Chrome 74, and detailed explanation and trace is in
16+
# https://github.com/philc/vimium/pull/3277#issuecomment-487363284
17+
getComputedStyle(@iframeElement).display
1218

1319
constructor: (iframeUrl, className, @handleMessage) ->
1420
DomUtils.documentReady =>

pages/hud.coffee

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
findMode = null
22

3+
# Chrome creates a unique port for each MessageChannel,
4+
# so there's a race condition between JavaScript messages of Vimium and browser messages during style recomputation.
5+
# This constant is to ensure all messages have been received and handled
6+
# See more in https://github.com/philc/vimium/pull/3277#discussion_r283080348
7+
TIME_TO_WAIT_FOR_IPC_MESSAGES = 17
8+
39
# Set the input element's text, and move the cursor to the end.
410
setTextInInputElement = (inputElement, text) ->
511
inputElement.textContent = text
@@ -86,7 +92,7 @@ handlers =
8692
countElement.id = "hud-match-count"
8793
countElement.style.float = "right"
8894
hud.appendChild countElement
89-
inputElement.focus()
95+
Utils.setTimeout TIME_TO_WAIT_FOR_IPC_MESSAGES, -> inputElement.focus()
9096

9197
findMode =
9298
historyIndex: -1
@@ -104,14 +110,14 @@ handlers =
104110
" (No matches)"
105111
countElement.textContent = if showMatchText then countText else ""
106112

107-
copyToClipboard: (data) ->
113+
copyToClipboard: (data) -> Utils.setTimeout TIME_TO_WAIT_FOR_IPC_MESSAGES, ->
108114
focusedElement = document.activeElement
109115
Clipboard.copy data
110116
focusedElement?.focus()
111117
window.parent.focus()
112118
UIComponentServer.postMessage {name: "unfocusIfFocused"}
113119

114-
pasteFromClipboard: ->
120+
pasteFromClipboard: -> Utils.setTimeout TIME_TO_WAIT_FOR_IPC_MESSAGES, ->
115121
focusedElement = document.activeElement
116122
data = Clipboard.paste()
117123
focusedElement?.focus()

0 commit comments

Comments
 (0)