如果用户右键单击选择或右键单击图像,我希望在上下文菜单中显示一个菜单项。
目前我正在使用此代码,但不幸的是,如果我右键单击用户选择的图像,它会显示两次菜单项。
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) );
});
其中CCD_ 1是返回当前选择的函数。
谢谢你的帮助。