简体   繁体   English

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

[英]Chrome Extension - Dynamic Right-Click Menu

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 "Display It". 如果用户选择了一些文本,然后右键单击,该选项将显示“显示它”。 If the user right-clicks without selecting some text, the option will say "Select Some Text First" and be grayed out. 如果用户右键单击而未选择某些文本,则该选项将显示“首先选择一些文本”并显示为灰色。 I am wondering how do I achieve this? 我想知道如何实现这一目标?

I currently have it so that the option will appear only when the user has selected some text. 我目前拥有它,只有当用户选择了一些文本时才会出现该选项。 I am unsure how to modify it to meet my second requirements. 我不确定如何修改它以满足我的第二个要求。

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

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. 你不能把一个项目弄清楚...... Chrome已经付出了一些努力,只有当它的相关时才会出现上下文菜单项,这就是为什么我猜这里没有灰色选项。 Your way goes against what Chrome have tried to implement and I think you really should rethink the way you go about this. 你的方式与Chrome试图实施的方式相悖,我认为你真的应该重新考虑你的方式。
Saying that, you can use the chrome.contextMenus.update to change a menu item. 这样说,您可以使用chrome.contextMenus.update来更改菜单项。
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);

I was looking to accomplish the same thing as the original post, and was able to get it working using some message passing. 我希望完成与原始帖子相同的操作,并且能够使用一些消息传递使其工作。 Regardless of whether it's bad practice or not, I used the enabled(true/false) contextMenu property to leave my context menu option present, but grayed out. 无论是否是不好的做法,我都使用了enabled(true / false)contextMenu属性来保留我的上下文菜单选项,但是显示为灰色。

I created a context menu. 我创建了一个上下文菜单。 The important property is the id. 重要的财产是身份证。 The rest is mostly arbitrary because it will be changed dynamically. 剩下的几乎是任意的,因为它会动态改变。

In content.js 在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);

In background.js: 在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"]
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM