页面刷新后Chrome API警报会停止(Chrome API Alarm stops after page refresh)

编程入门 行业动态 更新时间:2024-10-25 23:23:47
页面刷新后Chrome API警报会停止(Chrome API Alarm stops after page refresh)

阅读时,我的印象是,即使在Chrome关闭,页面刷新等情况下,Chrome闹钟仍会继续工作。来自Google的一项结果:

使用Chrome警报API,您可以设置一个警报,只要安装了应用程序,即使其后台页面处于非活动状态也会持续发出警报。

我的问题是我在我的options.js页面中为Chrome扩展程序设置了Chrome警报。 保存选项页面时会创建警报(chrome.alarms.create)。

如果我打开页面,警报就会起作用。 但是,如果我重新加载页面(options.html),我不再收到警报。

这是我的options.js的一部分

$('#save-options-button').on('click', function() { //Clears existing alarm chrome.alarms.clearAll(); //Create alarm chrome.alarms.create("fetchAlarm", { delayInMinutes: 1, periodInMinutes: 1 }); chrome.alarms.onAlarm.addListener(function(alarm) { console.log("Got an alarm!", alarm); }); });

Reading around, I was under the impression that the Chrome alarm continues to work even after Chrome is closed, page refreshed etc. From one of the Google results:

With the Chrome alarm APIs, you can set an alarm that lasts as long as the app is installed, even if its background page goes inactive.

My problem is that I have setup a Chrome alarm in my options.js page for a Chrome Extension. The alarm is created (chrome.alarms.create) when the options page is saved.

The alarm works if I keep my page open. However, if I reload the page (options.html), I don't get the alarms anymore.

Here is part of my options.js

$('#save-options-button').on('click', function() { //Clears existing alarm chrome.alarms.clearAll(); //Create alarm chrome.alarms.create("fetchAlarm", { delayInMinutes: 1, periodInMinutes: 1 }); chrome.alarms.onAlarm.addListener(function(alarm) { console.log("Got an alarm!", alarm); }); });

最满意答案

我猜你引用的是Chrome Apps文档,这就是为什么它会让人感到困惑。

意思是事件页面 (一种背景页面 )将被唤醒以提供警报事件,即使它们因空闲而被卸载。

这仅适用于事件/背景页面。 Chrome不会随意打开页面,因为过去页面收听的事件发生了。 因此,您不应将实际的事件侦听器逻辑放入UI页面(选项,弹出窗口等)

在您的情况下,刷新页面会使包含侦听器的JS上下文与其一起销毁,并且您的逻辑仅在单击后添加侦听器。 即使您将addListener到顶级代码,它也只会在页面打开时运行。

因此,您需要添加一个后台页面来为该事件(实际的侦听器)提供服务。

背景页面通常随时可以回答事件。 事件页面的特殊之处在于Chrome会记住哪些事件应该触发其加载并且不会加载它们。 您可以在我的另一个答案中阅读更多相关信息。

I guess you are quoting the Chrome Apps documentation, that's why it gets confusing.

What was meant is that event pages (a type of background page) will be woken up to serve an alarm event even if they were unloaded for being idle.

This applies only to event/background pages. Chrome would not randomly open a page only because an event that the page listened to in the past happened. As such, you should not put actual event listener logic into UI pages (options, popup, etc.)

In your case, refreshing the page makes the JS context that contained the listener to be destroyed along with it, and your logic only adds a listener after a click. Even if you added that addListener to the top level code instead, it would only function as long as the page is open.

So, you will need to add a background page to service that event (the actual listener).

A background page is normally always ready to answer events. Event pages are special in that Chrome remembers which events should trigger their load and doesn't keep them loaded. You can read more about it in another answer of mine.

更多推荐

本文发布于:2023-08-02 12:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1375458.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   页面   API   Chrome   refresh

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!