aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormrmr19932015-04-24 12:16:36 +0100
committermrmr19932015-04-24 14:39:40 +0100
commitb3986dcd68a9383b3552ffb99c13c19e94bd08e4 (patch)
tree0145d53ef941c52e53ed654ec552ee21b0041337
parent8a659af44a8205f39e4c0e04146978447ca3f38e (diff)
downloadvimium-b3986dcd68a9383b3552ffb99c13c19e94bd08e4.tar.bz2
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.
-rw-r--r--background_scripts/main.coffee9
-rw-r--r--content_scripts/vimium_frontend.coffee42
-rw-r--r--manifest.json1
3 files changed, 33 insertions, 19 deletions
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee
index 223b0d74..ab92559f 100644
--- a/background_scripts/main.coffee
+++ b/background_scripts/main.coffee
@@ -96,6 +96,15 @@ root.isEnabledForUrl = isEnabledForUrl = (request) ->
passKeys: rule?.passKeys or ""
}
+isEnabledForUpdatedUrl = (details) ->
+ message = isEnabledForUrl details
+ message.name = "updateEnabledForUrlState"
+ chrome.tabs.sendMessage details.tabId, message, {frameId: details.frameId}
+
+# Re-check whether Vimium is enabled for a frame when the url changes without a reload.
+chrome.webNavigation.onHistoryStateUpdated.addListener isEnabledForUpdatedUrl # history.pushState.
+chrome.webNavigation.onReferenceFragmentUpdated.addListener isEnabledForUpdatedUrl # Hash changed.
+
# Retrieves the help dialog HTML template from a file, and populates it with the latest keybindings.
# This is called by options.coffee.
root.helpDialogHtml = (showUnboundCommands, showCommandNames, customTitle) ->
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.
diff --git a/manifest.json b/manifest.json
index 4ba222fb..e8e51407 100644
--- a/manifest.json
+++ b/manifest.json
@@ -28,6 +28,7 @@
"storage",
"sessions",
"notifications",
+ "webNavigation",
"<all_urls>"
],
"content_scripts": [