【发布时间】:2019-07-29 13:43:17
【问题描述】:
我正在尝试进行扩展。它应该在网站每分钟自动刷新的文本字段中插入值时播放声音并显示通知。
对于通知,我发现这段代码可以正常工作,我把它放在 background.js 中:
createNotification();
audioNotification();
function audioNotification(){
var yourSound = new Audio('notification.mp3');
yourSound.play();
}
function createNotification(){
var opt = {type: "basic",title: "Your Title",
message: "Your message",iconUrl: "your_icon.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(){});},5000);}
因此,如果已为文本字段分配了值,我需要执行通知代码。
我曾尝试在上面的代码之前放置一个 if 和 then 语句,但它似乎不起作用。我没有 content.js 文件,还是应该?
【问题讨论】:
-
如果我没记错的话,谷歌浏览器不允许你在没有用户首先与网页上的按钮交互的情况下通过浏览器播放音频,这个权限将在每次页面加载时重置。
-
我们建议它不是为了声音通知,我应该在 content.js 中使用什么命令来加载和执行上面的代码?
-
我不知道
content.js是什么。 -
就像我上面提到的,我没有 content.js,因为我不知道如何使用它。我所知道的是我需要触发 background.js 并且它只能从 content.js 中发生?
-
你在使用某种框架吗?
content.js可以是任何东西
标签: google-chrome notifications background