]> git.gir.st - VimFx.git/blob - extension/lib/events-frame.coffee
Recognozie more ARIA roles as clickable
[VimFx.git] / extension / lib / events-frame.coffee
1 ###
2 # Copyright Simon Lydell 2015.
3 #
4 # This file is part of VimFx.
5 #
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
18 ###
19
20 # This file is the equivalent to events.coffee, but for frame scripts.
21
22 notation = require('vim-like-key-notation')
23 commands = require('./commands-frame')
24 messageManager = require('./message-manager')
25 utils = require('./utils')
26
27 class FrameEventManager
28 constructor: (@vim) ->
29 @numFocusToSuppress = 0
30 @keepInputs = false
31 @currentUrl = false
32
33 listen: utils.listen.bind(null, FRAME_SCRIPT_ENVIRONMENT)
34 listenOnce: utils.listenOnce.bind(null, FRAME_SCRIPT_ENVIRONMENT)
35
36 addListeners: ->
37 # If the page already was loaded when VimFx was initialized, send the
38 # 'frameCanReceiveEvents' message straight away.
39 if @vim.content.document.readyState == 'complete'
40 messageManager.send('frameCanReceiveEvents', true)
41
42 @listen('readystatechange', (event) =>
43 target = event.originalTarget
44 @currentUrl = @vim.content.location.href
45
46 if target == @vim.content.document
47 switch target.readyState
48 when 'interactive'
49 messageManager.send('locationChange', @currentUrl)
50 when 'complete'
51 messageManager.send('frameCanReceiveEvents', true)
52 )
53
54 @listen('pageshow', (event) =>
55 [oldUrl, @currentUrl] = [@currentUrl, @vim.content.location.href]
56
57 # When navigating the history, `event.persisted` is `true` (meaning that
58 # the page loaded from cache) and 'readystatechange' won’t be fired, so
59 # send a 'locationChange' message to make sure that the blacklist is
60 # applied etc. The reason we don’t simply _always_ do this on the
61 # 'pageshow' event, is because it usually fires too late. However, it also
62 # fires after having moved a tab to another window. In that case it is
63 # _not_ a location change; the blacklist should not be applied.
64 if event.persisted
65 url = @vim.content.location.href
66 messageManager.send('cachedPageshow', null, (movedToNewTab) =>
67 if not movedToNewTab and oldUrl != @currentUrl
68 messageManager.send('locationChange', @currentUrl)
69 )
70 )
71
72 @listen('pagehide', (event) =>
73 target = event.originalTarget
74 @currentUrl = null
75
76 if target == @vim.content.document
77 messageManager.send('frameCanReceiveEvents', false)
78
79 # If the target isn’t the topmost document, it means that a frame has
80 # changed: It could have been removed or its `src` attribute could have
81 # been changed. If the frame contains other frames, 'pagehide' events have
82 # already been fired for them.
83 @vim.resetState(target)
84 )
85
86 @listen('click', (event) =>
87 if @vim.mode == 'hints' and event.isTrusted
88 messageManager.send('enterMode', {mode: 'normal'})
89 )
90
91 @listen('overflow', (event) =>
92 target = event.originalTarget
93 @vim.state.scrollableElements.addChecked(target)
94 )
95
96 @listen('underflow', (event) =>
97 target = event.originalTarget
98 @vim.state.scrollableElements.deleteChecked(target)
99 )
100
101 @listen('keydown', (event) =>
102 @keepInputs = false
103
104 suppress = @vim.onInput(event)
105
106 # This also suppresses the 'keypress' and 'keyup' events. (Yes, in frame
107 # scripts, suppressing the 'keydown' events does seem to even suppress
108 # the 'keyup' event!)
109 utils.suppressEvent(event) if suppress
110
111 # From this line on, the rest of the code in `addListeners` is more or
112 # less devoted to autofocus prevention. When enabled, focus events that
113 # occur before the user has interacted with page are prevented.
114 #
115 # If this keydown event wasn’t suppressed (`not suppress`), it’s an
116 # obvious interaction with the page. If it _was_ suppressed, though, it’s
117 # an interaction depending on the command triggered; if it calls
118 # `vim.markPageInteraction()` or not.
119 @vim.markPageInteraction() unless suppress
120 )
121
122 @listen('keydown', ((event) =>
123 suppress = messageManager.get('lateKeydown', {
124 defaultPrevented: event.defaultPrevented
125 })
126
127 if @vim.state.inputs and @vim.mode == 'normal' and not suppress and
128 not event.defaultPrevented
129 # There is no need to take `ignore_keyboard_layout` and `translations`
130 # into account here, since we want to override the _native_ `<tab>`
131 # behavior. Then, `event.key` is the way to go. (Unless the prefs are
132 # customized. YAGNI until requested.)
133 keyStr = notation.stringify(event)
134 options = @vim.options(['focus_previous_key', 'focus_next_key'])
135 direction = switch keyStr
136 when '' then null
137 when options.focus_previous_key then -1
138 when options.focus_next_key then +1
139 else null
140 if direction?
141 suppress = commands.move_focus({@vim, direction})
142 @keepInputs = true
143
144 utils.suppressEvent(event) if suppress
145 ), false)
146
147 @listen('mousedown', (event) =>
148 # Allow clicking on another text input without exiting “gi mode”. Listen
149 # for 'mousedown' instead of 'click', because only the former runs before
150 # the 'blur' event. Also, `event.originalTarget` does _not_ work here.
151 @keepInputs = (@vim.state.inputs and event.target in @vim.state.inputs)
152
153 # Clicks are always counted as page interaction. Listen for 'mousedown'
154 # instead of 'click' to mark the interaction as soon as possible.
155 @vim.markPageInteraction()
156 )
157
158 messageManager.listen('browserRefocus', =>
159 # Suppress the next two focus events (for `document` and `window`; see
160 # `blurActiveBrowserElement`).
161 @numFocusToSuppress = 2
162 )
163
164 sendFocusType = =>
165 focusType = utils.getFocusType(utils.getActiveElement(@vim.content))
166 messageManager.send('focusType', focusType)
167
168 @listen('focus', (event) =>
169 target = event.originalTarget
170
171 if @numFocusToSuppress > 0
172 utils.suppressEvent(event)
173 @numFocusToSuppress--
174 return
175
176 @vim.state.explicitBodyFocus = (target == @vim.content.document.body)
177
178 sendFocusType()
179
180 # Reset `hasInteraction` when (re-)selecting a tab, or coming back from
181 # another window, in order to prevent the common “automatically re-focus
182 # when switching back to the tab” behaviour many sites have, unless a text
183 # input _should_ be re-focused when coming back to the tab (see the 'blur'
184 # event below).
185 if target == @vim.content.document
186 if @vim.state.shouldRefocus
187 @vim.state.hasInteraction = true
188 @vim.state.shouldRefocus = false
189 else
190 @vim.state.hasInteraction = false
191 return
192
193 # Save the last focused text input regardless of whether that input might
194 # be blurred because of autofocus prevention.
195 if utils.isTextInputElement(target)
196 @vim.state.lastFocusedTextInput = target
197 @vim.state.hasFocusedTextInput = true
198
199 focusManager = Cc['@mozilla.org/focus-manager;1']
200 .getService(Ci.nsIFocusManager)
201
202 # When moving a tab to another window, there is a short period of time
203 # when there’s no listener for this call.
204 return unless options = @vim.options(
205 ['prevent_autofocus', 'prevent_autofocus_modes']
206 )
207
208 # Blur the focus target, if autofocus prevention is enabled…
209 if options.prevent_autofocus and
210 @vim.mode in options.prevent_autofocus_modes and
211 # …and the user has interacted with the page…
212 not @vim.state.hasInteraction and
213 # …and the event is programmatic (not caused by clicks or keypresses)…
214 focusManager.getLastFocusMethod(null) == 0 and
215 # …and the target may steal most keystrokes.
216 utils.isTypingElement(target)
217 # Some sites (such as icloud.com) re-focuses inputs if they are blurred,
218 # causing an infinite loop of autofocus prevention and re-focusing.
219 # Therefore, blur events that happen just after an autofocus prevention
220 # are suppressed.
221 @listenOnce('blur', utils.suppressEvent)
222 target.blur()
223 @vim.state.hasFocusedTextInput = false
224 )
225
226 @listen('blur', (event) =>
227 target = event.originalTarget
228
229 sendFocusType()
230
231 # If a text input is blurred immediately before the document loses focus,
232 # it most likely means that the user switched tab, for example by pressing
233 # `<c-tab>`, or switched to another window, while the text input was
234 # focused. In this case, when switching back to that tab, the text input
235 # will, and should, be re-focused (because it was focused when you left
236 # the tab). This case is kept track of so that the autofocus prevention
237 # does not catch it.
238 if utils.isTypingElement(target)
239 utils.nextTick(@vim.content, =>
240 @vim.state.shouldRefocus = not @vim.content.document.hasFocus()
241
242 # “gi mode” ends when blurring a text input, unless `<tab>` was just
243 # pressed.
244 unless @vim.state.shouldRefocus or @keepInputs
245 commands.clear_inputs({@vim})
246 )
247 )
248
249 module.exports = FrameEventManager
Imprint / Impressum