3

我使用https://github.com/erikvold/toolbarbutton-jplib/包将一个工具栏按钮从 addon-sdk 插件添加到 firefox 导航栏。是否可以向此按钮添加下拉菜单(据我所知没有简单的方法)?

4

2 回答 2

4

我就是这样做的;希望对你有效。

var winUtils = require("window-utils");

var delegate = {
    onTrack: function(window) {
        if(window.location != "chrome://browser/content/browser.xul") {
            // console.log("=> win location false");
            return;
        }

        console.log("window tracked");

        var document = window.document;
        var navBar = document.getElementById('nav-bar');

        var btn = document.createElement('toolbarbutton');  
        btn.setAttribute('id', 'button'); 
        btn.setAttribute('type', 'menu-button');  
        btn.setAttribute('class', 'toolbarbutton-1'); 
        btn.setAttribute('image', 'http://www.facebook.com/favicon.ico');

        btn.addEventListener('command', function() {
        console.log("this=" + this.id); 
                    // your callback code here
                }
        , false);

        var menupopup = document.createElement('menupopup');
        menupopup.setAttribute('id', 'menupopup');
        menupopup.addEventListener('command', function(event) {
            // TODO your callback
          }
        , false);

        //menu items
        var menuitem1 = document.createElement('menuitem');
        menuitem1.setAttribute('id', 'menuitem1');
        menuitem1.setAttribute('label', 'Menu item1');
        menuitem1.setAttribute('class', 'menuitem-iconic');
        menuitem1.addEventListener('command', function(event) {
                // CODE
            }
        , false);
 }
  winUtils.WindowTracker(delegate);
于 2012-10-20T04:06:56.610 回答
4

此代码需要多几行才能实际工作:

var delegate = {
onTrack: function(window) {

    if(window.location != "chrome://browser/content/browser.xul") {
        // console.log("=> win location false");
        return;
    }

    var document = window.document;
    var navBar = document.getElementById('nav-bar');

    var btn = document.createElement('toolbarbutton');
    btn.setAttribute('id', 'button');
    btn.setAttribute('type', 'menu-button');
    btn.setAttribute('class', 'toolbarbutton-1');
    btn.setAttribute('image', 'http://www.facebook.com/favicon.ico');

    btn.addEventListener('command', function() {
            console.log("this=" + this.id);
            // your callback code here
        }
        , false);

    var menupopup = document.createElement('menupopup');
    menupopup.setAttribute('id', 'menupopup');
    menupopup.addEventListener('command', function(event) {
            // TODO your callback
        }
        , false);

    //menu items
    var menuitem1 = document.createElement('menuitem');
    menuitem1.setAttribute('id', 'menuitem1');
    menuitem1.setAttribute('label', 'Menu item1');
    menuitem1.setAttribute('class', 'menuitem-iconic');
    menuitem1.addEventListener('command', function(event) {
            // CODE
        }
        , false);

    menupopup.appendChild(menuitem1);
    btn.appendChild(menupopup);
    navBar.appendChild(btn);

    console.log("window tracked");

    }
};

//let utils = require('api-utils/window-utils');
let utils = require('sdk/deprecated/window-utils'); // for new style sdk
utils.WindowTracker(delegate);
于 2015-07-14T10:00:08.863 回答