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

标签 javascript firefox-addon-sdk xul

随着多进程 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,它也不会添加到窗口中。

最佳答案

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

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

关于javascript - 如何通过 Javascript 创建 XUL 工具栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34487459/

相关文章:

javascript - jQuery hide() 针对 <p> 元素隐藏整个父 div 的背景

javascript - 自上次用户在 Firefox 中事件以来的时间

javascript - reactjs - 在不重新渲染组件的情况下为单个 DOM 元素设置动画

javascript - 如何使用 firefox 插件 sdk 创建一个选项弹出对话框?

javascript - 如何使用 "browser-action-jplib"监听 main.js 中的消息

firefox-addon - 在 Page-Mod 中使用远程内容脚本文件/Javascript

firefox - 如何在 options.xul 中使用 oncommand 来调用 bootstrap.js 中的函数?

c++ - NS_ERROR_XPC_GS_RETURNED_FAILURE 错误码

javascript - jQuery 对话框默认显示为隐藏吗?

php - 使用 PHP- 文本编辑器压缩和合并 JS 文件