aboutsummaryrefslogtreecommitdiffstats
path: root/background_scripts
diff options
context:
space:
mode:
authorStephen Blott2016-02-27 15:25:28 +0000
committerStephen Blott2016-03-05 05:37:40 +0000
commitd388eddd192d925cda43700d7090ce8d52499df0 (patch)
treec7ed767e34a9e8ad6dc23101ac37d38f220f01aa /background_scripts
parent47de80f2fcb03c8741ab46308ce982209f74f6ac (diff)
downloadvimium-d388eddd192d925cda43700d7090ce8d52499df0.tar.bz2
Key bindings; remove legacy code.
Diffstat (limited to 'background_scripts')
-rw-r--r--background_scripts/commands.coffee1
-rw-r--r--background_scripts/main.coffee161
2 files changed, 1 insertions, 161 deletions
diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee
index a1002929..7e02047c 100644
--- a/background_scripts/commands.coffee
+++ b/background_scripts/commands.coffee
@@ -395,7 +395,6 @@ Settings.postUpdateHooks["keyMappings"] = (value) ->
Commands.clearKeyMappingsAndSetDefaults()
Commands.parseCustomKeyMappings value
Commands.generateKeyStateStructure()
- refreshCompletionKeysAfterMappingSave()
root = exports ? window
root.Commands = Commands
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee
index 41b9bf8a..1cb00a82 100644
--- a/background_scripts/main.coffee
+++ b/background_scripts/main.coffee
@@ -19,17 +19,9 @@ chrome.runtime.onInstalled.addListener ({ reason }) ->
func tab.id, { file: file, allFrames: contentScripts.all_frames }, checkLastRuntimeError
currentVersion = Utils.getCurrentVersion()
-keyQueue = "" # Queue of keys typed
-validFirstKeys = {}
-singleKeyCommands = []
frameIdsForTab = {}
root.urlForTab = {}
-# Keys are either literal characters, or "named" - for example <a-b> (alt+b), <left> (left arrow) or <f12>
-# This regular expression captures two groups: the first is a named key, the second is the remainder of
-# the string.
-namedKeyRegex = /^(<(?:[amc]-.|(?:[amc]-)?[a-z0-9]{2,5})>)(.*)$/
-
# This is exported for use by "marks.coffee".
root.tabLoadedHandlers = {} # tabId -> function()
@@ -189,14 +181,6 @@ fetchFileContents = (extensionFileName) ->
req.send()
req.responseText
-#
-# Returns the keys that can complete a valid command given the current key queue.
-#
-getCompletionKeysRequest = (request, keysToCheck = "") ->
- name: "refreshCompletionKeys"
- completionKeys: generateCompletionKeys(keysToCheck)
- validFirstKeys: validFirstKeys
-
TabOperations =
# Opens the url in the current tab.
openUrlInCurrentTab: (request, callback = (->)) ->
@@ -385,140 +369,6 @@ chrome.tabs.onUpdated.addListener (tabId, changeInfo, tab) ->
# End action functions
-splitKeyIntoFirstAndSecond = (key) ->
- if (key.search(namedKeyRegex) == 0)
- { first: RegExp.$1, second: RegExp.$2 }
- else
- { first: key[0], second: key.slice(1) }
-
-getActualKeyStrokeLength = (key) ->
- if (key.search(namedKeyRegex) == 0)
- 1 + getActualKeyStrokeLength(RegExp.$2)
- else
- key.length
-
-populateValidFirstKeys = ->
- for own key of Commands.keyToCommandRegistry
- if (getActualKeyStrokeLength(key) == 2)
- validFirstKeys[splitKeyIntoFirstAndSecond(key).first] = true
-
-populateSingleKeyCommands = ->
- for own key of Commands.keyToCommandRegistry
- if (getActualKeyStrokeLength(key) == 1)
- singleKeyCommands.push(key)
-
-# Invoked by options.coffee.
-root.refreshCompletionKeysAfterMappingSave = ->
- validFirstKeys = {}
- singleKeyCommands = []
-
- populateValidFirstKeys()
- populateSingleKeyCommands()
-
- sendRequestToAllTabs(getCompletionKeysRequest())
-
-# Generates a list of keys that can complete a valid command given the current key queue or the one passed in
-generateCompletionKeys = (keysToCheck) ->
- splitHash = splitKeyQueue(keysToCheck || keyQueue)
- command = splitHash.command
- count = splitHash.count
-
- completionKeys = singleKeyCommands.slice(0)
-
- if (getActualKeyStrokeLength(command) == 1)
- for own key of Commands.keyToCommandRegistry
- splitKey = splitKeyIntoFirstAndSecond(key)
- if (splitKey.first == command)
- completionKeys.push(splitKey.second)
-
- completionKeys
-
-splitKeyQueue = (queue) ->
- match = /([1-9][0-9]*)?(.*)/.exec(queue)
- count = parseInt(match[1], 10)
- command = match[2]
-
- { count: count, command: command }
-
-handleKeyDown = (sender) -> (request, port) ->
- key = request.keyChar
- if (key == "<ESC>")
- logMessage "clearing keyQueue", sender
- keyQueue = ""
- else
- logMessage "checking keyQueue: [#{keyQueue + key}]", sender
- keyQueue = checkKeyQueue(keyQueue + key, port.sender.tab.id, request.frameId)
- logMessage "new KeyQueue: #{keyQueue}", sender
- # Tell the content script whether there are keys in the queue.
- # FIXME: There is a race condition here. The behaviour in the content script depends upon whether this message gets
- # back there before or after the next keystroke.
- # That being said, I suspect there are other similar race conditions here, for example in checkKeyQueue().
- # Steve (23 Aug, 14).
- chrome.tabs.sendMessage(port.sender.tab.id,
- name: "currentKeyQueue",
- keyQueue: keyQueue)
-
-checkKeyQueue = (keysToCheck, tabId, frameId) ->
- refreshedCompletionKeys = false
- splitHash = splitKeyQueue(keysToCheck)
- command = splitHash.command
- count = splitHash.count
-
- return keysToCheck if command.length == 0
- count = 1 if isNaN(count)
-
- if (Commands.keyToCommandRegistry[command])
- registryEntry = Commands.keyToCommandRegistry[command]
- runCommand = true
- count *= registryEntry.options.count ? 1
-
- if registryEntry.noRepeat
- count = 1
- else if registryEntry.repeatLimit and count > registryEntry.repeatLimit
- runCommand = confirm """
- You have asked Vimium to perform #{count} repeats of the command:
- #{Commands.availableCommands[registryEntry.command].description}
-
- Are you sure you want to continue?
- """
-
- if runCommand
- if not registryEntry.isBackgroundCommand
- chrome.tabs.sendMessage tabId,
- name: "executePageCommand"
- command: registryEntry.command
- frameId: frameId
- count: count
- completionKeys: generateCompletionKeys ""
- registryEntry: registryEntry
- refreshedCompletionKeys = true
- else
- if registryEntry.passCountToFunction
- BackgroundCommands[registryEntry.command](count, frameId)
- else if registryEntry.noRepeat
- BackgroundCommands[registryEntry.command](frameId)
- else
- repeatFunction(BackgroundCommands[registryEntry.command], count, 0, frameId)
-
- newKeyQueue = ""
- else if (getActualKeyStrokeLength(command) > 1)
- splitKey = splitKeyIntoFirstAndSecond(command)
-
- # The second key might be a valid command by its self.
- if (Commands.keyToCommandRegistry[splitKey.second])
- newKeyQueue = checkKeyQueue(splitKey.second, tabId, frameId)
- else
- newKeyQueue = (if validFirstKeys[splitKey.second] then splitKey.second else "")
- else
- newKeyQueue = (if validFirstKeys[command] then count.toString() + command else "")
-
- # If we haven't sent the completion keys piggybacked on executePageCommand,
- # send them by themselves.
- unless refreshedCompletionKeys
- chrome.tabs.sendMessage(tabId, getCompletionKeysRequest(null, newKeyQueue), null)
-
- newKeyQueue
-
runBackgroundCommand = ({frameId, registryEntry, count}) ->
if registryEntry.passCountToFunction
BackgroundCommands[registryEntry.command] count, frameId
@@ -582,12 +432,10 @@ bgLog = (request, sender) ->
# Port handler mapping
portHandlers =
- keyDown: handleKeyDown,
completions: handleCompletions
sendRequestHandlers =
runBackgroundCommand: runBackgroundCommand
- getCompletionKeys: getCompletionKeysRequest
getCurrentTabUrl: getCurrentTabUrl
openUrlInNewTab: TabOperations.openUrlInNewTab
openUrlInIncognito: TabOperations.openUrlInIncognito
@@ -633,14 +481,7 @@ window.runTests = -> open(chrome.runtime.getURL('tests/dom_tests/dom_tests.html'
#
# Begin initialization.
#
-Commands.clearKeyMappingsAndSetDefaults()
-
-if Settings.has("keyMappings")
- Commands.parseCustomKeyMappings(Settings.get("keyMappings"))
-
-Commands.generateKeyStateStructure()
-populateValidFirstKeys()
-populateSingleKeyCommands()
+Settings.postUpdateHooks["keyMappings"] Settings.get "keyMappings"
# Show notification on upgrade.
showUpgradeMessage = ->