89 lines
No EOL
3 KiB
JavaScript
89 lines
No EOL
3 KiB
JavaScript
/**
|
|
* meta-handler.js
|
|
* Verwaltet das Abrufen und Anzeigen von Stream-Metadaten (Songtitel)
|
|
*/
|
|
|
|
const MetaHandler = {
|
|
proxyPath: 'metadata.php',
|
|
updateInterval: 20000,
|
|
intervalId: null,
|
|
proxyActive: false,
|
|
|
|
async init() {
|
|
try {
|
|
const response = await fetch(this.proxyPath, { method: 'HEAD' });
|
|
this.proxyActive = response.ok;
|
|
} catch (e) {
|
|
this.proxyActive = false;
|
|
}
|
|
},
|
|
|
|
async fetchTitle(streamUrl) {
|
|
if (!this.proxyActive || !streamUrl) return;
|
|
try {
|
|
const res = await fetch(`${this.proxyPath}?url=${encodeURIComponent(streamUrl)}`);
|
|
const data = await res.json();
|
|
if (data && data.title) {
|
|
this.updateUI(data.title);
|
|
}
|
|
} catch (e) {
|
|
console.error("Metadaten-Fehler:", e);
|
|
}
|
|
},
|
|
|
|
updateUI(title) {
|
|
const stationDisplay = document.getElementById('current-station-name');
|
|
if (!stationDisplay) return;
|
|
|
|
// 1. Alles vor dem Songtitel isolieren
|
|
let currentContent = stationDisplay.innerHTML;
|
|
|
|
// Wir suchen nach dem Icon oder dem geschützten Leerzeichen und schneiden dort ab
|
|
let stationPart = currentContent.split(' ')[0].split(' <i class="fa-solid fa-music"')[0].trim();
|
|
|
|
// 2. Dubletten-Check
|
|
const tempDiv = document.createElement("div");
|
|
tempDiv.innerHTML = stationPart;
|
|
const stationNameOnly = tempDiv.textContent.trim();
|
|
|
|
const cleanTitle = title.toLowerCase();
|
|
const cleanStation = stationNameOnly.toLowerCase();
|
|
|
|
if (cleanTitle === cleanStation || cleanTitle.includes(cleanStation) || title.includes('0848')) {
|
|
stationDisplay.innerHTML = stationPart;
|
|
return;
|
|
}
|
|
|
|
// 3. Anzeige neu zusammenbauen (die Leerzeichen werden hier jedes Mal neu gesetzt, nicht addiert)
|
|
stationDisplay.innerHTML = `${stationPart} <i class="fa-solid fa-music"></i> ${title}`;
|
|
},
|
|
|
|
start(streamUrl) {
|
|
this.stop();
|
|
this.fetchTitle(streamUrl);
|
|
|
|
this.intervalId = setInterval(() => {
|
|
this.fetchTitle(streamUrl);
|
|
}, this.updateInterval);
|
|
},
|
|
|
|
stop() {
|
|
if (this.intervalId) {
|
|
clearInterval(this.intervalId);
|
|
this.intervalId = null;
|
|
}
|
|
|
|
const stationDisplay = document.getElementById('current-station-name');
|
|
if (stationDisplay) {
|
|
// Beim Stoppen alles nach dem Sender-Teil entfernen
|
|
if (stationDisplay.innerHTML.includes(' ')) {
|
|
stationDisplay.innerHTML = stationDisplay.innerHTML.split(' ')[0].trim();
|
|
} else if (stationDisplay.innerHTML.includes('<i class="fa-solid fa-music"')) {
|
|
stationDisplay.innerHTML = stationDisplay.innerHTML.split(' <i class="fa-solid fa-music"')[0].trim();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
window.MetaHandler = MetaHandler;
|
|
MetaHandler.init(); |