diff options
| author | Stephen Blott | 2014-12-30 12:18:12 +0000 | 
|---|---|---|
| committer | Stephen Blott | 2014-12-30 12:18:12 +0000 | 
| commit | 0b610a6d707bd4cd1eb421605d15d276721ef493 (patch) | |
| tree | 4722bc92a9931e91e8a4109e5ce16a971c3d2dac /background_scripts | |
| parent | ba743054dcd26b225db407db261e480ab485e2d0 (diff) | |
| parent | bab498fcf2a54cacd2d1751106bf6c499644e0dc (diff) | |
| download | vimium-0b610a6d707bd4cd1eb421605d15d276721ef493.tar.bz2 | |
Merge branch 'passkeys---union-of-rules' of https://github.com/smblott-github/vimium into smblott-github-passkeys---union-of-rules
Diffstat (limited to 'background_scripts')
| -rw-r--r-- | background_scripts/exclusions.coffee | 35 | ||||
| -rw-r--r-- | background_scripts/main.coffee | 19 | 
2 files changed, 18 insertions, 36 deletions
diff --git a/background_scripts/exclusions.coffee b/background_scripts/exclusions.coffee index 2b34238b..55ced3ef 100644 --- a/background_scripts/exclusions.coffee +++ b/background_scripts/exclusions.coffee @@ -15,17 +15,27 @@ RegexpCache =  # The Exclusions class manages the exclusion rule setting.  # An exclusion is an object with two attributes: pattern and passKeys. -# The exclusions are an array of such objects (because the order matters). +# The exclusions are an array of such objects.  root.Exclusions = Exclusions = +  # Make RegexpCache, which is required on the page popup, accessible via the Exclusions object. +  RegexpCache: RegexpCache    rules: Settings.get("exclusionRules") -  # Return the first exclusion rule matching the URL, or null. -  getRule: (url) -> -    for rule in @rules -      return rule if url.match(RegexpCache.get(rule.pattern)) -    return null +  # Merge the matching rules for URL, or null.  In the normal case, we use the configured @rules; hence, this +  # is the default.  However, when called from the page popup, we are testing what effect candidate new rules +  # would have on the current tab.  In this case, the candidate rules are provided by the caller. +  getRule: (url, rules=@rules) -> +    matches = (rule for rule in rules when rule.pattern and 0 <= url.search(RegexpCache.get(rule.pattern))) +    # An absolute exclusion rule (with no passKeys) takes priority. +    for rule in matches +      return rule unless rule.passKeys +    if 0 < matches.length +      pattern: (rule.pattern for rule in matches).join " | " # Not used; for debugging only. +      passKeys: Utils.distinctCharacters (rule.passKeys for rule in matches).join "" +    else +      null    setRules: (rules) ->      # Callers map a rule to null to have it deleted, and rules without a pattern are useless. @@ -35,19 +45,6 @@ root.Exclusions = Exclusions =    postUpdateHook: (rules) ->      @rules = rules -  # Update an existing rule or add a new rule. -  updateOrAdd: (newRule) -> -    seen = false -    @rules.push(newRule) -    @setRules @rules.map (rule) -> -      if rule.pattern == newRule.pattern -        if seen then null else seen = newRule -      else -        rule - -  remove: (pattern) -> -    @setRules(@rules.filter((rule) -> rule and rule.pattern != pattern)) -  # Development and debug only.  # Enable this (temporarily) to restore legacy exclusion rules from backup.  if false and Settings.has("excludedUrlsBackup") diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 647923c0..cebb38ca 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -75,26 +75,10 @@ getCurrentTabUrl = (request, sender) -> sender.tab.url  root.isEnabledForUrl = isEnabledForUrl = (request) ->    rule = Exclusions.getRule(request.url)    { -    rule: rule      isEnabledForUrl: not rule or rule.passKeys      passKeys: rule?.passKeys or ""    } -# Called by the popup UI. -# If the URL pattern matches an existing rule, then the existing rule is updated. Otherwise, a new rule is created. -root.addExclusionRule = (pattern,passKeys) -> -  if pattern = pattern.trim() -    Exclusions.updateOrAdd({ pattern: pattern, passKeys: passKeys }) -    chrome.tabs.query({ windowId: chrome.windows.WINDOW_ID_CURRENT, active: true }, -      (tabs) -> updateActiveState(tabs[0].id)) - -# Called by the popup UI.  Remove all existing exclusion rules with this pattern. -root.removeExclusionRule = (pattern) -> -  if pattern = pattern.trim() -    Exclusions.remove(pattern) -    chrome.tabs.query({ windowId: chrome.windows.WINDOW_ID_CURRENT, active: true }, -      (tabs) -> updateActiveState(tabs[0].id)) -  saveHelpDialogSettings = (request) ->    Settings.set("helpDialog_showAdvancedCommands", request.showAdvancedCommands) @@ -353,7 +337,8 @@ setBrowserActionIcon = (tabId,path) ->  # Updates the browserAction icon to indicate whether Vimium is enabled or disabled on the current page.  # Also propagates new enabled/disabled/passkeys state to active window, if necessary.  # This lets you disable Vimium on a page without needing to reload. -updateActiveState = (tabId) -> +# Exported via root because it's called from the page popup. +root.updateActiveState = updateActiveState = (tabId) ->    enabledIcon = "icons/browser_action_enabled.png"    disabledIcon = "icons/browser_action_disabled.png"    partialIcon = "icons/browser_action_partial.png"  | 
