2015-11-19 48 views
0

我希望在用戶訪問某些網頁時彈出Chrome通知,例如'www.amazon.com'或'google.com'等。該擴展加載到鉻完美罰款,沒有錯誤,但通知不會彈出時,我去那些特定的頁面。Chrome擴展程序 - 某些網頁上的通知

我目前有下面的腳本。

的manifest.json

{ 
    "name": "Test", 
    "version": "0.0.1", 
    "manifest_version": 2, 
    "description": "This extension was created with the awesome extensionizr.com", 
    "homepage_url": "http://www.test.co.uk", 
    "icons": { 
    "16": "icons/icon16.png", 
    "48": "icons/icon48.png", 
    "128": "icons/icon128.png" 
    }, 
    "default_locale": "en", 
    "background": { 
    "page": "src/bg/background.html", 
    "persistent": true 
    }, 
    "options_page": "src/options/index.html", 
    "browser_action": { 
    "default_icon": "icons/icon19.png", 
    "default_title": "browser action demo", 
    "default_popup": "src/browser_action/browser_action.html" 
    }, 
    "permissions": ["notifications"], 
    "content_scripts": [ 
    { 
     "matches": ["http://www.amazon.com/*", "http://amazon.co.uk/*"], 
     "js": ["js/script.js"] 
    } 
    ] 
} 

JS /的script.js

if(onCertainWebsitesNeedNotificationAppearTrue) { 
    // send message to background script 
    chrome.runtime.sendMessage({greeting: "hello"}, function(response) { 
}); 
} 

background.js

chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) { 
    chrome.pageAction.show(sender.tab.id); 
    sendResponse(); 
    }); 


chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) { 
     //alert("good"); 
     if (request.greeting == "hello") 
     createNotification(); 
    }); 
function createNotification(){ 
    var opt = {type: "basic",title: "Your Title",message: "Your message",iconUrl: "128.png"} 
    chrome.notifications.create("notificationName",opt,function(){}); 

//include this line if you want to clear the notification after 5 seconds 
    setTimeout(function(){chrome.notifications.clear("notificationName",function(){});},10000); 
} 

氖w鉻擴展編碼,所以任何幫助將不勝感激!

+0

將'chrome.runtime.onMessage'回調中'chrome.extension.onMessage'回調中的代碼移出並移除前者。 – wOxxOm

+0

剛剛嘗試過 - 仍然沒有任何事情發生:( – Jonathan

+0

)如果取消註釋,是否會發出警報(「好」)?無論如何,請在後臺頁面上使用調試器,看看發生了什麼。 – wOxxOm

回答

0

onCertainWebsitesNeedNotificationAppearTrue沒有被定義在任何地方,所以代碼沒有被執行並且消息沒有被髮送。

error

使用devtools debugger來診斷問題。

還將代碼從chrome.extension.onMessage回撥到chrome.runtime.onMessage回調中並刪除前者。