【问题标题】:How to show the same context menu item under two conditions如何在两种情况下显示相同的上下文菜单项
【发布时间】:2012-07-24 21:13:11
【问题描述】:

如果用户右键单击选择他们右键单击图像,我想在上下文菜单中显示菜单项。

目前我正在使用此代码,但不幸的是,如果我右键单击作为用户选择的一部分的图像,它会显示菜单项 两次

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) }
});

如果你知道,请告诉我目前的方法是什么 - 我花了很多时间在这上面。

谢谢。

【问题讨论】:

    标签: javascript firefox-addon contextmenu firefox-addon-sdk


    【解决方案1】:

    我不确定是否有更简单的方法,但您可以使用 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  '+
                     '  } '+                 
                     '});'
    });
    

    【讨论】:

      【解决方案2】:

      我就是这样做的:

      self.on("context", function (node, data) {
              return ((node.nodeName == "IMG") || 
                      (getSelection().length) );
      });
      

      其中getSelection() 是一个返回当前选择的函数。

      感谢您的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-10
        • 2014-04-01
        • 1970-01-01
        • 2021-07-22
        • 1970-01-01
        • 1970-01-01
        • 2021-04-18
        • 1970-01-01
        相关资源
        最近更新 更多