[英]For-Loop Notifications Not Appearing
我已经建立了一系列的For-Loop生成的链接,这些链接应该以通知的形式显示自定义消息。
显示通知的功能在我的后台事件页面(eventPage.js)中:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (request.todo == "notification"){
var notifOptions = {
type: 'basic',
iconUrl: 'icon48.png',
title: request.ttl,
message: request.msg
};
chrome.notifications.create('notti', notifOptions);
}
});
同时,在内容页面(content.js)上,有两个条件链接到此函数:
第一:
if (request.todo == "full"){
chrome.runtime.sendMessage({todo: "notification", ttl: 'Title', msg: 'Test!'});
}
此第一个功能有效,并且通知显示没有任何问题(因此,权限没有问题等)。
第二个条件是不起作用的条件:
if(request.todo == "quick"){
for (let i = 0; i < wrds.length; i++) {
var entry = "#"+wrds[i];
if ($(entry).length) {
$(entry).on("click", function() {
var msg = prns[i]+'\n'+defs[i];
chrome.runtime.sendMessage({todo: "notification", ttl: 'Title', msg: msg});
alert(msg);
});
}
}}
此条件尝试在for循环中设置一系列通知。 问题是,尽管警报运行良好,但通知根本没有出现。
如何显示for循环生成的通知?
for循环版本不起作用,因为通知标题必须唯一。
为了使通知始终显示,无论之前是否出现过,都需要使用唯一名称进行创建。
为了解决这个问题,我决定让这个名称成为唯一的时间戳。
for (let i = 0; i < wrds.length; i++) {
var entry = "#"+wrds[i];
if ($(entry).length) {
$(entry).on("click", function() {
var timestamp = String(new Date().getTime());
var msg = prns[i]+'\n'+defs[i];
chrome.runtime.sendMessage({todo: "notification", ttl: wrds[i], msg: msg, stamp:timestamp});
});
}
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (request.todo == "notification"){
var notifOptions = {
type: 'basic',
iconUrl: 'icon48.png',
title: request.ttl,
message: request.msg
};
chrome.notifications.create(request.stamp, notifOptions);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.