2012-07-24 83 views
0

我想在上下文菜單中顯示一個菜單項,如果用戶在選擇上右鍵單擊它們右鍵單擊圖像。如何在兩種情況下顯示相同的上下文菜單項

目前我正在使用此代碼,但不幸的是它顯示菜單項兩次如果我右鍵單擊作爲用戶選擇的一部分的圖像。

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

你能告訴我現在如何做到這一點,如果你知道它 - 我花了這麼多時間在這個。

謝謝。

回答

0

我去了這一點:

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

其中getSelection()是返回當前選擇的功能。

感謝您的幫助。

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 '+ 
       ' } '+     
       '});' 
}); 
相關問題