2015-06-06 44 views
0

我正在嘗試通過單擊按鈕顯示簡單的Chrome通知,並且出於某種原因導致出現錯誤。 這裏的JavaScript部分:無法通過簡單的Chrome通知工作

$('#notification').click(function() { 
     var opt = { 
      TemplateType: "basic", 
      title: "Just a test!", 
      message: "Let's see if it works", 
      iconUrl: "icon.png" 
     } 

     chrome.notifications.create('notify', opt, function() { }); 
}); 

的HTML部分:

<input id="notification" type="submit" value="Get Notification!" /> 

而且清單:

{ 
"manifest_version": 2, 

"name": "SimpleNotification", 
"description": "Just a notification", 
"version": "1.0", 

"browser_action": { 
    "default_icon":"icon.png", 
    "default_popup":"popup.html" 
}, 

"options_page" : "options.html", 

"background": { 
    "scripts" : ["eventPage.js"], 
    "persistent" : false 
}, 

"permissions" : [ 
    "storage", 
    "notifications", 
    "contextMenus" 
] 

}

當試圖運行我得到:

Uncaught TypeError: Cannot read property 'create' of undefined

在此先感謝!

回答

0

好吧,顯然代碼是正確的。我試圖用Chrome瀏覽器運行該頁面,而不是擴展名。 去圖...

1

你在哪裏打電話chrome.notifications ?,這隻能從擴展名(背景)調用。你可以做到這一點,通過message passing到後臺腳本。

實施例:

//contentScript.js 
var opt = { 
    TemplateType: "basic", 
    title: "Just a test!", 
    message: "Let's see if it works", 
    iconUrl: "icon.png" 
}; 
chrome.runtime.sendMessage({type: "shownotification", opt: opt}, function(){}); 

//background.js 
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { 
    if(request.type === "shownotification"){ 
     chrome.notifications.create('notify', request.opt, function(){}) 
    } 
}); 
+0

感謝您的提示!我發現我應該從擴展本身,而不是從瀏覽器運行 –

+0

好吧,我做了它的工作,但由於一些奇怪的原因,我需要重新加載擴展,使其再次工作.. –