Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 52ae489443d9b52637f85b653a64e584743d2168 @ttaubert committed Mar 16, 2011
Showing with 188 additions and 0 deletions.
  1. 0 README
  2. +5 −0 README.md
  3. +2 −0 docs/main.md
  4. +33 −0 lib/browser-windows.js
  5. +21 −0 lib/keys.js
  6. +44 −0 lib/main.js
  7. +29 −0 lib/modifiers.js
  8. +13 −0 lib/shortcuts.js
  9. +9 −0 package.json
  10. +32 −0 tests/test-main.js
0 README
No changes.
5 README.md
@@ -0,0 +1,5 @@
+This is the DynamicLocationBar add-on. It contains:
+
+* A program (lib/main.js).
+* A few tests.
+* Some meager documentation.
2 docs/main.md
@@ -0,0 +1,2 @@
+The main module is a program that creates a widget. When a user clicks on
+the widget, the program loads the mozilla.org website in a new tab.
33 lib/browser-windows.js
@@ -0,0 +1,33 @@
+const {Cc, Ci} = require("chrome");
+const WINDOW_TYPE = "navigator:browser";
+
+let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
+let browserWindowEnum = wm.getEnumerator(WINDOW_TYPE);
+
+// TODO later, handle new windows
+let addEventListener = function (type, callback) {
+ for (let window in getWindows())
+ window.addEventListener(type, callback, false);
+}
+
+let removeEventListener = function (type, callback) {
+ for (let window in getWindows())
+ window.removeEventListener(type, callback, false);
+}
+
+let getWindows = function () {
+ while (browserWindowEnum.hasMoreElements())
+ yield browserWindowEnum.getNext();
+}
+
+let getMostRecentWindow = function () {
+ return wm.getMostRecentWindow(WINDOW_TYPE);
+}
+
+let getElementById = function (id) {
+ return getMostRecentWindow().document.getElementById(id);
+}
+
+exports.addEventListener = addEventListener;
+exports.removeEventListener = removeEventListener;
+exports.getElementById = getElementById;
21 lib/keys.js
@@ -0,0 +1,21 @@
+let modifiers = require("modifiers");
+let browserWindows = require("browser-windows");
+
+let Key = function (id) {
+ let element = browserWindows.getElementById(id);
+
+ this.key = element.getAttribute("key").toUpperCase().charCodeAt(0);
+ this.command = element.getAttribute("command");
+ this.modifiers = element.hasAttribute("modifiers") ? element.getAttribute("modifiers").split(",") : [];
+}
+
+Key.prototype.equalsEvent = function (event) {
+ return (this.key == event.keyCode && modifiers.equalsEvent(event, this.modifiers));
+}
+
+Key.prototype.executeCommand = function () {
+ let cmd = browserWindows.getElementById(this.command);
+ cmd && cmd.doCommand();
+}
+
+exports.Key = Key;
44 lib/main.js
@@ -0,0 +1,44 @@
+let Key = require("keys").Key;
+let Shortcut = require("shortcuts").Shortcut;
+let browserWindows = require("browser-windows");
+
+// TODO later: read from config
+let shortcuts = [
+ new Shortcut(new Key("key_tabview"), ["accel"], 32),
+ new Shortcut(new Key("key_viewSource"), ["accel", "shift"], 69)
+];
+
+let onKeyDown = function (event) {
+ let keys = [];
+
+ // check if this is a custom shortcut
+ let isCustom = shortcuts.some(function (shortcut) {
+ let key = shortcut.getKey();
+
+ if (shortcut.equalsEvent(event)) {
+ key.executeCommand();
+ return true;
+ }
+
+ keys.push(key);
+ return false;
+ });
+
+ // check if this is a shortcut that is overriden by a custom one
+ let isOverridden = !isCustom && keys.some(function (key) {
+ return key.equalsEvent(event);
+ });
+
+ if (isCustom || isOverridden) {
+ event.preventDefault();
+ event.stopPropagation();
+ }
+}
+
+exports.main = function (options, callbacks) {
+ browserWindows.addEventListener("keydown", onKeyDown);
+};
+
+exports.onUnload = function (reason) {
+ browserWindows.removeEventListener("keydown", onKeyDown);
+};
29 lib/modifiers.js
@@ -0,0 +1,29 @@
+const {Cc, Ci} = require("chrome");
+
+let prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);
+let accelKey = prefs.getBranch("ui.key.").getIntPref("accelKey");
+
+let isAccelKeyPressed = function (event) {
+ if (17 == accelKey)
+ return event.ctrlKey;
+ if (18 == accelKey)
+ return event.altKey;
+ if (224 == accelKey)
+ return event.metaKey;
+
+ return false;
+}
+
+let equalsEvent = function (event, modifiers) {
+ let eventModifiers = {
+ "control": event.ctrlKey,
+ "accel": isAccelKeyPressed(event),
+ "shift": event.shiftKey,
+ "meta": event.metaKey,
+ "alt": event.altKey
+ };
+
+ return modifiers.every(function (mod) eventModifiers[mod.toLowerCase()])
+}
+
+exports.equalsEvent = equalsEvent;
13 lib/shortcuts.js
@@ -0,0 +1,13 @@
+let modifiers = require("modifiers");
+
+let Shortcut = function (key, modifiers, keyCode) {
+ this.getKey = function () key;
+ this.getModifiers = function () modifiers;
+ this.getKeyCode = function () keyCode;
+}
+
+Shortcut.prototype.equalsEvent = function (event) {
+ return (this.getKeyCode() == event.keyCode && modifiers.equalsEvent(event, this.getModifiers()));
+}
+
+exports.Shortcut = Shortcut;
9 package.json
@@ -0,0 +1,9 @@
+{
+ "name": "shortcuts",
+ "license": "MPL 1.1/GPL 2.0/LGPL 2.1",
+ "author": "Tim Taubert",
+ "version": "0.1",
+ "fullName": "Shortcuts",
+ "id": "shortcuts@timtaubert.de",
+ "description": "Customize shortcuts"
+}
32 tests/test-main.js
@@ -0,0 +1,32 @@
+const main = require("main");
+
+exports.test_test_run = function(test) {
+ test.pass("Unit test running!");
+};
+
+exports.test_id = function(test) {
+ test.assert(require("self").id.length > 0);
+};
+
+exports.test_url = function(test) {
+ require("request").Request({
+ url: "http://www.mozilla.org/",
+ onComplete: function(response) {
+ test.assertEqual(response.statusText, "OK");
+ test.done();
+ }
+ }).get();
+ test.waitUntilDone(20000);
+};
+
+exports.test_open_tab = function(test) {
+ const tabs = require("tabs");
+ tabs.open({
+ url: "http://www.mozilla.org/",
+ onReady: function(tab) {
+ test.assertEqual(tab.url, "http://www.mozilla.org/");
+ test.done();
+ }
+ });
+ test.waitUntilDone(20000);
+};

0 comments on commit 52ae489

Please sign in to comment.
Something went wrong with that request. Please try again.