0

ユーザーが選択範囲を右クリックするか、画像を右クリックすると、コンテキスト メニューにメニュー項目を表示したいと思います。

現在、このコードを使用していますが、残念ながら、ユーザーが選択した画像の一部を右クリックすると、メニュー項目が2 回表示されます。

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

あなたが知っているなら、これを行う現在の方法を教えてください - 私はこれに多くの時間を費やしています.

ありがとう。

4

2 に答える 2

2

より簡単な方法があるかどうかはわかりませんが、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  '+
                 '  } '+                 
                 '});'
});
于 2012-07-25T02:14:51.427 に答える
0

私はこれに行きました:

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

ここgetSelection()で、現在の選択を返す関数です。

ご協力いただきありがとうございます。

于 2012-07-25T10:09:07.433 に答える