Why does runtime.onSuspend not execute window.localStorage.setItem?

I am coding my first extension right now and I wanted to save a value before the browser closes. I, at first, tried to use browser.runtime.onSuspend.addListener() :

browser.runtime.onSuspend.addListener(() => {
	window.localStorage.setItem("suspent", Date.now());
});

This did not work. I googled for a bit found that one could also use window.addEventListener(‘unload’, function):

window.addEventListener('unload', function () {
	window.localStorage.setItem("unload", Date.now());
	});

This version worked without any issues.

Does anybody know why the first one using onSuspend didn’t work?

Which Firefox version are you using, and how do you load the extension in Firefox?

Nevermind. I hadn’t read the documentation for onSuspend properly. It only triggers when the to be unloaded script is a non-persistent background page. I was using a persistent one.

But still thank you for trying to help.

1 Like