Chrome Extension - Dynamic Right-Click Menu

前端 未结 2 1106
别跟我提以往
别跟我提以往 2021-01-30 15:37

I am trying to create an option in the right-click menu that is dynamic based on the user\'s action. If the user selects some text, then right-clicks, the option will say \"Disp

2条回答
  •  自闭症患者
    2021-01-30 15:38

    You cant grey an item out...Chrome has gone to a bit of effort to only make context menu items appear when its relevant which is why i guess theres no grey out option. Your way goes against what Chrome have tried to implement and I think you really should rethink the way you go about this.
    Saying that, you can use the chrome.contextMenus.update to change a menu item.
    The following code is about as good as your going to get it your way (seriously, rethink this idea)....

    function selectedTrueOnClick(info, tab) {
        chrome.tabs.sendRequest(
        tab.id, {
            callFunction: "displaySidebar",
            info: info
        }, function(response) {
            console.log(response);
        });
    }
    
    function selectedFalseOnClick(info, tab) {
        //
    }
    
    var contextMenuID = chrome.contextMenus.create({
        title: "Select some text",
        contexts: ["all"],
        onclick: selectedFalseOnClick
    });
    
    function contextMenuUpdate(selected) {
        if (selected) chrome.contextMenus.update(contextMenuID, {
            title: 'You selected "%s"',
            contexts: ["all"],
            onclick: selectedTrueOnClick
        });
        else chrome.contextMenus.update(contextMenuID, {
            title: "Select some text",
            contexts: ["all"],
            onclick: selectedTrueOnClick
        });
    }
    
    contextMenuUpdate(false);
    

提交回复
热议问题