aboutsummaryrefslogtreecommitdiffstats
path: root/background_page.html
diff options
context:
space:
mode:
authorilya2009-12-05 15:49:02 -0800
committerilya2009-12-05 15:59:00 -0800
commit066ed27f2080fd0d4e7cb08559ac2183557b9f9a (patch)
tree239dde225dad6af76e2b4ad8787518d5f6515d8d /background_page.html
parenta16f45ca8695ce40e71c3a1c90acb7ccb63046b3 (diff)
downloadvimium-066ed27f2080fd0d4e7cb08559ac2183557b9f9a.tar.bz2
Refactor the command parser to not store invalid keys in the two-key buffer. Also, if the second key is itself a valid first key or a standalone command, keep it or execute it accordingly. Closes #20.
Diffstat (limited to 'background_page.html')
-rw-r--r--background_page.html40
1 files changed, 31 insertions, 9 deletions
diff --git a/background_page.html b/background_page.html
index a370ec2e..bc908481 100644
--- a/background_page.html
+++ b/background_page.html
@@ -3,6 +3,7 @@
<script type="text/javascript" charset="utf-8">
var tabQueue = {}; // windowId -> Array
var keyQueue = ""; // Queue of keys typed
+ var validFirstKeys = {};
var defaultSettings = { "scrollStepSize": 60 };
@@ -215,23 +216,32 @@
keyToCommandRegistry['d'] = removeTab;
keyToCommandRegistry['u'] = restoreTab;
+
+ function populateValidFirstKeys() {
+ for (var key in keyToCommandRegistry)
+ {
+ if (key.length == 2)
+ validFirstKeys[key[0]] = true;
+ }
+ }
+
function handleKeyDown(key) {
- keyQueue = keyQueue + key;
- console.log("current keyQueue: [", keyQueue, "]");
- checkKeyQueue();
+ console.log("checking keyQueue: [", keyQueue + key, "]");
+ keyQueue = checkKeyQueue(keyQueue + key);
+ console.log("new KeyQueue: " + keyQueue);
}
- function checkKeyQueue() {
- var match = /([0-9]*)(.*)/.exec(keyQueue);
+ function checkKeyQueue(keysToCheck) {
+ var match = /([0-9]*)(.*)/.exec(keysToCheck);
var count = parseInt(match[1]);
var command = match[2];
- if (command.length == 0) { return; }
+ if (command.length == 0) { return keysToCheck; }
if (isNaN(count)) { count = 1; }
if (keyToCommandRegistry[command]) {
registryEntry = keyToCommandRegistry[command];
- console.log("command found for [", keyQueue, "],", registryEntry);
+ console.log("command found for [", keysToCheck, "],", registryEntry);
if (typeof(registryEntry) == "string") {
chrome.tabs.getSelected(null, function(tab) {
@@ -242,11 +252,23 @@
repeatFunction(registryEntry, count, 0);
}
- keyQueue = "";
+ return "";
} else if (command.length > 1) {
- keyQueue = "";
+ // The second key might be a valid command by its self.
+ if (keyToCommandRegistry[command[1]])
+ return checkKeyQueue(command[1]);
+ else
+ return (validFirstKeys[command[1]] ? command[1] : "");
+ } else {
+ return (validFirstKeys[command] ? count.toString() + command : "");
}
}
+
+ function init() {
+ populateValidFirstKeys();
+ }
+
+ init();
</script>
</head>