Create a gist now

Instantly share code, notes, and snippets.

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");"", function(command) {
label: command.alias,
content: command.alias,
onClick: command.execute
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.
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=\"" + + "\"]");
if (els) {
for (let i = 0; i < els.length; i++) {
if (els[i].label) {
if (labels.length) {
commands[] =;
labels.forEach(function(label) aliases[label] =;
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)
} = function(text, callback) {
if (!cached) {
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