From bb372420d7179510c5bffd0c5c7b3c7051e4014f Mon Sep 17 00:00:00 2001 From: Willian Wang Date: Fri, 8 Sep 2023 10:55:49 -0300 Subject: [PATCH] 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 --- src/content/index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/content/index.js b/src/content/index.js index 6af79f0..be529b6 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -9,7 +9,7 @@ const init = async () => { await initSettings(); document.addEventListener("mouseup", handleMouseUp); document.addEventListener("keydown", handleKeyDown); - window.addEventListener("unload", onUnload, { once: true }); + document.addEventListener("visibilitychange", handleVisibilityChange); browser.storage.onChanged.addListener(handleSettingsChange); browser.runtime.onMessage.addListener(handleMessage); overWriteLogLevel(); @@ -133,8 +133,12 @@ const handleKeyDown = e => { } }; -const onUnload = () => { - browser.storage.onChanged.removeListener(handleSettingsChange); +const handleVisibilityChange = () => { + if (document.visibilityState === "hidden") { + browser.storage.onChanged.removeListener(handleSettingsChange); + } else { + browser.storage.onChanged.addListener(handleSettingsChange); + } }; let isEnabled = true;