Why event listeners in extensions stop working or work occasionally

Unpredictable execution of event listeners after service worker restarts

Marian C.
8 min readJul 21, 2024
The image was generated by https://pixlr.com/

Before modules were introduced into JavaScript, it used to be very common to use window load event to initialise listeners of DOM events:

When developing a browser extension it is very tempting to attach event handlers inside the onInstalled event listener. The official documentation recommends using onInstalled event for one-time initialization such as defining a context menu. However, unlike context menus, event listeners added in the onInstalled handler work a bit but soon afterwards do not work anymore:

Not only the listeners added in listeners, any event listeners that are attached if some condition is fulfilled are likely to stop responding shortly afterwards. Why it happens?

The official documentation of manifest 3 extension still has little information about the mechanics of events. It states…

--

--

Marian C.

Java, JavaScript and SQL developer. Interested in data collection and visualization.