主要使用 contextMenus 右键菜单功能,正常用它给页面设置右键菜单,其实它也可以给插件还有其它功能设置右键菜单,是根据 contexts 选项来设置给哪个功能设置右键菜单。
首先添加权限
"permissions": ["contextMenus"],
background.js 添加
chrome.contextMenus.create({id: '右键菜单',title: `右键菜单`,
});
重新加载插件, 可以看到页面右键时已经有的 右键菜单选项
官方关于 ContextType 的解释
The different contexts a menu can appear in. Specifying 'all' is equivalent to the combination of all other contexts except for 'launcher'. The 'launcher' context is only supported by apps and is used to add menu items to the context menu that appears when clicking the app icon in the launcher/taskbar/dock/etc. Different platforms might put limitations on what is actually supported in a launcher context menu.
取值范围
"all"
"page"
"frame"
"selection"
"link"
"editable"
"image"
"video"
"audio"
"launcher"
"browser_action"
"page_action"
"action"
取值为 action 时就是插件的右键菜单选项
chrome.contextMenus.create({id: '右键菜单',title: `右键菜单`,contexts: ['action'],
});