【问题标题】:Chrome Extension - Dynamic Right-Click MenuChrome 扩展 - 动态右键菜单
【发布时间】:2022-02-14 22:12:35
【问题描述】:

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

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

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

【问题讨论】:

  • 禁用“选择某些文本”选项会造成混淆。为什么不选择文本 时只有“显示”选项?
  • 这是设计团队做出的决定。我只是团队的业余开发者:P
  • 我相信他们的逻辑是让用户知道他们需要选择一些文本才能使用我们的扩展程序。

标签: javascript google-chrome-extension


【解决方案1】:

您不能将某个项目变灰...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);

【讨论】:

  • 感谢您的建议。我会把这个提交给设计团队。
  • @Jon,您可以将默认文本设置为“选择整个页面”而不是“选择一些文本”。当他点击“选择整个页面”时,你选择了整个页面。
【解决方案2】:

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

我创建了一个上下文菜单。重要的属性是 id。其余的大部分都是任意的,因为它将动态更改。

在 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"]
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2017-04-22
    • 1970-01-01
    • 2012-12-02
    相关资源
    最近更新 更多