Friday, April 8, 2011

How can I make my Firefox extension toolbar button appear automatically?

I've created a firefox extension that consists of a toolbar button. How can I set it up so that when my extension is installed, the button automatically appears in the main toolbar. I don't want my users to have to go to the customize toolbar menu and drag my button over.

From stackoverflow
  • See https://developer.mozilla.org/En/Code_snippets:Toolbar#Adding_button_by_default

  • May not be a good idea, the user may not want the extension to "congigure" his browser.

    I really hate things messing with my presets.

  • We're using the following code....

    function init() {
    
        // .... 
    
        var navbar = document.getElementById("nav-bar");
        if ((myExtensionShared.checkMyBtnInstalled() == false) &&
            (navbar != null && document.getElementById("myExtension-button") == null)) {
            var newset;
                if (navbar.getAttribute('currentset') && 
                  navbar.getAttribute('currentset').indexOf('myExtension-button') == -1) {
    
                    navbar.insertItem ('myExtension-button', null, null, false);
                    newset = navbar.getAttribute('currentset') + ',myExtension-button';
                    navbar.setAttribute('currentset', newset);
                    document.persist('nav-bar', 'currentset');
                }
                else if (!navbar.getAttribute('currentset')) {
    
                    navbar.insertItem ('myExtension-button', null, null, false);
                    newset=navbar.getAttribute('defaultset') + ',myExtension-button';
                    navbar.setAttribute('currentset', newset);
                    document.persist('nav-bar', 'currentset');
                }
    
        }
    
        // .... 
    
    }
    
    
    
    myExtensionShared.prototype.checkMyBtnInstalled = function() {
        var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                                        .getService(Components.interfaces.nsIPrefBranch);
        var btnInstalled = false;
        if (prefs.prefHasUserValue("extensions.myExtension.myBtnInstalled")) {
            btnInstalled = prefs.getBoolPref("extensions.myExtension.myBtnInstalled");
        }
        if (!btnInstalled) {
            prefs.setBoolPref("extensions.myExtension.myBtnInstalled", true);
        }
        return btnInstalled;
    }
    
  • We are using the following code that will append the button (if already exist somewhere else in the bar).

    //...
    appendButtonInToolbar:function(buttonId, toolbarId) {
        var toolbar = document.getElementById(toolbarId);
        var button = document.getElementById(buttonId);
        if(button) {
            var parentBar = button.parentNode;        
            if(parentBar && parentBar != toolbar) {
                var newset = this.removeButtonFromToolbarCurrentSet(parentBar,buttonId);              
            }
            toolbar.appendChild(button);
        }else{          
            toolbar.insertItem(buttonId);
        }
    
        this.appendButtonInToolbarCurrentSet(toolbar,buttonId);
    },
    
    appendButtonInToolbarCurrentSet:function(toolbar, buttonId) {
        var oldset = toolbar.getAttribute("currentset");
        var newset = "";
        if(oldset && oldset!="") {
            newset = oldset + ",";
        }        
        newset += buttonId;        
        toolbar.setAttribute("currentset", newset);
        document.persist(toolbar.id,"currentset");
        return newset;
    },
    
    
    removeButtonFromToolbarCurrentSet:function(toolbar, buttonId) {
        var oldset = toolbar.getAttribute("currentset");
        if(!oldset || oldset=="" || oldset.indexOf(buttonId) == -1) return oldset;
        var reg = new RegExp(buttonId+",?", "gi");        
        var newset = oldset.replace(reg,"");
        if (newset.charAt(newset.length-1) == ",") {
           newset = newset.substring(0, newset.length - 1);
        }
    
        toolbar.setAttribute("currentset", newset);
        document.persist(toolbar.id,"currentset");
        return newset;
    }, 
    //...
    

0 comments:

Post a Comment