我现在正在开发 Chrome 扩展程序。
我的问题是,当我调用 chrome.alarms.create()
时,出现以下错误:
Uncaught TypeError: Cannot read property 'create' of undefined
我的扩展包中有这些文件:
list .json
{
"manifest_version": 2,
"name": "Tool",
"version": "1.0",
"background": {
"scripts": ["background.js"]
},
"permissions": ["background", "tabs", "webNavigation", "alarms"]
}
myscript.js
chrome.alarms.create("aaa", {"when":Date.now()+5000});
chrome.alarms.onAlarm.addListener(function(alarm){
console.log("hello");
});
background.js
chrome.pageAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {file: "myscript.js"});
});
当我在 background.js
中调用 chrome.alarms.create()
时,它工作正常。
但是,当我调用 myscript.js
中的函数时,它会导致错误。
这是什么原因,我该如何解决这个问题?
最佳答案
您无法从内容脚本访问大多数 Chrome API。您需要使用 Messaging API 向后台页面发送消息,然后后台页面可以调用 Alarms API。
https://developer.chrome.com/extensions/messaging https://developer.chrome.com/extensions/content_scripts
关于javascript - 无法从内容脚本调用 chrome.alarms.create(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25590952/