blob: e263e5bf86a26e3f79736fa74f6abf8f3a868a80 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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();
});
})();
|