Chrome扩展程序:chrome.alarms在主窗口关闭后无法正常工作 [英] Chrome extension: chrome.alarms not working after main window closes

查看:131
本文介绍了Chrome扩展程序:chrome.alarms在主窗口关闭后无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每隔几个小时使用chrome.alarms API检查扩展程序中的某个值.在打开主窗口页面时触发警报,但是在关闭主窗口时不再触发警报.我的事件监听器和警报创建在后台页面上.在我的background.js中,我尝试使通知定期显示以测试警报.

这是我的"background.js"的相关部分:

  document.addEventListener("DOMContentLoaded",函数(e){chrome.runtime.getBackgroundPage(function(bg){bg.chrome.alarms.create("beep",{delayInMinutes:1,periodInMinutes:0.1});});var opt = {类型:基本",标题:任务到期",讯息:任务是明天到期.",iconUrl:"chrome-extension://paomcbcgpoikdcjhbanhllhdbemcjokf/Agenda_32.png",纽扣: [{标题:标记为完成"}]}函数clr(){chrome.notifications.clear('tst');}chrome.alarms.onAlarm.addListener(function(alarm){console.log("beeep");clr();chrome.notifications.create('tst',opt);});}); 

如何设置它,以便即使在我的主窗口关闭后也能触发警报?我已经阅读了一些文档,但是解决方案却使我难以理解.

解决方案

仅当用户配置文件的至少一个浏览器窗口正在运行时,扩展才运行,除非您专门添加了

I want to check a certain value in my extension every few hours using the chrome.alarms API. The alarms are triggering while the main window page is open, but when the main window closes the alarms no longer trigger. My event listener and alarm creation are on the background page. In my background.js, I try to make a notification appear at a regular interval to test the alarm.

This is the relevant part of my "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);
  });
});

How can I make it so that the alarms trigger even after my main window closes? I have read some documentation on this but the solution to this eludes me.

解决方案

Extensions run only when at least one browser window of the user profile is running unless you specifically add a permission in manifest.json:

"permissions": ["background"],
"background": {
  "persistent": true,
  "scripts": ["bg.js"]
}

Makes Chrome start up early and and shut down late, so that apps and extensions can have a longer life.

When any installed hosted app, packaged app, or extension has "background" permission, Chrome runs (invisibly) as soon as the user logs into their computer—before the user launches Chrome. The "background" permission also makes Chrome continue running (even after its last window is closed) until the user explicitly quits Chrome.

Important!

Make sure Continue running background apps is enabled in Chrome settings:

这篇关于Chrome扩展程序:chrome.alarms在主窗口关闭后无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆