Chrome扩展程序-动态右键菜单
我试图在右键单击菜单中创建一个根据用户操作动态变化的选项.如果用户选择了一些文本,然后单击鼠标右键,该选项将显示为显示".如果用户单击鼠标右键而不选择某些文本,则该选项将显示首先选择某些文本"并显示为灰色.我想知道如何实现这一目标?
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);}
);
}
});
您无法将某项显示为灰色... Chrome付出了一些努力,仅在相关菜单项出现时才显示它,这就是为什么我想没有灰显选项.您的方式与Chrome尝试实施的方式背道而驰,我认为您真的应该重新考虑您的实现方式.
话虽如此,您可以使用chrome.contextMenus.update更改菜单项.
下列代码与按自己的方式行事一样好(认真地重新思考这个想法)....
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);