Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Jetpack module for searching and executing browser commands

View commands.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/*
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
Something went wrong with that request. Please try again.