Google chrome Chrome扩展:如何使用自定义名称而不是插件名称创建上下文菜单

Google chrome Chrome扩展:如何使用自定义名称而不是插件名称创建上下文菜单,google-chrome,google-chrome-extension,contextmenu,Google Chrome,Google Chrome Extension,Contextmenu,我正在尝试创建一个基本的chrome扩展。在发现该插件后,我希望至少能够为父级命名,而不是我的插件名: chrome.contextMenus.create({ title: "Child Item 1", contexts:["selection"], }); chrome.contextMenus.create({ title: "Child Item 2", contexts:["selection"], }); 那么我该怎么做呢?我这样做的方法是创建一

我正在尝试创建一个基本的chrome扩展。在发现该插件后,我希望至少能够为父级命名,而不是我的插件名:

chrome.contextMenus.create({
    title: "Child Item 1",
    contexts:["selection"],
});
chrome.contextMenus.create({
    title: "Child Item 2",
    contexts:["selection"],
});


那么我该怎么做呢?

我这样做的方法是创建一个带有自定义
标题和
id的父项,然后将我需要的所有项作为子项添加到父项(到我的
script.js
文件):

结果如下:


虽然这很明显是可行的,但在创建子对象之前,等待父对象的
chrome.contextMenus.create()
回调可能会更加健壮。@Makyen也许是一个很好的观点。我当时可能没有意识到
create
方法是异步的。请随意添加考虑到这一点的您自己的答案。
chrome.contextMenus.create({
    title: "Custom Parent Name",
    contexts:["selection"],
    id: "parent",
});
chrome.contextMenus.create({
    title: "Child Item 1",
    contexts:["selection"],
    parentId: "parent",
    id: "child1",
});
chrome.contextMenus.create({
    title: "Child Item 2",
    contexts:["selection"],
    parentId: "parent",
    id: "child2",
});