Created
December 14, 2012 14:05
-
-
Save anonymous/4285679 to your computer and use it in GitHub Desktop.
Adding a Toolbar Button in a Bootstrapped Firefox Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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