Chrome 创建扩展警报 'undefined'
Chrome Extension Alarms Create 'undefined'
我正在为 google chrome 扩展创建一个插件,但我终究无法解决这个错误。
Uncaught TypeError: Cannot read property 'create' of undefined(anonymous function)
奇怪的是闹钟没问题!我的清单具有适当的权限:
"permissions": ["activeTab","storage","alarms"]
我在 background.js 中对应错误的代码如下所示:
var alarmName = "test";
chrome.alarms.create(alarmName, {
delayInMinutes: 0,
periodInMinutes: 1
});
chrome.alarms.onAlarm.addListener(function(alarmName) {
alert("Here");
});
知道我为什么会遇到这个问题吗?谢谢
编辑:这是我的全部清单。
{
"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://*/*"]
}],
首先,提个建议。
永远不要调用您的内容脚本background.js
这是一个巨大的混淆来源,因为它与后台脚本不同。
这也是它不起作用的原因:content scripts don't have access to most Chrome APIs:
However, content scripts have some limitations. They cannot:
Use chrome.* APIs, with the exception of:
- extension ( getURL , inIncognitoContext , lastError , onRequest , sendRequest )
- i18n
- runtime ( connect , getManifest , getURL , id , onConnect , onMessage , sendMessage )
- storage
您需要发送消息给您的(真实的)后台脚本来为您执行此操作。 不要重复使用那样的代码,因为它是一个完全不同的上下文。
警报“工作正常”,因为您在后台页面中也有此代码,它可以正常工作。
您可以通过打开 chrome://extensions
并单击“背景页面”link 来观察它。您似乎正在查看当前页面的控制台并看到内容脚本的输出。
花点时间阅读 Architecture Overview。
我正在为 google chrome 扩展创建一个插件,但我终究无法解决这个错误。
Uncaught TypeError: Cannot read property 'create' of undefined(anonymous function)
奇怪的是闹钟没问题!我的清单具有适当的权限:
"permissions": ["activeTab","storage","alarms"]
我在 background.js 中对应错误的代码如下所示:
var alarmName = "test";
chrome.alarms.create(alarmName, {
delayInMinutes: 0,
periodInMinutes: 1
});
chrome.alarms.onAlarm.addListener(function(alarmName) {
alert("Here");
});
知道我为什么会遇到这个问题吗?谢谢
编辑:这是我的全部清单。
{
"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://*/*"]
}],
首先,提个建议。
永远不要调用您的内容脚本background.js
这是一个巨大的混淆来源,因为它与后台脚本不同。
这也是它不起作用的原因:content scripts don't have access to most Chrome APIs:
However, content scripts have some limitations. They cannot:
Use chrome.* APIs, with the exception of:
- extension ( getURL , inIncognitoContext , lastError , onRequest , sendRequest )
- i18n
- runtime ( connect , getManifest , getURL , id , onConnect , onMessage , sendMessage )
- storage
您需要发送消息给您的(真实的)后台脚本来为您执行此操作。 不要重复使用那样的代码,因为它是一个完全不同的上下文。
警报“工作正常”,因为您在后台页面中也有此代码,它可以正常工作。
您可以通过打开 chrome://extensions
并单击“背景页面”link 来观察它。您似乎正在查看当前页面的控制台并看到内容脚本的输出。
花点时间阅读 Architecture Overview。