Update preset-manager.js

This commit is contained in:
simonpipe 2025-12-18 22:18:22 +01:00
parent a9fe23cf5e
commit bfae0c5c79

View file

@ -2,19 +2,32 @@
window.PresetManager = {
presets: [],
// Lädt Presets aus LocalStorage oder der JSON-Datei
// Lädt Presets: URL-Hash hat Vorrang vor LocalStorage
async load(callback) {
this.presets = JSON.parse(localStorage.getItem('presets')) || [];
if (this.presets.length === 0) {
// 1. Check: Ist ein Hash in der URL vorhanden?
const hash = window.location.hash.substring(1);
if (hash && hash.length > 30) {
const success = await this.importFromString(hash, false); // false = kein Reload
if (success) {
if (callback) callback(this.presets);
return;
}
}
// 2. Check: LocalStorage laden
const stored = localStorage.getItem('presets');
if (stored) {
try {
const response = await fetch('webradio_presets.json');
if (!response.ok) throw new Error('Datei konnte nicht geladen werden');
this.presets = await response.json();
this.save();
} catch (error) {
if (window.UI) window.UI.showAlert("Fehler", 'Fehler beim Laden der Presets: ' + error.message);
this.presets = JSON.parse(stored);
} catch (e) {
console.error("Fehler beim Parsen von LocalStorage", e);
this.presets = [];
}
} else {
// 3. Fallback: Leere Liste, wenn gar nichts gefunden wurde
this.presets = [];
}
if (callback) callback(this.presets);
},
@ -22,10 +35,10 @@ window.PresetManager = {
localStorage.setItem('presets', JSON.stringify(this.presets));
},
// --- EXPORT/IMPORT (Gzip/Base64) ---
async generateExportCode() {
// Generiert den Code und gibt ihn zurück
async generateExportCode(updateField = true) {
try {
if (!this.presets || this.presets.length === 0) return;
if (!this.presets || this.presets.length === 0) return "";
const compactData = this.presets.map(p => [p.name, p.url]);
const jsonString = JSON.stringify(compactData);
@ -35,14 +48,19 @@ window.PresetManager = {
const buffer = await response.arrayBuffer();
const base64String = btoa(String.fromCharCode(...new Uint8Array(buffer)));
if (updateField) {
const exportField = document.getElementById('export-string-field');
if (exportField) exportField.value = base64String;
}
return base64String;
} catch (e) {
console.error("Export-Fehler:", e);
return "";
}
},
async importFromString(code) {
// Importiert von einem String
async importFromString(code, shouldReload = true) {
try {
const binary = atob(code);
const bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
@ -54,10 +72,15 @@ window.PresetManager = {
const importedArray = JSON.parse(resultText);
this.presets = importedArray.map(item => ({ name: item[0], url: item[1] }));
this.save();
location.reload();
if (shouldReload) {
// Seite ohne den langen Hash neu laden für eine saubere URL
location.href = window.location.origin + window.location.pathname;
}
return true;
} catch (e) {
console.error("Import-Fehler:", e);
if (window.UI) window.UI.showAlert("Fehler", "Code ungültig oder beschädigt!");
return false;
}
},
@ -68,9 +91,7 @@ window.PresetManager = {
const a = document.createElement('a');
a.href = url;
a.download = 'radio_presets.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
};