91 lines
No EOL
3.1 KiB
JavaScript
91 lines
No EOL
3.1 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 Titel extrahieren (enthält das Sender-Icon + Name)
|
||
let currentContent = stationDisplay.innerHTML;
|
||
// Wir isolieren den Sender-Teil, indem wir alles nach dem Music-Icon oder Trenner-Span abschneiden
|
||
let stationPart = currentContent.split(' <span')[0].split(' <i class="fa-solid fa-music"')[0].trim();
|
||
|
||
// 2. Dubletten-Check (Text ohne Icons vergleichen)
|
||
const tempDiv = document.createElement("div");
|
||
tempDiv.innerHTML = stationPart;
|
||
const stationNameOnly = tempDiv.textContent.trim();
|
||
|
||
const cleanTitle = title.toLowerCase();
|
||
const cleanStation = stationNameOnly.toLowerCase();
|
||
|
||
// Filter für Dubletten und Systemmeldungen
|
||
if (cleanTitle === cleanStation || cleanTitle.includes(cleanStation) || title.includes('0848')) {
|
||
stationDisplay.innerHTML = stationPart; // Nur Sender anzeigen
|
||
return;
|
||
}
|
||
|
||
// 3. Anzeige OHNE Bindestrich zusammenbauen (mit Abstandshaltern)
|
||
stationDisplay.innerHTML = `${stationPart} <i class="fa-solid fa-music"></i> ${title}`;
|
||
},
|
||
|
||
start(streamUrl) {
|
||
this.stop();
|
||
// Erster Aufruf sofort
|
||
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) {
|
||
// Entfernt alles ab dem Musik-Icon oder dem alten Bindestrich
|
||
if (stationDisplay.innerHTML.includes('<i class="fa-solid fa-music"')) {
|
||
stationDisplay.innerHTML = stationDisplay.innerHTML.split(' <i class="fa-solid fa-music"')[0];
|
||
} else if (stationDisplay.innerHTML.includes(' – ')) {
|
||
stationDisplay.innerHTML = stationDisplay.innerHTML.split(' – ')[0];
|
||
}
|
||
}
|
||
}
|
||
};
|
||
|
||
// Global verfügbar machen
|
||
window.MetaHandler = MetaHandler;
|
||
MetaHandler.init(); |