Skip to content

Instantly share code, notes, and snippets.

@autonome
Created November 22, 2010 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save autonome/709681 to your computer and use it in GitHub Desktop.
Save autonome/709681 to your computer and use it in GitHub Desktop.
Jetpack module for searching and executing browser commands
/*
Module for searching available commands in the browser.
Allows searching and execution of commands in Firefox, such as
'Back', 'Zoom In', 'Reload'.
Example that adds buttons to the add-on bar for every command,
which trigger the command when clicked:
const widget = require("widget");
const commands = require("commands");
commands.search("", function(command) {
widget.Widget({
label: command.alias,
content: command.alias,
onClick: command.execute
});
});
API
The 'search' method takes two parameters. the first is a string of text
to match against command names. For example, "zo" will match the commands
"Zoom In" and "Zoom Out". The second parameter is a callback that'll be
passed the command objects for all commands matching the search. The command
object has an 'alias' property, which contains a label used in the application
to refer to the command. The command object has an 'execute' method which
will trigger the command.
Implementation
The module enumerates all <command> elements found in the active browser
window, and then searches for any element in the browser that has a
command attribute with that command's id, storing the item's label
attribute value. The DOM traveral is only done once, on the first invocation
of the 'search' method.
*/
let commands = {};
let aliases = {};
let cached = false;
function cacheCommands() {
let window = require("window-utils").activeBrowserWindow;
let document = window.document;
let commandEls = document.querySelectorAll("command");
for (let i = 0; i < commandEls.length; i++) {
let commandEl = commandEls[i];
let labels = [];
let els = document.querySelectorAll("*[command=\"" + commandEl.id + "\"]");
if (els) {
for (let i = 0; i < els.length; i++) {
if (els[i].label) {
labels.push(els[i].label);
}
}
}
if (labels.length) {
commands[commandEl.id] = commandEl.id;
labels.forEach(function(label) aliases[label] = commandEl.id);
}
}
}
function command(id, alias) {
this.alias = alias;
this.execute = function() {
if (commands[id]) {
let document = require("window-utils").activeBrowserWindow.document
let el = document.getElementById(id);
if (el)
el.doCommand();
}
};
}
exports.search = function(text, callback) {
if (!cached) {
cacheCommands();
cached = true;
}
for (let [alias, id] in Iterator(aliases)) {
if (!text.length || (new RegExp(text, "i")).test(alias))
callback(new command(id, alias));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment