blob: 7f488f85419015ed2dc4b0c110673a9f2ec9397d (
plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
const player = document.getElementById('radioPlayer');
const button = document.getElementById('radioButton');
const infoEl = document.getElementById('info');
button.addEventListener('click', () => {
if (player.paused) {
player.play();
button.textContent = 'stop';
} else {
player.pause();
player.currentTime = 0;
button.textContent = 'play';
}
});
let spinnerInterval;
function startSpinner() {
const spinnerChars = ['|', '/', '-', '\\'];
let i = 0;
spinnerInterval = setInterval(() => {
infoEl.textContent = `${spinnerChars[i++ % spinnerChars.length]}`;
}, 100);
}
function stopSpinner() {
clearInterval(spinnerInterval);
}
let spinnerHasRun = false;
async function fetchCurrentTrack() {
if (!spinnerHasRun) {
startSpinner();
await new Promise(resolve => setTimeout(resolve, 3000));
}
try {
const response = await fetch('/info');
if (!response.ok) throw new Error('Network response was not ok');
const data = await response.json();
const source = data.icestats.source;
const title = source.title || 'unknown';
const listeners = source.listeners || 0;
const listenerLabel = listeners === 1 ? 'listener' : 'listeners';
stopSpinner();
infoEl.textContent = `${title} | ${listeners} ${listenerLabel}`;
spinnerHasRun = true;
} catch (error) {
stopSpinner();
infoEl.textContent = 'Error loading track info';
console.error(error);
}
}
fetchCurrentTrack();
setInterval(fetchCurrentTrack, 10000);
|