如何将下拉菜单添加到 Firefox 插件 sdk 驱动的插件工具栏按钮? [英] How to add a dropdown menu to a firefox addon sdk powered addon toolbar button?
本文介绍了如何将下拉菜单添加到 Firefox 插件 sdk 驱动的插件工具栏按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 https://github.com/erikvold/toolbarbutton-jplib/包以将工具栏按钮从 addon-sdk 插件添加到 firefox 导航栏.是否可以向此按钮添加下拉菜单(据我所知没有简单的方法)?
I use the https://github.com/erikvold/toolbarbutton-jplib/ package to add a toolbar button from an addon-sdk addon to the firefox navigation bar. Is it possible to add a drop down menu to this button (as I understand there are no easy ways)?
推荐答案
这段代码需要多几行才能真正起作用:
This code requires a few more lines to actually work:
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);
这篇关于如何将下拉菜单添加到 Firefox 插件 sdk 驱动的插件工具栏按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文