Skip to content

Instantly share code, notes, and snippets.

Created December 14, 2012 14:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4285679 to your computer and use it in GitHub Desktop.
Save anonymous/4285679 to your computer and use it in GitHub Desktop.
Adding a Toolbar Button in a Bootstrapped Firefox Extension
function addButton(toolbarId, buttonId, label, iconPath, firstRun) {
var toolbar = document.getElementById(toolbarId);
var toolbarButton = document.createElement("toolbarbutton");
toolbarButton.setAttribute("id", buttonId);
toolbarButton.setAttribute("type", "button");
toolbarButton.setAttribute("removable", "true");
toolbarButton.setAttribute("class",
"toolbarbutton-1 chromeclass-toolbar-additional");
toolbarButton.setAttribute("label", label);
toolbarButton.style.listStyleImage = "url(" + iconPath + ")";
var palette = document.getElementById("navigator-toolbox").palette;
palette.appendChild(toolbarButton);
var currentset = toolbar.getAttribute("currentset").split(",");
var index = currentset.indexOf(buttonId);
if (index == -1) {
if (firstRun) {
// No button yet so add it to the toolbar.
toolbar.appendChild(toolbarButton);
toolbar.setAttribute("currentset", toolbar.currentSet);
document.persist(toolbar.id, "currentset");
}
}
else {
// The ID is in the currentset, so find the position and
// insert the button there.
var before = null;
for (var i=index+1; i<currentset.length; i++) {
before = document.getElementById(currentset[i]);
if (before) {
toolbar.insertItem(buttonId, before);
break;
}
}
if (!before) {
toolbar.insertItem(buttonId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment