如何通过 Javascript 创建 XUL 工具栏?

How to Create XUL Toolbar via Javascript?

随着多进程 Firefox 的到来,我决定使用 Addon-SDK 重写我的插件。

我的插件主要是一个带有大量菜单的工具栏。

addonsdk 不提供任何构建菜单的方法。 So I found this method 通过它我可以将它们添加到现有的工具栏。但是,我找不到任何方法来创建这样的菜单并将它们添加到 Addon-SDK 工具栏。所以我想我会像创建菜单一样创建工具栏。

所以,我基本上是在尝试通过 javascript 创建一个 XUL 工具栏(我认为):

var MyDelegate = {
    onTrack: function(window){
        //don't add this to other windows, just the browser window
        if(window.location != "chrome://browser/content/browser.xul") {
            // console.log("=> win location false");
            return;
        }
        var document = window.document; //we need this to attach the XUL elements

        var MyBar = window.document.createElement('toolbar');
        MyBar.setAttribute('id', 'MyToolbarID');
        MyBar.setAttribute('toolbarname', 'MyTitle');
        MyBar.setAttribute('class', 'chromeclass-toolbar');
        MyBar.setAttribute('mode', 'full');     
        MyBar.setAttribute('hidden', 'false');      
        MyBar.setAttribute('insertafter', 'PersonalToolbar');   
    }
}
let utils = require('sdk/deprecated/window-utils'); // for new style sdk
utils.WindowTracker(spatDelegate);

我需要做什么才能真正构建此工具栏并在浏览器中显示?

[更新]

我不使用 SDK 工具栏的原因是工具栏是异步创建的,并且不及时存在以获取其 html id 的句柄。即使我使用浏览器工具箱获取 html id,它也不会添加到 window.

您需要将MyBar添加到工具箱:

window.document.getElementById("navigator-toolbox").appendChild(MyBar);