Firefox: how can I add / change toolbars using the Add-on SDK (Jetpack)

So, I looked at the documentation for the Add-on SDK several times, and no where can I see how to create toolbars or modify existing ones. They have a tutorial on creating superscript icons, but that’s not what I want. Does the SDK dock support this yet? If so, can anyone connect me with an example / tutorial.

+6
firefox firefox-addon firefox-addon-sdk toolbar
source share
2 answers

This works for me:

var data = require("self").data; var {Cc, Ci} = require("chrome"); var mediator = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator); exports.main = function(options, callbacks) { addToolbarButton(); // other stuff }; function addToolbarButton() { var document = mediator.getMostRecentWindow("navigator:browser").document; var navBar = document.getElementById("nav-bar"); if (!navBar) { return; } var btn = document.createElement("toolbarbutton"); btn.setAttribute('type', 'button'); btn.setAttribute('class', 'toolbarbutton-1'); btn.setAttribute('image', data.url('img/icon16.png')); // path is relative to data folder btn.setAttribute('orient', 'horizontal'); btn.setAttribute('label', 'My App'); btn.addEventListener('click', function() { // use tabs.activeTab.attach() to execute scripts in the context of the browser tab console.log('clicked'); }, false) navBar.appendChild(btn); } 
+11
source share

This is the embellishment of the first answer.

If you have the difficulties described by dcolish in his comment on the top answer, add this to main.js:

 var tim = require("timers"); intervalId = tim.setInterval(timerFn,2000); function timerFn() { var win = mediator.getMostRecentWindow('navigator:browser'); if (win) var document = win.document; else return; var isBtn = document.getElementById('myappbutton-id'); if (!isBtn) addToolbarButton(); } 

This is rude, but it works.

EDIT: Much simpler and cleaner:

 var windows = require("windows").browserWindows; windows.on('open', function(window) { addToolbarButton(); }); 

On my Mac, Firefox 15 automatically removes the icon when the window closes. Therefore window.on('close', ...) not required.

+7
source share

All Articles