我正在嘗試在基於用戶操作的動態右鍵菜單中創建一個選項。如果用戶選擇了一些文本,然後右鍵單擊,該選項會顯示「顯示它」。如果用戶沒有選擇一些文本而右鍵單擊,則該選項將會顯示「首先選擇一些文本」並呈灰色顯示。我想知道我該如何實現這一目標?Chrome擴展程序 - 動態右鍵菜單
我目前擁有它,因此只有當用戶選擇了一些文本時纔會顯示該選項。我不確定如何修改它以符合我的第二個要求。
我正在嘗試在基於用戶操作的動態右鍵菜單中創建一個選項。如果用戶選擇了一些文本,然後右鍵單擊,該選項會顯示「顯示它」。如果用戶沒有選擇一些文本而右鍵單擊,則該選項將會顯示「首先選擇一些文本」並呈灰色顯示。我想知道我該如何實現這一目標?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);
我一直在尋找來完成同樣的事情,原來的職位,並且是能夠使用某些消息傳遞來使其工作。無論是否是不好的做法,我都使用啓用(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"]
});
有一個禁用 「選擇一些文本」 選項是混亂。爲什麼不選擇文本_is_時只有「顯示它」選項? – Bojangles 2012-03-25 17:22:27
這是設計團隊做出的決定。我只是團隊的業餘開發人員:P – Jon 2012-03-25 17:24:24
我相信他們的邏輯是讓用戶知道他們需要選擇一些文本才能使用我們的擴展。 – Jon 2012-03-25 17:24:53