持久性按需的背景页面还是无法卸载的事件页面? [英] Persistent background page on demand or an event page that doesn't unload?

查看:111
本文介绍了持久性按需的背景页面还是无法卸载的事件页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个行为类似于计时器的扩展.启用后,它应该倒计时秒数,但是不启用时,它什么也不做.

chrome.alarms API很有趣,但是没有足够的精度或粒度.每分钟最多只能发射一次,并且可能发射得晚.如果我想让某个东西执行得更多,那我就不能使用这个API.

然后,下一个自然的解决方案是使用背景页面并使用setTimeout或其中的setInterval.但是,后台页面是持久性的,即使空闲时它们也会占用资源(例如内存).因此它们并不理想.

最好的解决方案似乎是运行计时器的事件页面.但是,文档说:

一旦加载,事件页面将一直保持活动状态(例如,调用扩展API或发出网络请求).

[…]

一旦事件页面在短时间内(几秒钟)处于空闲状态,就会调度runtime.onSuspend事件.在事件页面被强行卸载之前,还有几秒钟的时间来处理该事件.

[…]

如果您的扩展使用window.setTimeout()window.setInterval(),请改为使用警报API.如果事件页面关闭,基于DOM的计时器将不被接受.

不幸的是,拥有活动的setInterval不足以使事件页面处于活动状态.实际上,根据我的测试,最多10秒的间隔足以使事件页面保持运行状态,但是任何大于10或15秒的时间间隔都相差太远,因此事件页面将被卸载.我已经在我的crx-reload-tab项目上对此进行了测试.


我相信我想要的是中间立场:

  • 我想要一个可以按需加载和卸载的背景页面. (而不是一直保持加载状态的.)
  • 我想要一个事件页面,只要我说的那样,它就一直保留在内存中;但否则可能会被卸载. (而不是由浏览器自动卸载的.)

有可能吗?我该怎么办?

解决方案

无法按需卸载背景页面,Chrome会为您确定事件页面的生命周期(您无法在onSuspend中进行任何操作来阻止它). /p>


如果您关心的是计时器,则可以尝试从此答案中尝试我的解决方案,该方法基本上将计时器分为几个较短的计时器进行稀疏"的忙碌等待.这足以保持事件页面的加载,并且如果您不需要经常执行此操作,那么这是一个可行的解决方案.


通常,有一些事情可以使事件页面保持加载状态:

如果您正在使用消息传递,请确保关闭未使用的消息端口.在关闭所有消息端口之前,事件页面不会关闭.

如果您有任何其他上下文可以保持对Port的打开状态(例如,内容脚本),则可以利用此功能.有关更多详细信息,请参见寿命长的连接文档.


在实践中,如果您经常或经常需要精确的分钟计时器,那么事件"页面是一个不好的解决方案.使用一个资源所获得的资源可能无法证明其合理性.

I want to build a extension that behaves like a timer. It should count down the seconds when activated, but should do nothing with inactive.

The chrome.alarms API is interesting, but does not have enough precision nor granularity. It only fires at most once per minute, and it may fire late. If I want something to execute more often than that, I can't use this API.

Then, the next natural solution is to use a background page and use setTimeout or setInterval in there. However, background pages are persistent, and they take up resources (e.g. memory) even when idle. So they are not ideal.

The best solution seems to be an event page to run the timer. However, the documentation says:

Once it has been loaded, the event page will stay running as long as it is active (for example, calling an extension API or issuing a network request).

[…]

Once the event page has been idle a short time (a few seconds), the runtime.onSuspend event is dispatched. The event page has a few more seconds to handle this event before it is forcibly unloaded.

[…]

If your extension uses window.setTimeout() or window.setInterval(), switch to using the alarms API instead. DOM-based timers won't be honored if the event page shuts down.

Unfortunately, having an active setInterval is not enough to consider an event page active. In fact, from my tests, an interval up to 10 seconds is short enough to keep the event page running, but anything greater than 10 or 15 seconds is too far apart and the event page will get unloaded. I've tested this on my crx-reload-tab project.


I believe what I want is a middle ground:

  • I want a background page that I can load and unload on demand. (Instead of one that keeps loaded all the time.)
  • I want an event page that stays persistent in memory for as long as I say; but otherwise could be unloaded. (Instead of one that gets unloaded automatically by the browser.)

Is it possible? How can I do it?

解决方案

Background pages cannot be unloaded on demand, and Chrome decides Event page lifecycle for you (there is nothing you can do in onSuspend to prevent it).


If your concern is timers, you could try my solution from this answer, which basically splits a timer into shorter timers for a "sparse" busy-wait. That's enough to keep the event page loaded and is a viable solution if you don't need to do that frequently.


In general, there are some things that will keep an event page loaded:

If you're using message passing, be sure to close unused message ports. The event page will not shut down until all message ports are closed.

This can be exploited if you have any other context to keep an open Port to, for example a content script. See Long-lived connections docs for more details.


In practice, if you often or constantly need precise, sub-minute timers, an Event page is a bad solution. Your resource gains from using one might not justify it.

这篇关于持久性按需的背景页面还是无法卸载的事件页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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