Skip to content

Commit

Permalink
Initial version of goer.
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Jan 30, 2011
0 parents commit 8194d4a
Show file tree
Hide file tree
Showing 7 changed files with 323 additions and 0 deletions.
22 changes: 22 additions & 0 deletions MIT-LICENSE.txt
@@ -0,0 +1,22 @@
Copyright (c) 2010 Conrad Irwin

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions background/handlers.js
@@ -0,0 +1,37 @@
/*global chrome*/
function handler(spec, done) {
return {
load: function (request) {
request.respond(Object.keys(spec));
},
done: function (request) {
done(spec[request.value], request);
}
};
}

handler.mark = handler({
"m": ["https://mail.google.com/"],
"e": ["chrome://extensions/"],
"s": ["chrome://settings/"]
}, function (line, request) {

function open(url) {
if (request.modifier === "'") {
chrome.tabs.update(request.tab.id, {url: url});
} else if (request.modifier === "`") {
chrome.tabs.create({url: url, index: request.tab.index + 1, windowId: request.tab.windowId});
}
}
if (line.length === 1) {
open(line[0]);
}
});

chrome.extension.onRequest.addListener(function (request, sender, respond) {
if (handler[request.name] && handler[request.name][request.action]) {
request.tab = sender.tab;
request.respond = respond;
handler[request.name][request.action](request, respond);
}
});
2 changes: 2 additions & 0 deletions background/index.html
@@ -0,0 +1,2 @@
<!DOCTYPE html>
<script src="handlers.js"></script>
98 changes: 98 additions & 0 deletions foreground/input.js
@@ -0,0 +1,98 @@
/*global document, window, chrome*/
/* The input object is a singleton that deals with getting the name of an
* action from the user.
*
* The way event handling works is first there is an indicator keydown
* event (which can only be reliably identified by keyCode), this then
* opens the input in order to read the rest of the event string, which may
* be any text identifiable by the actual characters.
*
* public:
* .activate(name, modifier) - A function to start accepting user-input for
* the given mode with the given modifier.
*
* internal:
* .input - The HTML input element the user is typing into.
* .timeout - If the user has typed a partial match, the timeout
* until we assume they're not going to type any more.
* .handle - A function that deals with what the user has typed.
*
* TODO: Rigourously test this, user-interaction in javascript is very hard
* to get right, and this is almost certainly too simplistic.
*/
function input(mode) {
if (!input.input) {
input.input = document.createElement('input');
input.input.onblur = function () {
input.input.parentElement.removeChild(input.input);
};
}
input.input.value = "";
input.input.onkeyup = function () {
input.handle(input.input.value);
};
document.body.appendChild(input.input);
input.input.focus();
}

input.activate = function (name, modifier) {
return function () {
input(name);

// Before the response list has loaded, we want to do nothing.
input.handle = function () {
return false;
};

// Get the list of possible arguments for this mode into response.
chrome.extension.sendRequest({name: name, action: 'load', modifier: modifier}, function (response) {
input.handle = function (value) {

// The user has typed something, so we need to reset our timer.
if (input.timeout) {
window.clearTimeout(input.timeout);
}

// The user has pressed backspace and deleted the modifier.
if (!value) {
input.input.blur();
return;

// Otherwise the modifier should still be in there.
} else if (value.indexOf(modifier) === 0) {
value = value.substr(1);
}

// Get the list of possible arguments filtered by prefix match
// with what the user has typed so far.
var results = response.filter(function (possible) {
return possible.indexOf(value) === 0;
});

// If the user has typed an unambiguous match, accept it.
if (results.length === 1 && results[0] === value) {
chrome.extension.sendRequest({name: name, action: 'done', value: value, modifier: modifier});
input.input.blur();

// Otherwise if they're on the way to a match, wait for them.
} else if (results.length) {
input.timeout = window.setTimeout(function () {
if (results.indexOf(value) > -1) {
chrome.extension.sendRequest({name: name, action: 'done', value: value, modifier: modifier});
}
input.input.blur();
}, 1000);

// Though if they've typed nonsense, we'll get out of the way.
} else {
input.input.blur();
}
};

// If the user has typed a key before the response list was loaded,
// handle their input here.
input.handle(input.input.value);
});
};
};

99 changes: 99 additions & 0 deletions foreground/keys.js
@@ -0,0 +1,99 @@
/* This file is heavily based on the Vimium source code, which bears
* the following notice:
*
* Copyright (c) 2010 Phil Crosby, Ilya Sukhar.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

/*global navigator*/

// Get a vim key escape based on a keydown event.
// NOTE: This has many (many) limitations and should be used sparingly.
var keyDownToVim = (function () {
var keyCodes = { ESC: 27, backspace: 8, tab: 9, deleteKey: 46, enter: 13, space: 32, shiftKey: 16, f1: 112, f12: 123},
// More vimmy names for some keys (TODO: expand)
keyNames = { 27: "esc", 37: "left", 38: "up", 39: "right", 40: "down" },

// WebKit key-down events have the wrong charCode except on Mac.
// https://bugs.webkit.org/show_bug.cgi?id=19906 for more details.
correctors = /Mac/.test(navigator.userAgent) ? {} : {
"U+00C0": ["U+0060", "U+007E"], // `~
"U+00BD": ["U+002D", "U+005F"], // -_
"U+00BB": ["U+003D", "U+002B"], // =+
"U+00DB": ["U+005B", "U+007B"], // [{
"U+00DD": ["U+005D", "U+007D"], // ]}
"U+00DC": ["U+005C", "U+007C"], // \|
"U+00BA": ["U+003B", "U+003A"], // ;:
"U+00DE": ["U+0027", "U+0022"], // '"
"U+00BC": ["U+002C", "U+003C"], // ,<
"U+00BE": ["U+002E", "U+003E"], // .>
"U+00BF": ["U+002F", "U+003F"] // /?
};

function getKeyChar(e) {
if (!/^U\+/.test(e.keyIdentifier)) {
if (keyNames[e.keyCode]) {
return keyNames[e.keyCode];
} else if (e.keyCode >= keyCodes.f1 && e.keyCode <= keyCodes.f12) {
return "f" + (1 + e.keyCode - keyCodes.f1);
} else {
return e.keyIdentifier;
}
} else if (keyNames[e.keyCode]) {
return keyNames[e.keyCode];
}

var keyIdentifier = (correctors[e.keyIdentifier] ? correctors[e.keyIdentifier][e.shiftKey ? 1 : 0] : e.keyIdentifier),
keyChar = String.fromCharCode(parseInt(keyIdentifier.replace(/^U+/, ''), 16));

if (e.shiftKey) {
return keyChar.toUpperCase();
} else {
return keyChar.toLowerCase();
}
}

return function (e) {
var modifiers = [],
keyChar = getKeyChar(e);

if (e.metaKey) {
modifiers.push("m");
}
if (e.ctrlKey) {
modifiers.push("c");
}
if (e.altKey) {
modifiers.push("a");
}

if (modifiers.length) {
modifiers.push(keyChar);
return "<" + modifiers.join("-") + ">";
} else if (keyChar.length > 1) {
return "<" + keyChar + ">";
} else {
return getKeyChar(e);
}
};
}());
48 changes: 48 additions & 0 deletions foreground/modes.js
@@ -0,0 +1,48 @@
/*global document, window, keyDownToVim, input*/

/* The modes object is a singleton that handles the keyboard modes.
*
* Each mode can have two "minor-modes", one when focussed on an element,
* and the other when not interacting with the current page.
*
* These are specified as a mapping of keys to functions, where the
* keyDownToVim helper is used to convert between keydown events and
* the keys.
*/
function modes(normal, focussed) {
function press(charcode, spec, e) {
if (spec[charcode]) {
spec[charcode](e);
}
}
return {
handle: function (e, normal_mode) {
press(keyDownToVim(e), (normal_mode ? normal : focussed), e);
}
};
}
document.addEventListener('keydown', function (e) {
(modes.active || modes.normal).handle(e, (document.activeElement === document.body));
}, true);
modes.activate = function (name) {
return function (e) {
modes.active = modes[name];
};
};

// Interact mode defines as few keyboard mappings as possible,
// only an escape when not focussed to get out of this mode.
modes.interact = modes({
"<esc>": modes.activate("normal")
}, {});

// Normal mode is where we spend the vast majority of time.
modes.normal = modes({
"i": modes.activate("interact"),
"'": input.activate("mark", "'"),
"`": input.activate("mark", "`")
}, {
"<esc>": function () {
document.activeElement.blur();
}
});
17 changes: 17 additions & 0 deletions manifest.json
@@ -0,0 +1,17 @@
{
"name": "Goer",
"version": "0.1",
"description": "Some people like to browse, other's prefer to go directly.",
"permissions": ["<all_urls>"],
"background_page": "background/index.html",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["foreground/keys.js",
"foreground/input.js",
"foreground/modes.js"],
"run_at": "document_start",
"all_frames": true
}
]
}

0 comments on commit 8194d4a

Please sign in to comment.