Replace unload event with visibilitychange (#480)

This should fix bfcache compatibility issues.

https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event#usage_notes
This commit is contained in:
Willian Wang 2023-09-08 10:55:49 -03:00 committed by GitHub
parent 6af6abd501
commit bb372420d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,7 +9,7 @@ const init = async () => {
await initSettings(); await initSettings();
document.addEventListener("mouseup", handleMouseUp); document.addEventListener("mouseup", handleMouseUp);
document.addEventListener("keydown", handleKeyDown); document.addEventListener("keydown", handleKeyDown);
window.addEventListener("unload", onUnload, { once: true }); document.addEventListener("visibilitychange", handleVisibilityChange);
browser.storage.onChanged.addListener(handleSettingsChange); browser.storage.onChanged.addListener(handleSettingsChange);
browser.runtime.onMessage.addListener(handleMessage); browser.runtime.onMessage.addListener(handleMessage);
overWriteLogLevel(); overWriteLogLevel();
@ -133,8 +133,12 @@ const handleKeyDown = e => {
} }
}; };
const onUnload = () => { const handleVisibilityChange = () => {
browser.storage.onChanged.removeListener(handleSettingsChange); if (document.visibilityState === "hidden") {
browser.storage.onChanged.removeListener(handleSettingsChange);
} else {
browser.storage.onChanged.addListener(handleSettingsChange);
}
}; };
let isEnabled = true; let isEnabled = true;