根据所选文本的内容显示上下文菜单项的子集(firefox addon)

时间:2014-11-15 18:51:02

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

我有一个带有selectionContext的firefox上下文菜单和该菜单上的三个项目。在菜单中,我有一个内容脚本,用于监听onclick事件,它获取被点击的项目和所选文本。一切正常。

Menu
    item1
    item2
    item3

我试图仅根据所选文本的内容显示菜单项的子集,因为并非所有菜单项都适用于所有情况。

除了上述内容之外,我还尝试向每个侦听selectionContext的项目添加on context和另一个内容脚本。使用此方法,我可以获取所选文本,但项目永远不会显示在菜单中,因为它会被监听oncontext事件的项目内容脚本取消。然后,菜单的onclick内容脚本永远不会应用于该项目。

我知道我可以使用menu.addItem()menu.removeItem()添加和删除项目。如果我使用onclick内容脚本,则在点击它们之前不会更新项目,但是为时已晚,但我必须保留onclick,以便用户可以单击这些项目。如果我向项目或菜单添​​加oncontext内容脚本,则项目或菜单不会显示,因为oncontext击败了onclick

如果有人能告诉我如何根据文本内容显示可以使用点击内容脚本的菜单项子集,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

除了predicateContext之外,我还使用selectionContext对其进行了排序。

//context attribute of the Menu
context: [contextMenu.PredicateContext(checkText),contextmenu.selectionContext()]




function checkText(data) {

       if(data.selectionText === null)
           return false;

       console.log('selectionText: ' + data.selectionText);

       //handle showing or hiding of menu items based on the text content.
       menuItemToggle(data.selectionText);

       return true;
};

function menuItemToggle(text){
    // do stuff here

};