簡體   English   中英

從內容腳本觸發Chrome擴展程序通知

[英]Trigger Chrome extension notifications from content script

我目前正在嘗試這樣做,所以我的擴展程序會在調用時顯示通知。

單擊圖標時,background.js會在頁面中執行腳本。 這是我的background.js文件的樣子:

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(null,{file: "buy.js"});
  }
);

chrome.extension.onRequest.addListener(
  function (request, sender, sendResponse) {
    var notify = webkitNotifications.createNotification(
      'face.png',
      'Item Sniped!',
      'Item sniper sniped an item!'
    );
    notify.show();
  }
);

是的,我確實在manifest.json中設置了所有權限。 我的清單文件:

{
   "manifest_version": 2,
   "name": "Sniper",
   "version": "1.5",
   "description": "Sniper",
   "browser_action": {
     "default_icon": "face.png",
     "default_title": "Limited Sniper"
   },
   "background": { "scripts": ["background.js"] },
   "permissions": [
     "notifications",
     "tabs",
     "http://*/*"
   ]
}

我知道我需要在background.js文件中有一個監聽器,但是甚至可以從buy.js(執行的腳本)向background.js發送請求來發出通知嗎?

是。 內容腳本無法做某事。 看這里

所以你必須向background.js發送一個請求來發出通知。 並且,如果您的通知有圖標,請記住在您的manifest.json中注冊它:

 "web_accessible_resources":["face.png"]

但是,內容腳本有一些限制。 它們不能:使用chrome。* API(chrome.extension的部分除外)使用由其擴展名頁面定義的變量或函數使用由網頁或其他內容腳本定義的變量或函數

暫無
暫無

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

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