【发布时间】:2020-07-21 06:41:31
【问题描述】:
我想每隔几个小时使用 chrome.alarms API 检查我的扩展程序中的某个值。警报在主窗口页面打开时触发,但当主窗口关闭时警报不再触发。我的事件监听器和警报创建在后台页面上。在我的 background.js 中,我尝试让通知定期出现以测试警报。
这是我的“background.js”的相关部分:
document.addEventListener("DOMContentLoaded", function(e) {
chrome.runtime.getBackgroundPage(function(bg) {
bg.chrome.alarms.create("beep", {delayInMinutes: 1, periodInMinutes: 0.1} );
});
var opt = {
type: "basic",
title: "Task due",
message: 'A task is due tomorrow.',
iconUrl: "chrome-extension://paomcbcgpoikdcjhbanhllhdbemcjokf/Agenda_32.png",
buttons: [
{
title: 'Mark as done'
}
]
}
function clr() {
chrome.notifications.clear('tst');
}
chrome.alarms.onAlarm.addListener(function(alarm) {
console.log("beeep");
clr();
chrome.notifications.create('tst', opt);
});
});
我怎样才能使警报即使在我的主窗口关闭后也能触发?我已经阅读了一些关于此的文档,但我没有找到解决方案。
【问题讨论】:
标签: javascript google-chrome-extension