在非持久性背景脚本上添加上下文菜单项? [英] Adding context menu item on a non-persistent background script?
问题描述
我使用非持久性后台脚本添加上下文菜单项:
chrome.contextMenus.create({
title:'Get Code',
id:'myUniqueIdForThisExtension123',
contexts:['all'],
onclick:onClickHandler
});
函数onClickHandler(){}
documentation 简单地说:
分配给此项目的唯一ID。活动页面必须提供。不能与此扩展程序的另一个ID相同。
所以我添加了一个唯一的ID,但仍然无法使其工作。
你说你有一个非持续性的背景页面,所以你应该有一个如下所示的 manifest.json
文件:
{
manifest_version:2,
name:我的扩展名,
description:我的描述,
version:1 ,
$ b $permissions:[contextMenus],
background:{
persistent:false,
scripts:[
/background.js
]
}
}
现在,由于您拥有非持久性背景页面,因此当您需要使用上下文菜单时,您必须使用正确的侦听器唤醒。
从 官方文档 引用:
使用活动页面的最佳做法:
[...]
- 如果您使用上下文菜单API,请将字符串ID参数传递给
contextMenus.create
,然后使用contextMenus.onClicked
回调onclick
参数添加到contextMenus.create
。
因此,您的错误是使用 onclick
参数而不是 contextMenus.onClicked.addListener()
方法放入 background.js
中。
解决方案
我已经说过您应该使用 onClicked
事件,但我想补充一点,请参阅 x a的回答:
您应该通过
contextMenus.create
在runtime.onInstalled
,因为这些上下文菜单注册仍然存在。
所以,简单地说,一旦创建,上下文菜单将保留在您的扩展中,最佳做法是只定义一次:扩展安装(或更新),并在每次加载后台页面时添加侦听器。
chrome.runtime.onInstalled.addListener(function(){
chrome.contextMenus.create({
title: '我的菜单',
id:'menu1',//你将在处理函数中使用它来标识这个上下文菜单项
contexts:['all'],
}) ;
});
chrome.contextMenus.onClicked.addListener(function(info,tab){
if(info.menuItemId ===menu1){//这里是你需要ID的地方
//做某事
}
});
您需要在事件页面中创建上下文菜单,如 文档页面 chrome.contextMenus API。
I am adding a context menu item from a non-persistent background script using:
chrome.contextMenus.create({
title: 'Get Code',
id: 'myUniqueIdForThisExtension123',
contexts: ['all'],
onclick: onClickHandler
});
function onClickHandler() {}
The documentation simply states:
The unique ID to assign to this item. Mandatory for event pages. Cannot be the same as another ID for this extension.
So I added a unique ID, but I still cannot make it work. Nothing new inserted on the context menu.
You are saying you've got a non-persistent background page, so you should have a manifest.json
file that looks like this:
{
"manifest_version": 2,
"name": "My extension",
"description": "My description",
"version": "1",
"permissions": ["contextMenus"],
"background": {
"persistent": false,
"scripts": [
"/background.js"
]
}
}
Now, since that you've got a non-persistent background page, you have to use the right listeners to "wake it up" when you need to use the context menu.
Quoting from the official documentation:
Best practices when using event pages:
[...]
- If you're using the context menus API, pass a string id parameter to
contextMenus.create
, and use thecontextMenus.onClicked
callback instead of anonclick
parameter tocontextMenus.create
.
So, simply, your error is using the onclick
parameter instead of the contextMenus.onClicked.addListener()
method in your background.js
.
Solution
I already said that you should use the onClicked
event, but I would like to add that, referring to x a's answer:
You should register a context menu via
contextMenus.create
in the event handler ofruntime.onInstalled
, as these context menu registrations persist anyway.
So simply, given that, once created, the context menus persist in your extension, it's best practice to only define them a single time: when your extension is installed (or updated), and add the listener each time the background page is loaded.
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({
title: 'My menu',
id: 'menu1', // you'll use this in the handler function to identify this context menu item
contexts: ['all'],
});
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId === "menu1") { // here's where you'll need the ID
// do something
}
});
That's what you need to create a context menu in an event page, as shown in the documentation page of the chrome.contextMenus
API.
这篇关于在非持久性背景脚本上添加上下文菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!