From 898b52edcb47bcb3e9d6106e74ca73e74ea01e70 Mon Sep 17 00:00:00 2001 From: sillylaird Date: Thu, 3 Sep 2026 00:33:59 +0000 Subject: import live www.sillylaird.ca webroot --- assets/js/pages/radio-nowplaying.js | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 assets/js/pages/radio-nowplaying.js (limited to 'assets/js/pages/radio-nowplaying.js') diff --git a/assets/js/pages/radio-nowplaying.js b/assets/js/pages/radio-nowplaying.js new file mode 100644 index 0000000..e263e5b --- /dev/null +++ b/assets/js/pages/radio-nowplaying.js @@ -0,0 +1,41 @@ +(function () { + var line = document.getElementById('radio-nowplaying'); + var el = document.getElementById('radio-nowplaying-title'); + if (!line || !el) return; + var pollMs = 30000; + var backoffMs = pollMs; + + // childNodes[0] is the "Now playing: " text node before the . + function setLabel(text) { + if (line.childNodes[0] && line.childNodes[0].nodeType === 3) { + line.childNodes[0].textContent = text; + } + } + function setOffline() { + setLabel('Radio: '); + el.textContent = 'currently offline'; + } + function setPlaying(title) { + setLabel('Now playing: '); + el.textContent = title; + } + + function tick() { + fetch('/api/nowplaying.php', { cache: 'no-store' }) + .then(function (r) { return r.ok ? r.json() : Promise.reject(r.status); }) + .then(function (j) { + var title = j && j.title ? String(j.title).trim() : ''; + if (title) { setPlaying(title); } else { setOffline(); } + backoffMs = pollMs; + }) + .catch(function () { + setOffline(); + backoffMs = Math.min(backoffMs * 2, 5 * 60 * 1000); + }) + .finally(function () { setTimeout(tick, backoffMs); }); + } + if (document.visibilityState !== 'hidden') tick(); + document.addEventListener('visibilitychange', function () { + if (document.visibilityState === 'visible') tick(); + }); +})(); -- cgit v1.2.3