2023-08-22 16:35:43 +04:00
|
|
|
const $ = id => document.getElementById(id);
|
2022-03-08 01:17:24 +04:00
|
|
|
|
2023-10-07 05:33:57 +04:00
|
|
|
const formatDuration = date => `${date.getUTCHours() > 0 ? date.getUTCHours() + ":" : ""}${date.getUTCMinutes()}:${date.getUTCSeconds().toString().padStart(2, "0")}`;
|
|
|
|
const formatStartAt = date => `${date.getHours().toString().padStart(2, "0")}:${date.getMinutes().toString().padStart(2, "0")}`;
|
|
|
|
|
|
|
|
let cursong_startat = null;
|
|
|
|
let cursong_duration_msec = 0;
|
|
|
|
|
|
|
|
async function updateStatus() {
|
|
|
|
const resp = await fetch("/api/status");
|
|
|
|
|
|
|
|
if (!resp.ok || 200 != resp.status) {
|
|
|
|
$("radio-song").textContent =
|
|
|
|
$("radio-duration-estimate").textContent =
|
|
|
|
$("radio-duration").textContent = "";
|
2023-10-02 03:56:57 +04:00
|
|
|
$("radio-listeners").textContent =
|
2023-10-07 05:33:57 +04:00
|
|
|
$("radio-listener-peak").textContent = "0";
|
2024-05-10 00:11:33 +04:00
|
|
|
$("last-songs").lastChild.remove();
|
2023-10-07 05:33:57 +04:00
|
|
|
return [-1, null];
|
|
|
|
}
|
|
|
|
|
|
|
|
const s = await resp.json();
|
|
|
|
|
2024-05-10 00:11:33 +04:00
|
|
|
if (undefined != s.last_songs) {
|
|
|
|
$("last-songs").lastChild.remove();
|
|
|
|
$("last-songs").appendChild(document.createElement("tbody"));
|
|
|
|
for (let i = 0; i < s.last_songs.length; ++i) {
|
|
|
|
let row = $("last-songs").lastChild.insertRow();
|
|
|
|
row.insertCell().appendChild(document.createTextNode(formatStartAt(new Date(s.last_songs[i].start_at))));
|
|
|
|
row.insertCell().appendChild(document.createTextNode((s.last_songs[i].listeners == 0 ? "" : s.last_songs[i].listeners + "/") + (s.last_songs[i].max_listeners == 0 ? "" : s.last_songs[i].max_listeners)));
|
|
|
|
row.insertCell().appendChild(document.createTextNode(`${s.last_songs[i].artist} - ${s.last_songs[i].title}`));
|
|
|
|
}
|
2023-10-08 04:09:41 +04:00
|
|
|
}
|
|
|
|
|
2024-05-10 00:11:33 +04:00
|
|
|
if (undefined == s.current_song || undefined == s.current_song.duration_msec)
|
2023-10-07 05:33:57 +04:00
|
|
|
return [-1, null];
|
|
|
|
|
|
|
|
$("radio-song").textContent = `${s.current_song.artist} - ${s.current_song.title}`;
|
|
|
|
$("radio-duration").textContent = formatDuration(new Date(s.current_song.duration_msec));
|
|
|
|
$("radio-listeners").textContent = s.listeners.current;
|
|
|
|
$("radio-listener-peak").textContent = s.listeners.peak;
|
|
|
|
|
|
|
|
return [s.current_song.duration_msec, new Date(s.current_song.start_at)];
|
2023-10-02 03:56:57 +04:00
|
|
|
}
|
|
|
|
|
2023-10-07 05:33:57 +04:00
|
|
|
async function update() {
|
|
|
|
if (null === cursong_startat)
|
2023-10-09 01:44:46 +04:00
|
|
|
return -1;
|
2023-10-07 05:33:57 +04:00
|
|
|
|
|
|
|
const estimate = (new Date()) - (new Date(cursong_startat));
|
|
|
|
if (estimate >= cursong_duration_msec) {
|
2023-10-09 01:44:46 +04:00
|
|
|
return 1;
|
2023-10-07 05:33:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$("radio-duration-estimate").textContent = `${formatDuration(new Date(estimate))} / `;
|
2023-10-09 01:44:46 +04:00
|
|
|
return 0;
|
2023-10-07 05:33:57 +04:00
|
|
|
}
|
2022-03-08 01:17:24 +04:00
|
|
|
|
2023-10-07 05:33:57 +04:00
|
|
|
$("radio-update").addEventListener("click", async () =>
|
|
|
|
[cursong_duration_msec, cursong_startat] = await updateStatus());
|
2022-03-08 01:17:24 +04:00
|
|
|
|
|
|
|
|
2023-10-07 05:33:57 +04:00
|
|
|
let update_interval_id = null;
|
|
|
|
async function interval() {
|
2023-10-09 01:44:46 +04:00
|
|
|
switch (await update()) {
|
|
|
|
case 1:
|
|
|
|
[cursong_duration_msec, cursong_startat] = await updateStatus();
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
clearInterval(update_interval_id);
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
|
|
[cursong_duration_msec, cursong_startat] = await updateStatus();
|
|
|
|
update_interval_id = setInterval(interval, 1000);
|
2023-10-07 05:33:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-07 21:30:19 +04:00
|
|
|
updateStatus().then(r => [cursong_duration_msec, cursong_startat] = r);
|
2023-10-07 05:33:57 +04:00
|
|
|
update_interval_id = setInterval(interval, 1000);
|
2022-03-08 01:17:24 +04:00
|
|
|
|
|
|
|
|
2023-08-20 01:20:28 +04:00
|
|
|
|
2023-08-22 16:50:27 +04:00
|
|
|
const audio = document.getElementsByTagName("audio")[0];
|
|
|
|
audio.hidden = true;
|
|
|
|
const audio_src = audio.childNodes[0].src;
|
|
|
|
|
2023-08-22 04:37:17 +04:00
|
|
|
const volume = $("radio-volume");
|
2024-05-10 00:11:33 +04:00
|
|
|
volume.value = +(localStorage.getItem("volume") || 50) * 100.0;
|
2023-08-22 16:50:27 +04:00
|
|
|
audio.volume = volume.value / 100.0;
|
2024-05-10 00:11:33 +04:00
|
|
|
volume.addEventListener("input", e => {
|
|
|
|
audio.volume = e.target.value / 100.0;
|
|
|
|
localStorage.setItem("volume", audio.volume); });
|
2023-08-20 01:20:28 +04:00
|
|
|
|
2023-08-22 16:50:27 +04:00
|
|
|
$("player").style.display = $("player").firstChild.style.display = "flex";
|
2023-08-22 03:39:29 +04:00
|
|
|
|
2023-08-22 04:37:17 +04:00
|
|
|
$("radio-play").addEventListener("click", e => {
|
2023-08-22 16:50:27 +04:00
|
|
|
audio.paused ? (audio.src = audio_src) && audio.play() : audio.src = "";
|
2023-08-20 02:23:00 +04:00
|
|
|
e.target.style.maskImage = e.target.style.webkitMaskImage = audio.paused ?
|
2023-08-20 02:19:04 +04:00
|
|
|
"url(/assets/img/play.svg)" : "url(/assets/img/stop.svg)"; });
|