【问题标题】:Creating Chrome Extension to Dismiss Notifications via Keyboard Shortcut创建 Chrome 扩展程序以通过键盘快捷键关闭通知
【发布时间】: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


    【解决方案1】:

    您可能已经想通了,但是对于将来偶然发现这个问题的任何人:不可能编写一个扩展程序来消除 Chrome 上的所有通知,因为任何人都不可能扩展以访问用户的浏览器范围的通知; manifest.json 中的 notifications 权限允许您创建和清除由 your 扩展程序创建的通知。事实上,如果允许任何扩展程序这样做,那将是侵犯隐私!

    来自https://developer.chrome.com/apps/notifications#method-getAll

    检索应用或扩展程序的所有通知(强调我的。)

    【讨论】:

      【解决方案2】:

      您需要在清单中添加notifications 权限

      {
          "name": "ClearAll",
          "permissions": ["notifications"],
          .......
      }
      

      【讨论】:

      • 您说的完全正确,谢谢!添加该行后,我不再收到错误消息......但通知实际上永远不会被解雇!我已经进入 chrome://flags 在本地和非本地通知 toast 上测试脚本,但我仍然没有运气。你知道为什么会这样吗?
      • 这实际上没有帮助,因为“通知”权限仅允许您创建/编辑/清除扩展程序的通知,而不是系统范围的通知,正如我在回答中提到的那样。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多