2013-03-02 89 views
0

我目前正在嘗試使其擴展程序在調用時顯示通知。從內容腳本觸發Chrome擴展程序通知

當圖標被點擊時,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一個請求(執行腳本)背景。 js做出通知?

+0

請注意定義由它們的擴展的頁面使用變量或 函數定義的變量或 功能。在這個問題上的很多信息已經過時了。 'webkitNotifications'已棄用。 – Xan 2015-05-29 12:09:36

回答

2

是的。內容腳本不能做某事。 see here

所以你必須發送請求到background.js來做出通知。 ,如果您的通知有圖標,rememmber以記數它在你的manifest.json:

"web_accessible_resources":["face.png"] 

然而,內容腳本有一定的侷限性。他們不能:使用 鉻*的API(除了chrome.extension的部分)使用的網頁或其他內容的腳本

相關問題