« get me outta code hell

Use a poll to catch extra tricky location changes - interactive-bgm - Browser extension that adds background music based on the site you're browsing
about summary refs log tree commit diff
path: root/extension
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-03-26 11:16:04 -0300
committerFlorrie <towerofnix@gmail.com>2019-03-26 11:16:04 -0300
commit7dbcbe1ebb08cf3201df644561bc8de357e8f9ba (patch)
tree6ec67f6c17af463910749f30721566ecc4f635b5 /extension
parenta6b148696d4430656708148787af5b6f738ec9a1 (diff)
Use a poll to catch extra tricky location changes HEAD master
Diffstat (limited to 'extension')
-rw-r--r--extension/interactive-bgm.js19
1 files changed, 9 insertions, 10 deletions
diff --git a/extension/interactive-bgm.js b/extension/interactive-bgm.js
index 2bafe54..9c0e5dd 100644
--- a/extension/interactive-bgm.js
+++ b/extension/interactive-bgm.js
@@ -1,18 +1,17 @@
 const sendURL = () => {
-    browser.runtime.sendMessage({urlString: location.toString()});
+    if (!document.hidden) {
+        browser.runtime.sendMessage({urlString: location.toString()});
+    }
 };
 
 window.addEventListener('focus', sendURL);
+window.addEventListener('popstate', sendURL);
+document.addEventListener('visibilitychange', sendURL);
+sendURL();
 
-document.addEventListener('visibilitychange', () => {
-    if (!document.hidden) {
-        sendURL();
-    }
-});
-
-if (!document.hidden) {
-    sendURL();
-}
+// Some things we just can't reliably detect through event, like the location being changed via JS.
+// To catch those, we poll regularly.
+setInterval(sendURL, 1000);
 
 let isShowingCreateTrack = false;