如果用户右键单击某个选项或他们右键单击图像,我希望在上下文菜单中显示一个菜单项。
目前我正在使用此代码,但不幸的是,如果我右键单击用户选择的图像,它会显示菜单项两次。
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 '+
' } '+
'});'
});
关于javascript - 如何在两种情况下显示相同的上下文菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11639387/