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

∥☆過路亽.° 提交于 2020-06-24 05:44:35

问题


So I've looked over the documentation for the Add-on SDK several times now and no where can I see how to create toolbars or modify existing ones. They have a tutorial on creating add-on bar icons but thats not what I want. Does the Add-on SDK support this yet? If it does, can someone link me to an example/tutorial.


回答1:


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);
}



回答2:


This an embellishment on the first answer.

If you have the difficulties described by dcolish in his comment to the top response 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();
}

It's crude but works.

EDIT: Much easier and cleaner is:

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

On my Mac Firefox 15 automatically removes the Icon when closing a window. So window.on('close', ...) is not needed.



来源:https://stackoverflow.com/questions/5572632/firefox-how-can-i-add-modify-toolbars-using-the-add-on-sdk-jetpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!