xul或sdk解决方案,用于将项目添加到firefox中的[tools]菜单

时间:2014-03-18 19:53:44

标签: firefox-addon firefox-addon-sdk

好的,所以我创建了一个菜单项,

    var menuitem = require("menuitems").Menuitem({
    id: "menu_item_id",
    menuid: "menu_ToolsPopup",
    label: "menu_item",
    onCommand: function() {
    console.log("clicked menu item");
 },

});

现在我想在上面添加一个sub_menu_item,

这似乎无法正常工作

var menuitem = require("menuitems").Menuitem({
id: "sub_menu_item_id",
menuid: "menu_item_id", <= parent element id
label: "sub_menu_item",
onCommand: function() {
console.log("clicked menu item");
},

});

有关如何添加子菜单项的任何想法?

2 个答案:

答案 0 :(得分:1)

将其粘贴到您的main.js中,当您右键单击所有窗口中带有标签的第一个标签元素(在标签栏中)时,应该添加上下文菜单。只是一个测试,向您展示它如何按您的要求工作。此代码未经测试。

var {Cc, Cu, Ci} = require('chrome');
Cu.import('resource://gre/modules/Services.jsm');

    var myMenuJson =
['xul:menupopup', {id: 'myMenu1'},
['xul:menuitem', {label:'menu item1'}],
['xul:menu', {label:'menu item2 is submenu1'},
['xul:menupopup', {},
['xul:menuitem', {label:'submenu1 item1'}],
['xul:menuitem', {label:'submenu1 item2'}],
['xul:menuitem', {label:'submenu1 item3'}]
]
],
['xul:menuitem', {label:'menu item3 is before a seperator'}],
['xul:menuseparator', {}],
['xul:menuitem', {label:'menu item4 is after a seperator'}]
];



    let DOMWindows = Services.wm.getEnumerator(null);
while (DOMWindows.hasMoreElements()) {
let aDOMWindow = DOMWindows.getNext();
if (aDOMWindow.gBrowser && aDOMWindow.gBrowser.tabContainer) {
var doc = aDOMWindow.document;
aDOMWindow.gBrowser.tabContainer.childNodes[0].setAttribute('context', 'myMenu1');
var mainPopupSet = doc.getElementById('mainPopupSet');
mainPopupSet.appendChild(jsonToDOM(myMenuJson, doc, {}));
}
}







/*dom insertion library function from MDN - https://developer.mozilla.org/en-US/docs/XUL_School/DOM_Building_and_HTML_Insertion*/
jsonToDOM.namespaces = {
html: 'http://www.w3.org/1999/xhtml',
xul: 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'
};
jsonToDOM.defaultNamespace = jsonToDOM.namespaces.html;
function jsonToDOM(xml, doc, nodes) {
function namespace(name) {
var m = /^(?:(.*):)?(.*)$/.exec(name);
return [jsonToDOM.namespaces[m[1]], m[2]];
}

function tag(name, attr) {
if (Array.isArray(name)) {
var frag = doc.createDocumentFragment();
Array.forEach(arguments, function (arg) {
if (!Array.isArray(arg[0]))
frag.appendChild(tag.apply(null, arg));
else
arg.forEach(function (arg) {
frag.appendChild(tag.apply(null, arg));
});
});
return frag;
}

var args = Array.slice(arguments, 2);
var vals = namespace(name);
var elem = doc.createElementNS(vals[0] || jsonToDOM.defaultNamespace, vals[1]);

for (var key in attr) {
var val = attr[key];
if (nodes && key == 'key')
nodes[val] = elem;

vals = namespace(key);
if (typeof val == 'function')
elem.addEventListener(key.replace(/^on/, ''), val, false);
else
elem.setAttributeNS(vals[0] || '', vals[1], val);
}
args.forEach(function(e) {
try {
elem.appendChild(
Object.prototype.toString.call(e) == '[object Array]'
?
tag.apply(null, e)
:
e instanceof doc.defaultView.Node
?
e
:
doc.createTextNode(e)
);
} catch (ex) {
elem.appendChild(doc.createTextNode(ex));
}
});
return elem;
}
return tag.apply(null, xml);
}
/*end - dom insertion library function from MDN*/

答案 1 :(得分:0)

前几天我帮助过这个人。但它依赖于非sdk解决方案。我也看到你没有将菜单附加到小部件,只是添加到上下文菜单中。但这只是我的意思: https://gist.github.com/Noitidart/9523055

function doit() {
    //create and add the panel now
    var doc = document;
    var myWidget = doc.getElementById('widget:jid1-lwNbwJJiiMXM4A@jetpack-open-traveleye'); //we add the menu to this widget, we can add menu to anything by setting the context attribute of it to the id of the menu we want to give it

    var myMenuJson = 
                    ['xul:menupopup', {id: 'myMenu1'},
                        ['xul:menuitem', {label:'menu item1'}],
                        ['xul:menu', {label:'menu item2 is submenu1'},
                            ['xul:menupopup', {},
                                ['xul:menuitem', {label:'submenu1 item1'}],
                                ['xul:menuitem', {label:'submenu1 item2'}],
                                ['xul:menuitem', {label:'submenu1 item3'}]
                            ]
                        ],
                        ['xul:menuitem', {label:'menu item3 is before a seperator'}],
                        ['xul:menuseparator', {}],
                        ['xul:menuitem', {label:'menu item4 is after a seperator'}]
                    ];

    var mainPopupSet = doc.getElementById('mainPopupSet');
    mainPopupSet.appendChild(jsonToDOM(myMenuJson, doc, {}));

    myWidget.setAttribute('context', 'myMenu1');
}