我试图通过点击按钮来显示简单的Chrome通知,由于某种原因我收到了错误消息。 这是Javascript部分:
$('#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"
]
}
试图跑步时我得到了:
未捕获的TypeError:无法读取未定义的属性“create”
提前致谢!
答案 0 :(得分:2)
你在哪里调用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(){})
}
});
答案 1 :(得分:0)
嗯,显然代码是正确的。我试图使用Chrome作为扩展程序来运行该页面。 去图......