From 8a659af44a8205f39e4c0e04146978447ca3f38e Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Fri, 24 Apr 2015 12:02:09 +0100 Subject: Get incognto state directly from chrome.extensions.inIncognitoContext --- content_scripts/vimium_frontend.coffee | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 6c09ab72..7b99287e 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -12,7 +12,7 @@ findModeInitialRange = null isShowingHelpDialog = false keyPort = null isEnabledForUrl = true -isIncognitoMode = false +isIncognitoMode = chrome.extension.inIncognitoContext passKeys = null keyQueue = null # The user's operating system. @@ -551,9 +551,7 @@ checkIfEnabledForUrl = -> url = window.location.toString() chrome.runtime.sendMessage { handler: "isEnabledForUrl", url: url }, (response) -> - isEnabledForUrl = response.isEnabledForUrl - passKeys = response.passKeys - isIncognitoMode = response.incognito + {isEnabledForUrl, passKeys} = response if isEnabledForUrl initializeWhenEnabled() else if HUD.isReady() -- cgit v1.2.3 From b3986dcd68a9383b3552ffb99c13c19e94bd08e4 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Fri, 24 Apr 2015 12:16:36 +0100 Subject: Re-check enabled state after history.pushState/location.hash is changed This uses the chrome.webRequest API to detect changes to page URL which *do not* cause the content script to refresh. --- content_scripts/vimium_frontend.coffee | 42 +++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 7b99287e..862118bc 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -175,12 +175,13 @@ initializePreDomReady = -> currentKeyQueue: (request) -> keyQueue = request.keyQueue handlerStack.bubbleEvent "registerKeyQueue", { keyQueue: keyQueue } + updateEnabledForUrlState: updateEnabledForUrlState 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://' - return unless isEnabledForUrl + return unless isEnabledForUrl or request.name in ["updateEnabledForUrlState"] # These requests are delivered to the options page, but there are no handlers there. return if request.handler in [ "registerFrame", "frameFocused", "unregisterFrame" ] sendResponse requestHandlers[request.name](request, sender) @@ -550,24 +551,27 @@ onKeyup = (event) -> checkIfEnabledForUrl = -> url = window.location.toString() - chrome.runtime.sendMessage { handler: "isEnabledForUrl", url: url }, (response) -> - {isEnabledForUrl, passKeys} = response - if isEnabledForUrl - initializeWhenEnabled() - 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() - handlerStack.bubbleEvent "registerStateChange", - enabled: isEnabledForUrl - passKeys: passKeys - # Update the page icon, if necessary. - if document.hasFocus() - chrome.runtime.sendMessage - handler: "setIcon" - icon: - if isEnabledForUrl and not passKeys then "enabled" - else if isEnabledForUrl then "partial" - else "disabled" + chrome.runtime.sendMessage { handler: "isEnabledForUrl", url: url }, updateEnabledForUrlState + +updateEnabledForUrlState = (response) -> + {isEnabledForUrl, passKeys} = response + if isEnabledForUrl + initializeWhenEnabled() + 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() + handlerStack.bubbleEvent "registerStateChange", + enabled: isEnabledForUrl + passKeys: passKeys + # Update the page icon, if necessary. + if document.hasFocus() + chrome.runtime.sendMessage + handler: "setIcon" + icon: + if isEnabledForUrl and not passKeys then "enabled" + else if isEnabledForUrl then "partial" + else "disabled" + null # Exported to window, but only for DOM tests. -- cgit v1.2.3 From 6446cf04c7b44c3d419dc450a73b60bcaf5cdf02 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Fri, 24 Apr 2015 13:53:54 +0100 Subject: Always initialise event listeners early The event listeners were registered late, potentially allowing the page to get priority over us for key events, etc., when: * the original URL was disabled by an exclusion rule * the URL was changed - without a page load (by history.pushState or modifying location.hash), and - the new URL isn't (completely) disabled by any exclusion rules. This forces the event listeners to be registered even when the current URL is disabled, to avoid this problem. --- content_scripts/vimium_frontend.coffee | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 862118bc..cc97b515 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -200,7 +200,7 @@ installListener = (element, event, callback) -> # Run this as early as possible, so the page can't register any event handlers before us. # installedListeners = false -window.initializeWhenEnabled = -> +window.initializeWithState = -> unless installedListeners # 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. @@ -555,9 +555,8 @@ checkIfEnabledForUrl = -> updateEnabledForUrlState = (response) -> {isEnabledForUrl, passKeys} = response - if isEnabledForUrl - initializeWhenEnabled() - else if HUD.isReady() + initializeWithState() + if HUD.isReady() and not isEnabledForUrl # Quickly hide any HUD we might already be showing, e.g. if we entered insert mode on page load. HUD.hide() handlerStack.bubbleEvent "registerStateChange", -- cgit v1.2.3 From 7b3b3b4b7e9b9b39cf583e857c4f384a4fff7fb1 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 25 Apr 2015 09:47:49 +0100 Subject: Remove requirement for Chrome 41. --- content_scripts/vimium_frontend.coffee | 48 ++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 23 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index c41ca62f..2b90fe95 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -189,7 +189,7 @@ initializePreDomReady = -> handlerStack.bubbleEvent "registerKeyQueue", { keyQueue: keyQueue } # A frame has received the focus. We don't care here (the Vomnibar/UI-component handles this). frameFocused: -> - updateEnabledForUrlState: updateEnabledForUrlState + checkEnabledAfterURLChange: checkEnabledAfterURLChange chrome.runtime.onMessage.addListener (request, sender, sendResponse) -> # In the options page, we will receive requests from both content and background scripts. ignore those @@ -199,7 +199,7 @@ initializePreDomReady = -> return if request.handler in [ "registerFrame", "frameFocused", "unregisterFrame" ] shouldHandleRequest = isEnabledForUrl # We always handle the message if it's one of these listed message types. - shouldHandleRequest ||= request.name in [ "executePageCommand", "updateEnabledForUrlState" ] + shouldHandleRequest ||= request.name in [ "executePageCommand", "checkEnabledAfterURLChange" ] # Requests with a frameId of zero should always and only be handled in the main/top frame (regardless of # whether Vimium is enabled there). if request.frameId == 0 and DomUtils.isTopFrame() @@ -587,27 +587,29 @@ onKeyup = (event) -> checkIfEnabledForUrl = -> url = window.location.toString() - chrome.runtime.sendMessage { handler: "isEnabledForUrl", url: url }, updateEnabledForUrlState - -updateEnabledForUrlState = (response) -> - { isEnabledForUrl, passKeys } = response - installListeners() - if HUD.isReady() and not isEnabledForUrl - # Quickly hide any HUD we might already be showing, e.g. if we entered insert mode on page load. - HUD.hide() - handlerStack.bubbleEvent "registerStateChange", - enabled: isEnabledForUrl - passKeys: passKeys - # Update the page icon, if necessary. - if document.hasFocus() - chrome.runtime.sendMessage - handler: "setIcon" - icon: - if isEnabledForUrl and not passKeys then "enabled" - else if isEnabledForUrl then "partial" - else "disabled" - null - + chrome.runtime.sendMessage { handler: "isEnabledForUrl", url: url }, (response) -> + { isEnabledForUrl, passKeys } = response + installListeners() # But only if they have not been installed already. + if HUD.isReady() and not isEnabledForUrl + # Quickly hide any HUD we might already be showing, e.g. if we entered insert mode on page load. + HUD.hide() + handlerStack.bubbleEvent "registerStateChange", + enabled: isEnabledForUrl + passKeys: passKeys + # Update the page icon, if necessary. + if windowIsFocused() + chrome.runtime.sendMessage + handler: "setIcon" + icon: + if isEnabledForUrl and not passKeys then "enabled" + else if isEnabledForUrl then "partial" + else "disabled" + null + +# 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). +checkEnabledAfterURLChange = -> + checkIfEnabledForUrl() if windowIsFocused() # Exported to window, but only for DOM tests. window.refreshCompletionKeys = (response) -> -- cgit v1.2.3