diff options
| author | Stephen Blott | 2014-08-23 17:28:55 +0100 |
|---|---|---|
| committer | Stephen Blott | 2014-08-23 17:28:55 +0100 |
| commit | eeda751f54fd67bb895541264fcf2b5eb91b2556 (patch) | |
| tree | 6396afc561ae66dba3ed96fa92a4a6fcde860297 | |
| parent | 951f8839d02a8d85747d86ccd09efc0ee3a72501 (diff) | |
| download | vimium-eeda751f54fd67bb895541264fcf2b5eb91b2556.tar.bz2 | |
Allow passing of keys to the underlying page (populate page popup with existing rule).
| -rw-r--r-- | background_scripts/main.coffee | 36 | ||||
| -rw-r--r-- | pages/popup.coffee | 17 |
2 files changed, 40 insertions, 13 deletions
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 46a6a695..f3df2943 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -72,7 +72,7 @@ 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. # -isEnabledForUrl = (request) -> +root.isEnabledForUrl = isEnabledForUrl = (request) -> # Excluded URLs are stored as a series of URL expressions and optional passKeys, separated by newlines. # Lines for which the first non-blank character is "#" are comments. excludedLines = (line.trim() for line in Settings.get("excludedUrls").split("\n")) @@ -85,21 +85,39 @@ isEnabledForUrl = (request) -> passKeys = spec[1..].join("") if passKeys # Enabled, but not for these keys. - return { isEnabledForUrl: true, passKeys: passKeys } + return { isEnabledForUrl: true, passKeys: passKeys, matchingUrl: url } # Wholly disabled. - return { isEnabledForUrl: false } + return { isEnabledForUrl: false, passKeys: "", matchingUrl: url } # Enabled (the default). - { isEnabledForUrl: true } + { isEnabledForUrl: true, passKeys: undefined, matchingUrl: undefined } # Called by the popup UI. Strips leading/trailing whitespace and ignores empty strings. +# Also eliminates duplicates based on same URL/regexp. Of duplicates, the last is kept. +# Excluded URL specifications are kept in the same order as they were originally, with the new exclusion at +# the end. Passkeys, if any, are simply compied through within spec. root.addExcludedUrl = (url) -> return unless url = url.trim() - excludedUrls = Settings.get("excludedUrls") - return if excludedUrls.indexOf(url) >= 0 - - excludedUrls += "\n" + url - Settings.set("excludedUrls", excludedUrls) + excludedUrls = Settings.get("excludedUrls").split("\n") + excludedUrls.push(url) + + # Eliminate duplicates: reverse list, filter out duplicates based only on the URL/regexp, then reverse again + # and install the new excludedUrls. parse[0] is the URL/regexp. + seen = {} + newExcludedUrls = [] + for spec in excludedUrls.reverse() + spec = spec.trim() + parse = spec.split(/\s+/) + if parse.length == 0 or spec.indexOf("#") == 0 + # Keep comments and empty lines. + newExcludedUrls.push(spec) + else if !seen[parse[0]] + seen[parse[0]] = true + newExcludedUrls.push(spec) + else + console.log "addExcludedUrl: removing " + spec + + Settings.set("excludedUrls", newExcludedUrls.reverse().join("\n")) chrome.tabs.query({ windowId: chrome.windows.WINDOW_ID_CURRENT, active: true }, (tabs) -> updateActiveState(tabs[0].id)) diff --git a/pages/popup.coffee b/pages/popup.coffee index 6d7afafc..5332a596 100644 --- a/pages/popup.coffee +++ b/pages/popup.coffee @@ -1,10 +1,19 @@ onLoad = -> document.getElementById("optionsLink").setAttribute "href", chrome.runtime.getURL("pages/options.html") chrome.tabs.getSelected null, (tab) -> - # The common use case is to disable Vimium at the domain level. - # This regexp will match "http://www.example.com/" from "http://www.example.com/path/to/page.html". - domain = tab.url.match(/[^\/]*\/\/[^\/]*\//) or tab.url - document.getElementById("popupInput").value = domain + "*" + # Check if we have an existing exclusing rule for this page. + isEnabled = chrome.extension.getBackgroundPage().isEnabledForUrl(url: tab.url) + if isEnabled.matchingUrl + # There is an existing rule for this page. + pattern = isEnabled.matchingUrl + pattern += " " + isEnabled.passKeys if isEnabled.passKeys + document.getElementById("popupInput").value = pattern + else + # No existing exclusion rule. + # The common use case is to disable Vimium at the domain level. + # This regexp will match "http://www.example.com/" from "http://www.example.com/path/to/page.html". + domain = tab.url.match(/[^\/]*\/\/[^\/]*\//) or tab.url + document.getElementById("popupInput").value = domain + "*" onExcludeUrl = (e) -> url = document.getElementById("popupInput").value |
