aboutsummaryrefslogtreecommitdiffstats
path: root/content_scripts
diff options
context:
space:
mode:
authorStephen Blott2016-03-15 15:04:10 +0000
committerStephen Blott2016-03-17 11:17:03 +0000
commitf504fd305e5b2c16b2053a76090ea2618ab42332 (patch)
tree67f91c28adaa672e61e904f795802719a32466aa /content_scripts
parent8912215c01504b459ff1c6aa17309a86037f6af8 (diff)
downloadvimium-f504fd305e5b2c16b2053a76090ea2618ab42332.tar.bz2
Move isEnabledForUrl to Frame.port.
Diffstat (limited to 'content_scripts')
-rw-r--r--content_scripts/vimium_frontend.coffee25
1 files changed, 16 insertions, 9 deletions
diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee
index 4b461847..49187e4a 100644
--- a/content_scripts/vimium_frontend.coffee
+++ b/content_scripts/vimium_frontend.coffee
@@ -218,12 +218,18 @@ DomUtils.documentReady ->
Frame =
port: null
+ listeners: {}
- init: ->
+ addEventListener: (handler, callback) -> (@listeners[handler] ?= []).push callback
+ postMessage: (handler, request = {}) -> @port.postMessage extend request, {handler}
+ registerFrameId: ({chromeFrameId}) -> frameId = window.frameId = chromeFrameId
+
+ init: (callback) ->
@port = chrome.runtime.connect name: "frames"
- @port.onMessage.addListener (request) => this[request.name] request
+ @port.onMessage.addListener (request) =>
+ handler request for handler in @listeners[request.handler]
- registerFrameId: ({chromeFrameId}) -> frameId = window.frameId = chromeFrameId
+ @addEventListener "registerFrameId", Frame.registerFrameId
handleShowHUDforDuration = ({ text, duration }) ->
if DomUtils.isTopFrame()
@@ -442,10 +448,9 @@ initializeTopFrame = (request = null) ->
# Checks if Vimium should be enabled or not in this frame. As a side effect, it also informs the background
# page whether this frame has the focus, allowing the background page to track the active frame's URL.
-checkIfEnabledForUrl = (frameIsFocused = windowIsFocused()) ->
- url = window.location.toString()
- chrome.runtime.sendMessage { handler: "isEnabledForUrl", url: url, frameIsFocused: frameIsFocused }, (response) ->
- { isEnabledForUrl, passKeys } = response
+checkIfEnabledForUrl = do ->
+ Frame.addEventListener "isEnabledForUrl", (response) ->
+ {isEnabledForUrl, passKeys, frameIsFocused} = response
installListeners() # But only if they have not been installed already.
# Initialize UI components. We only initialize these once we know that Vimium is enabled; see #1838.
if isEnabledForUrl
@@ -456,14 +461,16 @@ checkIfEnabledForUrl = (frameIsFocused = windowIsFocused()) ->
HUD.hide()
normalMode?.setPassKeys passKeys
# Update the page icon, if necessary.
- if windowIsFocused()
+ if frameIsFocused
chrome.runtime.sendMessage
handler: "setIcon"
icon:
if isEnabledForUrl and not passKeys then "enabled"
else if isEnabledForUrl then "partial"
else "disabled"
- null
+
+ (frameIsFocused = windowIsFocused()) ->
+ Frame.postMessage "isEnabledForUrl", {frameIsFocused, url: window.location.toString()}
# When we're informed by the background page that a URL in this tab has changed, we check if we have the
# correct enabled state (but only if this frame has the focus).