Google Chrome 扩展:如何创建带有层次结构分支的右键单击扩展?
Google Chrome Extension: How to create a right click extension with hierarchy branches?
我希望能够向我的右键单击扩展添加多个选项。我目前有一个,它可以正常工作,但我复制了一个简单的教程,需要更多指导。感谢您的帮助。
// Set up context menu at install time.
chrome.runtime.onInstalled.addListener(function() {
var context = "selection";
var title = "Search on blah blah";
var id = chrome.contextMenus.create({"title": title, "contexts":[context],
"id": "context" + context});
});
// add click event
chrome.contextMenus.onClicked.addListener(onClickHandler);
// The onClicked callback function.
function onClickHandler(info, tab) {
var sText = info.selectionText;
var url = "https://www.google.com/search?q=" + encodeURIComponent(sText);
window.open(url, '_blank');
};
看看chrome.contextMenus.create,你可以通过设置parentId
创建一个子上下文菜单项(层级分支)
// Set up context menu at install time.
chrome.runtime.onInstalled.addListener(function() {
var context = "selection";
var title = "Search on blah blah";
var id = chrome.contextMenus.create({"title": title, "contexts":[context],
"id": "context" + context});
var child1_id = chrome.contextMenus.create({"title": "child1", "parentId": id, "contexts":[context],
"id": "child1_id"});
var child2_id = chrome.contextMenus.create({"title": "child2", "parentId": id, "contexts":[context],
"id": "child2_id"});
});
// add click event
chrome.contextMenus.onClicked.addListener(onClickHandler);
// The onClicked callback function.
function onClickHandler(info, tab) {
var sText = info.selectionText;
var url = "https://www.google.com/search?q=" + encodeURIComponent(sText);
window.open(url, '_blank');
};
我希望能够向我的右键单击扩展添加多个选项。我目前有一个,它可以正常工作,但我复制了一个简单的教程,需要更多指导。感谢您的帮助。
// Set up context menu at install time.
chrome.runtime.onInstalled.addListener(function() {
var context = "selection";
var title = "Search on blah blah";
var id = chrome.contextMenus.create({"title": title, "contexts":[context],
"id": "context" + context});
});
// add click event
chrome.contextMenus.onClicked.addListener(onClickHandler);
// The onClicked callback function.
function onClickHandler(info, tab) {
var sText = info.selectionText;
var url = "https://www.google.com/search?q=" + encodeURIComponent(sText);
window.open(url, '_blank');
};
看看chrome.contextMenus.create,你可以通过设置parentId
// Set up context menu at install time.
chrome.runtime.onInstalled.addListener(function() {
var context = "selection";
var title = "Search on blah blah";
var id = chrome.contextMenus.create({"title": title, "contexts":[context],
"id": "context" + context});
var child1_id = chrome.contextMenus.create({"title": "child1", "parentId": id, "contexts":[context],
"id": "child1_id"});
var child2_id = chrome.contextMenus.create({"title": "child2", "parentId": id, "contexts":[context],
"id": "child2_id"});
});
// add click event
chrome.contextMenus.onClicked.addListener(onClickHandler);
// The onClicked callback function.
function onClickHandler(info, tab) {
var sText = info.selectionText;
var url = "https://www.google.com/search?q=" + encodeURIComponent(sText);
window.open(url, '_blank');
};