diff options
| author | sillylaird <sillyfanboy@gmail.com> | 2026-09-03 00:33:59 +0000 |
|---|---|---|
| committer | sillylaird <sillyfanboy@gmail.com> | 2026-09-03 00:33:59 +0000 |
| commit | 898b52edcb47bcb3e9d6106e74ca73e74ea01e70 (patch) | |
| tree | 85c6ee5ad58b860144551184d4cf86b560c62b91 /assets/js/pages/radio-nowplaying.js | |
| download | www-main.tar.gz www-main.zip | |
Diffstat (limited to 'assets/js/pages/radio-nowplaying.js')
| -rw-r--r-- | assets/js/pages/radio-nowplaying.js | 41 |
1 files changed, 41 insertions, 0 deletions
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 <span>. + 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(); + }); +})(); |
