For my chrome and firefox extension, I need a way to figure if the background script has unloaded or is unloading. Is there an event fired when this happens? I went through multiple links on stackoverflow and even some official chromium bug links but could not find a concrete answer. beforeunload is not supported for background scripts and closing the windows does not guarantee that browser is closed cause if extension has "background" permission, the background script would still be running in background. What can I do now?
1 Answer
The closest there is is chrome.runtime.onSuspend, but there is no guarantee how much time you're allowed before the context shuts down. Note that this is not supported in WebExtensions as of yet. The documentation expressly states:
Sent to the event page just before it is unloaded. This gives the extension opportunity to do some clean up. Note that since the page is unloading, any asynchronous operations started while handling this event are not guaranteed to complete.
In general, you can't do much on event/background page unload and should make your code robust to abrupt unloads. This event may even not trigger for some shutdowns.
5 Comments
storage.set when your data changes. And be tolerant of loss of small chunk of data anyway since, again, no operation is guaranteed to finish before shutdown.