Event Pages and Background Pages

Since there is no clear explanation in the Chrome Extensions documentation, I came here for reference.

I learned that the background pages are mainly designed to extend the life of the extension and are designed to store values ​​or save the “engine” running in the background, so no one notices it. Because as soon as you click on the extension icon, you get what they call a pop-up window, and as soon as you click on a pop-up window, it disappears immediately, and the most important extension “dies” (its life span ends )

So far we are good, and everything is fine, but: the event pages were invented after that and they are mainly man pages that work only when they are called (to provide more memory).

If so, is it not contradictory? What is the use of event pages if they only work when called?

+7
google-chrome-extension
source share
1 answer

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

In this case, the page makes sense. It is not completely unloaded, as if the extension was stopped - it defines its event handlers (what it wants to listen to), and then it turns off until it is needed. Think this is "I'm going to sleep, don't wake me if it doesn't happen."

The difference with your example: a closed pop-up window ceases to exist completely, and Chrome remembers that he needs to call a specific extension for certain events . If this event occurs, the background page starts again and the event fires in it.

This saves resources, but does not always work. Closing the background context of the page clears its local state; it should be stored 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 respond very quickly or very often, pausing / resuming may be successful.

In general, event pages are not a complete replacement for background pages ; therefore, they are optional, and not defaulted. There are many things to consider when creating an event page.

PS As for your “popup as the most important part of the extension”: that’s why it may not be the most important part in most cases. Typically, the background page is also used along with a pop-up window to store event listeners and local state.

+6
source share

All Articles