Skip to content

Instantly share code, notes, and snippets.

@fwenzel
Created March 31, 2012 22:41
Show Gist options
  • Save fwenzel/2269233 to your computer and use it in GitHub Desktop.
Save fwenzel/2269233 to your computer and use it in GitHub Desktop.
A way to add a Firefox toolbar button with the addons SDK
/** Hat tip to <http://stackoverflow.com/questions/5572632/> */
var data = require("self").data;
var {Cc, Ci} = require("chrome");
var mediator = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
/**
* Add a toolbar button to the main nav bar.
*
* options you may want to set:
* image, label, click
* use use tabs.activeTab.attach() in the click handler to execute scripts in
* the context of the browser tab
*/
function addToolbarButton(options) {
var document = mediator.getMostRecentWindow("navigator:browser").document;
var navBar = document.getElementById("nav-bar");
if (!navBar) return; // s.o.l.
var btn = document.createElement("toolbarbutton");
var defaults = {
type: 'button',
class: 'toolbarbutton-1',
orient: 'horizontal'
};
// Set attributes and click event handler
for (var o in options) {
if o == 'click' {
btn.addEventListener('click', options[o], false);
} else {
btn.setAttribute(o, options[o]);
}
}
// Set fallback attributes
for (var d in defaults) {
if (d not in options) {
btn.setAttribute(d, defaults[d]);
}
}
navBar.appendChild(btn);
}
exports.addToolbarButton = addToolbarButton;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment