[英]Chrome Extension Alarms Create 'undefined'
I'm creating a plugin for google chrome extensions and for the life of me cannot figure out this error.我正在为 google chrome 扩展创建一个插件,我一生都无法弄清楚这个错误。
Uncaught TypeError: Cannot read property 'create' of undefined(anonymous function)
The strange thing is the alarm works fine!奇怪的是闹钟工作正常! My manifest has the proper permissions:
我的清单具有适当的权限:
"permissions": ["activeTab","storage","alarms"]
And my code in background.js which corresponds to the error looks like so:我在 background.js 中对应于错误的代码如下所示:
var alarmName = "test";
chrome.alarms.create(alarmName, {
delayInMinutes: 0,
periodInMinutes: 1
});
chrome.alarms.onAlarm.addListener(function(alarmName) {
alert("Here");
});
Any ideas why I'm getting this issue?任何想法为什么我会遇到这个问题? Thanks
谢谢
EDIT: Here's my whole manifest.编辑:这是我的整个清单。
{
"manifest_version": 2,
"name": "Test App",
"options_page": "options.html",
"description": "Amazing new test app!",
"version": "1.0",
"icons": { "16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png" },
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [ {
"js": [ "jquery.min.js", "jquery.js", "background.js" ],
"matches": [ "http://*/*", "https://*/*"]
}],
"background": {
"scripts": ["jquery.min.js", "jquery.js", "background.js"],
"persistent":true
},
"web_accessible_resources": ["src/options/options.html"],
"permissions": [
"activeTab","storage","alarms"]
}
"content_scripts": [ {
"js": [ "jquery.min.js", "jquery.js", "background.js" ],
"matches": [ "http://*/*", "https://*/*"]
}],
First, a piece of advice.首先,一个忠告。
This is a huge source of confusion, since it's not the same as background scripts.这是一个巨大的混乱来源,因为它与后台脚本不同。
And it's also the reason it does not work: content scripts don't have access to most Chrome APIs :这也是它不起作用的原因:内容脚本无法访问大多数 Chrome API :
However, content scripts have some limitations.
但是,内容脚本有一些限制。 They cannot:
他们不可以:
Use chrome.* APIs, with the exception of:
使用 chrome.* API,但以下情况除外:
- extension ( getURL , inIncognitoContext , lastError , onRequest , sendRequest )
扩展( getURL 、 inIncognitoContext 、 lastError 、 onRequest 、 sendRequest )
- i18n
国际化
- runtime ( connect , getManifest , getURL , id , onConnect , onMessage , sendMessage )
运行时(连接、getManifest、getURL、id、onConnect、onMessage、sendMessage)
- storage
贮存
You'll need to message your (real) background script to do this for you.您需要向您的(真实)后台脚本发送消息才能为您执行此操作。 Don't reuse code like that, since it's a completely different context.
不要重用这样的代码,因为它是一个完全不同的上下文。
The alarm "works fine" since you also have this code in the background page, where it works properly.警报“工作正常”,因为您在后台页面中也有此代码,它可以正常工作。
You can observe this by opening chrome://extensions
and clicking the "background page" link to inspect it.您可以通过打开
chrome://extensions
并单击“背景页面”链接进行检查来观察这一点。 You seem to be looking in the current page's console and seeing the content script's output.您似乎正在查看当前页面的控制台并查看内容脚本的输出。
Take a moment to read the Architecture Overview .花点时间阅读架构概述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.