dusk-radio/script.js

71 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-12-21 16:59:54 +00:00
document.addEventListener('DOMContentLoaded', () => {
setTimeout(initialize, 500)
})
function initialize() {
let playButtons = [...document.querySelectorAll('.play')]
playButtons.map((x) => x.addEventListener('click', () => playButtonAction(x)))
let cadenaLinks = [...document.querySelectorAll('.linkButton')]
cadenaLinks.map((x) => x.addEventListener('click', () => copyLink(x)))
var cadenaPlaying = [...document.querySelectorAll('.playing')]
cadenaPlaying.map((x) => actualitzar(x))
let refreshButton = document.querySelector('#refresh')
refreshButton.addEventListener('click', () => { cadenaPlaying.map((x) => actualitzar(x))})
}
function playButtonAction(x) {
let audio = x.parentNode.parentNode.querySelector('audio')
if (audio.paused) {
audio.load()
audio.play()
x.src="./stop.svg"
}
else {
let aux = audio.querySelector('source').src
audio.querySelector('source').src = ""
audio.pause()
setTimeout(() => audio.load())
x.src="./play.svg"
audio.querySelector('source').src = aux
}
}
function copyLink(x) {
let link = x.parentNode.parentNode.querySelector('source')
let pText = x.parentNode.parentNode.querySelector('.link')
console.log(link)
console.log(pText)
pText.style.display = "unset"
pText.value = link.src
console.log(pText.value)
pText.select()
document.execCommand('copy')
pText.style.display = "none"
}
function actualitzar(x) {
actualitzarIntern(x)
}
function actualitzarIntern(x) {
fetch("status-json.xsl").then((response) => response.json())
.then((json) => {
var link = x.parentNode.querySelector('source').src
var filename = basename(link)
var title = ""
var JSONArray = Array.from(json.icestats.source)
for (let i = 0; i < JSONArray.length; i++) {
if (basename(JSONArray[i].listenurl) === filename) {
title = "♪ " + JSONArray[i].title
}
}
x.parentNode.querySelector('.playing').innerHTML = title
})
}
function basename (str) {
return str.substring(str.lastIndexOf('/') + 1)
}