【发布时间】:2018-09-18 15:26:55
【问题描述】:
我是 Chrome 扩展程序开发的新手。我目前正在寻找一个 Chrome 扩展来关闭通知。我希望通过快捷键激活一次扩展。
在查看下面的代码之前,我想让大家知道alert 确实出现了......但是 Chrome 扩展页面显示了错误:
“commands.onCommand 的事件处理程序出错:TypeError: 无法读取未定义的属性 'getAll'”
上线:
chrome.notifications.getAll((items) => {
chrome.notifications 对象不知何故未定义,因此 Chrome 似乎认为当前没有显示通知……这很奇怪,因为确实存在,如图所示。
有人能帮忙解释一下这种情况吗?
manifest.json:
{
"name": "ClearAll",
"version": "1.0",
"description": "Clear notifications!",
"background": {
"scripts": ["background.js"],
"persistent": false
},
"commands": {
"clear": {
"suggested_key":{
"default": "Alt+Shift+S"
},
"description": "Executes clear"
}
},
"manifest_version": 2
}
background.js:
chrome.commands.onCommand.addListener(function(command) {
if (command == 'clear') {
alert("testing");
chrome.notifications.getAll((items) => {
if (items)
for (let key in items)
chrome.notifications.clear(key);
});
}
});
错误:
【问题讨论】:
标签: javascript google-chrome google-chrome-extension notifications shortcut