hooray
hooray

Reputation: 95

Show a subset of context-menu items based on the content of the selected text (firefox addon)

I have a firefox context menu with a selectionContext and three items on that menu. In the menu I have a content script which listens for an onclick event and it gets the item that was clicked and the selected text. That all works fine.

Menu
    item1
    item2
    item3

I am trying to only show a subset of the menu items based on the content of the selected text as not all of them will apply in all cases.

In addition to the above I have tried to add a selectionContext and another content script to each item which listens for on context. Using this method I can get the selected text but the item never shows in the menu because it is cancelled out by the item's content script which listens for an oncontext event. The Menu's onclick content script is then never applied to the item.

I know I can add and remove Items with menu.addItem() and menu.removeItem(). If I use the onclick content script the items will not be updated until after they are clicked by which point it is too late but I must keep the onclick so the user can click the items. If I add an oncontext content script to the items or the menu then the items or menu do not show because the oncontext beats the onclick.

I would be grateful if anyone could advise me on how to show a subset of menu items that can use an on click content script based on the content of the text.

Upvotes: 1

Views: 201

Answers (1)

hooray
hooray

Reputation: 95

I sorted it by using a predicateContext in addition to the 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

};

Upvotes: 3

Related Questions