没有现有的 API 方法来检索有关给定菜单项的信息,因为这些信息很少需要,并且已经可供扩展程序的开发人员使用。
这是实现结果的通用函数:
/**
* Creates a menu item using chrome.contextMenus.create.
* When the second argument is specified, the click handler receives a
* third argument: The original creation data.
* When the "onclick" property is set in the creationData, the "onclick"
* event does not receive a third parameter.
*
* @param object creationObject Basic creation object
* @param function onclickHandler "click" property of the creationObject
*/
function createMenuItem(creationObject, onclickHandler) {
if (onclickHandler) {
creationObject.onclick = function(onClickData, tab) {
onclickHandler(onClickData, tab, creationObject);
};
}
return chrome.contextMenus.create(creationObject);
}
// Usage:
createMenuItem({"title": "sometitle", "contexts":["selection"]}, searchSelection);
function searchSelection(info, tab, creationData) {
var query = "<i want title " + creationData.title + " here>" + info.selectionText;
var url = "http://www.google.com/search?q=" + query;
chrome.tabs.create({url: url});
}
不,没有办法添加内联可编辑的菜单项(文档chrome.contextMenus.create
中提到了唯一可用的选项)。