问题 Chrome扩展程序 - 动态右键菜单


我试图在右键单击菜单中创建一个基于用户操作动态的选项。如果用户选择了一些文本,然后右键单击,该选项将显示“显示它”。如果用户右键单击而未选择某些文本,则该选项将显示“选择一些文本优先”并显示为灰色。我想知道如何实现这一目标?

我目前拥有它,只有当用户选择了一些文本时才会出现该选项。我不确定如何修改它以满足我的第二个要求。

chrome.contextMenus.create ({
    title:"Display It!", contexts:["selection"], onclick:function(info,tab) {
        chrome.tabs.sendRequest(
            tab.id,
            {callFunction: "displaySidebar", info: info}, 
            function(response) {console.log(response);}
        );
    }           
});

3412
2018-03-25 17:12


起源

禁用“选择一些文本”选项会让人感到困惑。为什么不在文本时只显示“显示”选项 是 选择? - Bojangles
这是设计团队做出的决定。我只是团队的业余开发者:P - Jon
我相信他们的逻辑是让用户知道他们需要选择一些文本才能使用我们的扩展。 - Jon


答案:


你不能把一个项目弄清楚...... Chrome已经付出了一些努力,只有当它的相关时才会出现上下文菜单项,这就是为什么我猜这里没有灰色选项。你的方式与Chrome试图实施的方式相悖,我认为你真的应该重新思考你的方式。
这样说,您可以使用chrome.contextMenus.update来更改菜单项。
以下代码与您的方式一样好(严肃地说,重新考虑这个想法)....

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

9
2018-03-25 19:24



感谢您的建议。我会把它带到设计团队。 - Jon
@Jon,您可以将默认文本设为“选择整页”而不是“选择一些文本”。当他点击“选择整页”时,选择整个页面。 - Pacerier


我希望完成与原始帖子相同的操作,并且能够使用一些消息传递使其工作。无论是否是不好的练习,我都使用enabled(true / false)contextMenu属性来保留我的上下文菜单选项,但是显示为灰色。

我创建了一个上下文菜单。重要的财产是身份证。剩下的几乎是任意的,因为它会动态改变。

在content.js中

//This event listener will determine if the context menu should be updated 
//based on if the right-button was clicked and if there is a selection or not
document.addEventListener("mousedown", function(event){
    if (event.button !== 2) {
        return false;
    }
    var selected = window.getSelection().toString();
        if(event.button == 2 && selected != '') {
                //get selected text and send request to bkgd page to create menu
            chrome.extension.sendMessage({
                   'message': 'updateContextMenu', 
                   'selection': true});
        } else {
        chrome.extension.sendMessage({
                   'message': 'updateContextMenu',
                   'selection': false});
        }
}, true);

在background.js中:

//add a message listener that will modify the context menu however you see fit
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.message == 'updateContextMenu') {
        if (request.selection) {
            chrome.contextMenus.update('contextMenuId',{
                'title': 'New Title', 
                'enabled': true, 
                "contexts": ["all"],
                'onclick': someFunction
            });
        } else {
            chrome.contextMenus.update('contextMenuId',{
                'title': 'Select some text first', 
                'enabled': false, 
                "contexts": ["all"]
            });
        }
    } else {
        sendResponse({});
    }
});

//The original context menu.  The important property is the id.  The rest is mostly 
//arbitrary because it will be changed dynamically by the listener above.
    chrome.contextMenus.create({
        'id': 'contextMenuId', 
        'enabled': false, 
        'title': 'Some Title', 
        "contexts": ["all"]
        });

6
2017-10-30 18:50