活动页面和背景页面 [英] Event pages and background pages

查看:79
本文介绍了活动页面和背景页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我了解到,后台页面基本上是为了延长扩展的使用寿命而设计的,保持值或保持引擎在后台运行,所以没有人注意到它。因为一旦你点击了扩展程序的图标,就会得到他们称之为弹出式窗口的内容,一旦你在弹出窗口外单击,它将立即消失,最重要的是扩展程序死亡(它的生命期结束)。 >

到目前为止,我们都很好,一切都很好,但是:事件页面是在
之后发明的,它们基本上是仅在被调用时才起作用的背景页面提供更多的内存空间)。



如果是这样的话,那么这不会是矛盾的吗?如果事件页面只在被调用的时候才起作用,它有什么作用?

有时候背景页面只需要响应事件(消息,web请求,按钮点击等)。

在这种情况下,活动页面是有道理的。它没有完全卸载,就好像扩展停止了一样 - 它定义了它的事件处理程序(它想要听的),然后关闭直到需要。考虑这是我要睡觉,除非发生A事件,否则不要叫醒我。



与您的示例的不同之处在于:封闭的弹出窗口不再完全存在,而 Chrome记得它需要针对特定​​事件调用特定的扩展程序。如果发生该事件,则会再次启动后台页面,并在该事件中触发该事件。



这节省了资源,但并不总是合适的。关闭后台页面的上下文会擦除其本地状态;它必须保存在各种存储API中而不是变量中。如果当地的国家很复杂,这可能是不值得的。此外,如果您的扩展程序需要反应速度非常快或者经常发生,暂停/恢复可能会被打击。总而言之,活动页面是不是完整的替代背景页面;这就是为什么它们是可选的而不是默认的。制作活动页面时有需要考虑很多事项



PS关于你的弹出作为扩展的最重要部分:这正是它在大多数情况下不能成为最重要的部分的原因。通常,背景页面也会与弹出窗口一起用于保持事件侦听器和本地状态。


Since there's no clear explanation in Chrome Extensions documentation, I came here for help.

I learned that background pages are basically invented to extend the extension's lifetime, and designed to hold values or keep the "engine" running in background so no one notices it. Because once you click on the extension's icon, you get what they call it, a "popup", and once you click outside the "popup" it disappears immediately and most important the extension "dies" (its lifetime ends).

So far we are good and everything is nice but: event pages are invented after that
and they are basically background pages that only work when they are called (to provide more memory space).

If that's the case, then wouldn't that be contradictory? What's the use of event pages if they only work when they're called?

解决方案

Sometimes background pages only need to respond to events outside them (messages, web requests, button clicks, etc.)

In that case, an event page makes sense. It's not completely unloaded as if the extension is stopped - it defines its event handlers (what it wants to listen to) and then it's shut down until needed. Consider this to be "I'm going to sleep; don't wake me up unless A happens."

The difference with your example: closed popup ceases to exist completely, while Chrome remembers it needs to call a particular extension on particular events. If that event happens, the background page is started again an the event is fired in it.

This saves resources, but not always appropriate. Shutting down background page's context wipes its local state; it must be saved in various storage APIs instead of variables. If the local state is complex, it may not be worth the effort. Also, if your extension needs to react really fast or really often, suspend/resume may prove to be a performance hit.

All in all, event pages are not a complete replacement for background pages; that's why they are optional and not default. There are many things to consider when making an event page.

P.S. Regarding your "popup as most important part of the extension": this is exactly why it can't be the most important part in most cases. Usually, a background page is also used alongside a popup to keep event listeners and local state.

这篇关于活动页面和背景页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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