簡體   English   中英

創建 Chrome 擴展程序以通過鍵盤快捷鍵關閉通知

[英]Creating Chrome Extension to Dismiss Notifications via Keyboard Shortcut

我是 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
}

背景.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);
      });
    }
});

錯誤:

圖像:無法讀取未定義錯誤的屬性“getAll”

您可能已經弄清楚了這一點,但是對於將來偶然發現這個問題的任何人來說:不可能編寫一個擴展程序來關閉 Chrome 上的所有通知,因為任何擴展程序都無法訪問用戶的瀏覽器范圍通知; notifications在許可manifest.json允許您創建和您的擴展創建明確的通知。 事實上,如果允許任何擴展這樣做,那將是侵犯隱私!

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

檢索應用程序或擴展程序的所有通知(重點是我的。)

您需要在清單中添加notifications權限

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM