aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2016-03-17 13:44:09 +0000
committerStephen Blott2016-03-17 13:50:51 +0000
commit1199849d929ae20e79d8e07dbf14957d9c1029f8 (patch)
tree460ce10ea7fb0e86b20efe31aef983f5c4df1631
parent2e197df82a13d3cbc3ad305d08650b1a329bdcad (diff)
downloadvimium-1199849d929ae20e79d8e07dbf14957d9c1029f8.tar.bz2
Minor refactoring.
This tidies up some logic that was showing its age.
-rw-r--r--content_scripts/vimium_frontend.coffee46
-rw-r--r--tests/dom_tests/dom_tests.coffee2
2 files changed, 20 insertions, 28 deletions
diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee
index 13d1377d..9a42266a 100644
--- a/content_scripts/vimium_frontend.coffee
+++ b/content_scripts/vimium_frontend.coffee
@@ -150,18 +150,11 @@ initializePreDomReady = ->
Utils.invokeCommandString registryEntry.command, [sourceFrameId, registryEntry] if DomUtils.isTopFrame()
chrome.runtime.onMessage.addListener (request, sender, sendResponse) ->
- # In the options page, we will receive requests from both content and background scripts. ignore those
- # from the former.
- return if sender.tab and not sender.tab.url.startsWith 'chrome-extension://'
- # These requests are intended for the background page, but are delivered to the options page too, where
- # there are no handlers.
- return if request.handler and not request.name
- shouldHandleRequest = isEnabledForUrl
- # We always handle the message if it's one of these listed message types.
- shouldHandleRequest ||= request.name in ["checkEnabledAfterURLChange", "openVomnibar"]
- sendResponse requestHandlers[request.name](request, sender) if shouldHandleRequest
- # Ensure the sendResponse callback is freed.
- false
+ # These requests are intended for the background page, but they're delivered to the options page too.
+ unless request.handler and not request.name
+ if isEnabledForUrl or request.name in ["checkEnabledAfterURLChange", "runInTopFrame"]
+ sendResponse requestHandlers[request.name] request, sender
+ false # Ensure that the sendResponse callback is freed.
# Wrapper to install event listeners. Syntactic sugar.
installListener = (element, event, callback) ->
@@ -176,19 +169,18 @@ installListener = (element, event, callback) ->
# Note: We install the listeners even if Vimium is disabled. See comment in commit
# 6446cf04c7b44c3d419dc450a73b60bcaf5cdf02.
#
-installedListeners = false
-window.installListeners = ->
- unless installedListeners
- initializeModes()
- # Key event handlers fire on window before they do on document. Prefer window for key events so the page
- # can't set handlers to grab the keys before us.
- for type in [ "keydown", "keypress", "keyup", "click", "focus", "blur", "mousedown", "scroll" ]
- do (type) -> installListener window, type, (event) -> handlerStack.bubbleEvent type, event
- installListener document, "DOMActivate", (event) -> handlerStack.bubbleEvent 'DOMActivate', event
- installedListeners = true
- # Other once-only initialisation.
- FindModeHistory.init()
- new GrabBackFocus if isEnabledForUrl
+window.installListeners = installListeners = (isEnabledForUrl) ->
+ # Prevent this initialization from happening more than once.
+ window.installListeners = installListeners = ->
+ # Key event handlers fire on window before they do on document. Prefer window for key events so the page
+ # can't set handlers to grab the keys before us.
+ for type in ["keydown", "keypress", "keyup", "click", "focus", "blur", "mousedown", "scroll"]
+ do (type) -> installListener window, type, (event) -> handlerStack.bubbleEvent type, event
+ installListener document, "DOMActivate", (event) -> handlerStack.bubbleEvent 'DOMActivate', event
+ # Initialize mode state.
+ initializeModes()
+ FindModeHistory.init()
+ new GrabBackFocus if isEnabledForUrl
#
# Whenever we get the focus:
@@ -448,7 +440,8 @@ initializeTopFrame = (request = null) ->
checkIfEnabledForUrl = do ->
Frame.addEventListener "isEnabledForUrl", (response) ->
{isEnabledForUrl, passKeys, frameIsFocused} = response
- installListeners() # But only if they have not been installed already.
+ installListeners isEnabledForUrl
+ normalMode?.setPassKeys passKeys
# Initialize UI components. We only initialize these once we know that Vimium is enabled; see #1838.
if isEnabledForUrl
initializeTopFrame()
@@ -456,7 +449,6 @@ checkIfEnabledForUrl = do ->
else if HUD.isReady()
# Quickly hide any HUD we might already be showing, e.g. if we entered insert mode on page load.
HUD.hide()
- normalMode?.setPassKeys passKeys
# Update the page icon, if necessary.
if windowIsFocused()
chrome.runtime.sendMessage
diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee
index 53e76fc3..b1d1e586 100644
--- a/tests/dom_tests/dom_tests.coffee
+++ b/tests/dom_tests/dom_tests.coffee
@@ -1,6 +1,6 @@
# Install frontend event handlers.
-installListeners()
+installListeners true
HUD.init()
Frame.registerFrameId chromeFrameId: 0