gpt4 book ai didi

javascript - 无法让简单的 Chrome 通知发挥作用

转载 作者:行者123 更新时间:2023-12-02 16:06:15 27 4
gpt4 key购买 nike

我试图通过单击按钮来调出一个简单的 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!" />

list :

{
"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

提前致谢!

最佳答案

您在哪里调用 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(){})
}
});

关于javascript - 无法让简单的 Chrome 通知发挥作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30684740/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com