aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--background_scripts/main.coffee32
-rw-r--r--content_scripts/vimium_frontend.coffee25
-rw-r--r--tests/unit_tests/exclusion_test.coffee2
3 files changed, 33 insertions, 26 deletions
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee
index f6e70d3a..5135a158 100644
--- a/background_scripts/main.coffee
+++ b/background_scripts/main.coffee
@@ -102,20 +102,6 @@ logMessage = do ->
#
getCurrentTabUrl = (request, sender) -> sender.tab.url
-#
-# Checks the user's preferences in local storage to determine if Vimium is enabled for the given URL, and
-# whether any keys should be passed through to the underlying page.
-# The source frame also informs us whether or not it has the focus, which allows us to track the URL of the
-# active frame.
-#
-root.isEnabledForUrl = isEnabledForUrl = (request, sender) ->
- urlForTab[sender.tab.id] = request.url if request.frameIsFocused
- rule = Exclusions.getRule(request.url)
- {
- isEnabledForUrl: not rule or rule.passKeys
- passKeys: rule?.passKeys or ""
- }
-
onURLChange = (details) ->
chrome.tabs.sendMessage details.tabId, name: "checkEnabledAfterURLChange"
@@ -384,7 +370,7 @@ Frames =
frameIdsForTab[tabId]?
frameIdsForTab[tabId] ?= [0]
frameIdsForTab[tabId].push frameId unless frameId == 0
- port.postMessage name: "registerFrameId", chromeFrameId: frameId
+ port.postMessage handler: "registerFrameId", chromeFrameId: frameId
port.onDisconnect.addListener listener = ->
# Unregister the frame. However, we never unregister the main/top frame. If the tab is navigating to
@@ -394,7 +380,19 @@ Frames =
# registering before the old one is deregistered).
if tabId of frameIdsForTab and frameId != 0
frameIdsForTab[tabId] = frameIdsForTab[tabId].filter (fId) -> fId != frameId
- port.onDisconnect.removeListener listener
+
+ # Return our onMessage handler for this port.
+ (request, port) =>
+ response = this[request.handler] {request, tabId, frameId, port}
+ port.postMessage response if response != false
+
+ isEnabledForUrl: ({request, tabId}) ->
+ urlForTab[tabId] = request.url if request.frameIsFocused
+ rule = Exclusions.getRule request.url
+ # Send a response...
+ extend request,
+ isEnabledForUrl: not rule or 0 < rule.passKeys.length
+ passKeys: rule?.passKeys ? ""
handleFrameFocused = (request, sender) ->
[tabId, frameId] = [sender.tab.id, sender.frameId]
@@ -437,7 +435,6 @@ sendRequestHandlers =
nextFrame: (request) -> BackgroundCommands.nextFrame 1, request.frameId
copyToClipboard: copyToClipboard
pasteFromClipboard: pasteFromClipboard
- isEnabledForUrl: isEnabledForUrl
selectSpecificTab: selectSpecificTab
createMark: Marks.create.bind(Marks)
gotoMark: Marks.goto.bind(Marks)
@@ -502,3 +499,4 @@ chrome.runtime.onInstalled.addListener ({reason}) ->
root.TabOperations = TabOperations
root.logMessage = logMessage
+root.Frames = Frames
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).
diff --git a/tests/unit_tests/exclusion_test.coffee b/tests/unit_tests/exclusion_test.coffee
index 0e4b87bc..e6a47779 100644
--- a/tests/unit_tests/exclusion_test.coffee
+++ b/tests/unit_tests/exclusion_test.coffee
@@ -19,6 +19,8 @@ extend(global, require "../../background_scripts/exclusions.js")
extend(global, require "../../background_scripts/commands.js")
extend(global, require "../../background_scripts/main.js")
+isEnabledForUrl = (request) -> Frames.isEnabledForUrl {request, tabId: 0}
+
# These tests cover only the most basic aspects of excluded URLs and passKeys.
#
context "Excluded URLs and pass keys",