按需持久化后台页面还是不卸载的事件页面? [英] Persistent background page on demand or an event page that doesn't unload?

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

问题描述

我想构建一个行为像计时器的扩展.它应该在激活时倒计时,但不应该对不活动进行任何操作.

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.

chrome.alarms API 很有趣,但没有足够的精度和粒度.它每分钟最多只能触发一次,并且可能会延迟触发.如果我想要更频繁地执行某些操作,则无法使用此 API.

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.

然后,下一个自然的解决方案是使用 背景页面 并使用 setTimeoutsetInterval 在那里.然而,后台页面是持久的,即使在空闲时它们也会占用资源(例如内存).所以它们并不理想.

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:

一旦加载完毕,只要它处于活动状态,事件页面就会一直运行(例如,调用扩展 API 或发出网络请求).

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).

[…]

一旦事件页面空闲了一小段时间(几秒),runtime.onSuspend 事件就会被调度.事件页面在被强行卸载之前还有几秒钟的时间来处理这个事件.

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.

[…]

如果您的扩展程序使用 window.setTimeout()window.setInterval(),请改用警报 API.如果事件页面关闭,则基于 DOM 的计时器将不会受到尊重.

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.

不幸的是,只有一个活动的 setInterval 不足以将事件页面视为活动页面.事实上,根据我的测试,最多 10 秒的间隔足够短以保持事件页面运行,但任何大于 10 或 15 秒的间隔都太远,事件页面将被卸载.我已经在 我的 crx-reload-tab 项目 上对此进行了测试.

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?

推荐答案

后台页面无法按需卸载,Chrome为你决定事件页面生命周期(onSuspend你无能为力防止它).

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).

如果您关心的是计时器,您可以尝试我的解决方案 this answer,它基本上将计时器拆分为较短的计时器对于稀疏"忙等待.这足以保持事件页面加载,如果您不需要经常这样做,这是一个可行的解决方案.

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.

如果您有任何其他上下文来保持开放的 Port 到例如内容脚本,则可以利用此漏洞.有关详细信息,请参阅长期连接文档.

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天全站免登陆