From e23bab0103ee7dccd94b086ee6f48dd8970388dc Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 1 Jun 2019 17:38:49 -0500 Subject: [PATCH] Only add notification event listener after onload --- assets/js/embed.js | 2 +- assets/js/notifications.js | 52 +++++++++++++++++------------------ assets/js/subscribe_widget.js | 4 +-- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/assets/js/embed.js b/assets/js/embed.js index 12530fbe..d2116b2e 100644 --- a/assets/js/embed.js +++ b/assets/js/embed.js @@ -1,4 +1,4 @@ -function get_playlist(plid, timeouts) { +function get_playlist(plid, timeouts = 0) { if (timeouts > 10) { console.log('Failed to pull playlist'); return; diff --git a/assets/js/notifications.js b/assets/js/notifications.js index 5c9847de..90b8c4f0 100644 --- a/assets/js/notifications.js +++ b/assets/js/notifications.js @@ -1,6 +1,6 @@ var notifications, delivered; -function get_subscriptions(callback, failures) { +function get_subscriptions(callback, failures = 1) { if (failures >= 10) { return } @@ -92,31 +92,6 @@ function create_notification_stream(subscriptions) { notifications.stream(); } -window.addEventListener('storage', function (e) { - if (e.key === 'stream' && !e.newValue) { - if (notifications) { - localStorage.setItem('stream', true); - } else { - setTimeout(function () { - if (!localStorage.getItem('stream')) { - get_subscriptions(create_notification_stream); - localStorage.setItem('stream', true); - } - }, Math.random() * 1000 + 10); - } - } else if (e.key === 'notification_count') { - var notification_ticker = document.getElementById('notification_ticker'); - - if (parseInt(e.newValue) > 0) { - notification_ticker.innerHTML = - '' + e.newValue + ' '; - } else { - notification_ticker.innerHTML = - ''; - } - } -}); - window.addEventListener('load', function (e) { localStorage.setItem('notification_count', document.getElementById('notification_count') ? document.getElementById('notification_count').innerText : '0'); @@ -130,6 +105,31 @@ window.addEventListener('load', function (e) { } }, Math.random() * 1000 + 10); } + + window.addEventListener('storage', function (e) { + if (e.key === 'stream' && !e.newValue) { + if (notifications) { + localStorage.setItem('stream', true); + } else { + setTimeout(function () { + if (!localStorage.getItem('stream')) { + get_subscriptions(create_notification_stream); + localStorage.setItem('stream', true); + } + }, Math.random() * 1000 + 10); + } + } else if (e.key === 'notification_count') { + var notification_ticker = document.getElementById('notification_ticker'); + + if (parseInt(e.newValue) > 0) { + notification_ticker.innerHTML = + '' + e.newValue + ' '; + } else { + notification_ticker.innerHTML = + ''; + } + } + }); }); window.addEventListener('unload', function (e) { diff --git a/assets/js/subscribe_widget.js b/assets/js/subscribe_widget.js index f875d505..8f055e26 100644 --- a/assets/js/subscribe_widget.js +++ b/assets/js/subscribe_widget.js @@ -7,7 +7,7 @@ if (subscribe_button.getAttribute('data-type') === 'subscribe') { subscribe_button.onclick = unsubscribe; } -function subscribe(timeouts) { +function subscribe(timeouts = 0) { if (timeouts >= 10) { console.log('Failed to subscribe.'); return; @@ -41,7 +41,7 @@ function subscribe(timeouts) { } } -function unsubscribe(timeouts) { +function unsubscribe(timeouts = 0) { if (timeouts >= 10) { console.log('Failed to subscribe'); return;