Javascript 如何在两种情况下显示相同的上下文菜单项
如果用户右键单击所选内容或他们右键单击图像,我希望在上下文菜单中显示一个菜单项 目前我正在使用此代码,但不幸的是,如果我右键单击用户选择的图像,它会显示两次菜单项Javascript 如何在两种情况下显示相同的上下文菜单项,javascript,firefox-addon,contextmenu,firefox-addon-sdk,Javascript,Firefox Addon,Contextmenu,Firefox Addon Sdk,如果用户右键单击所选内容或他们右键单击图像,我希望在上下文菜单中显示一个菜单项 目前我正在使用此代码,但不幸的是,如果我右键单击用户选择的图像,它会显示两次菜单项 contextMenu.Item({ label: contextMenuItemLabel, contentScriptFile: contextMenuItemContentScriptFiles, context: contextMenu.SelectionContext(),
contextMenu.Item({
label: contextMenuItemLabel,
contentScriptFile: contextMenuItemContentScriptFiles,
context: contextMenu.SelectionContext(),
onMessage: function (posted) { someFunction(posted) }
});
contextMenu.Item({
label: contextMenuItemLabel,
contentScriptFile: contextMenuItemContentScriptFiles,
context: contextMenu.SelectorContext("img"),
onMessage: function (posted) { someFunction(posted) }
});
如果你知道的话,你能告诉我目前做这件事的方法吗?我在这件事上花了很多时间
谢谢。我不确定是否有更简单的方法,但您可以使用contentScript来确定您是否在图像元素/节点上和/或用户是否选择了文本
var cm = require("context-menu");
cm.Item({
label: "dfhdfg",
contentScript: 'self.on("context", function (node) {' +
' if(node.nodeName==="IMG"){'+
' //do something to web page or post a message back to addon '+
' } '+
' else if(window.getSelection().toString()!===""){'+
' //do something to web page or post a message back to addon '+
' } '+
'});'
});
我是这样想的:
self.on("context", function (node, data) {
return ((node.nodeName == "IMG") ||
(getSelection().length) );
});
其中getSelection()
是返回当前选择的函数
谢谢你的帮助