97 lines
No EOL
3.5 KiB
JavaScript
97 lines
No EOL
3.5 KiB
JavaScript
// preset-manager.js - Verwaltung der Radio-Sender
|
|
window.PresetManager = {
|
|
presets: [],
|
|
|
|
// Lädt Presets: URL-Hash hat Vorrang vor LocalStorage
|
|
async load(callback) {
|
|
// 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 {
|
|
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);
|
|
},
|
|
|
|
save() {
|
|
localStorage.setItem('presets', JSON.stringify(this.presets));
|
|
},
|
|
|
|
// Generiert den Code und gibt ihn zurück
|
|
async generateExportCode(updateField = true) {
|
|
try {
|
|
if (!this.presets || this.presets.length === 0) return "";
|
|
const compactData = this.presets.map(p => [p.name, p.url]);
|
|
const jsonString = JSON.stringify(compactData);
|
|
|
|
const stream = new Blob([jsonString]).stream();
|
|
const compressedStream = stream.pipeThrough(new CompressionStream('gzip'));
|
|
const response = new Response(compressedStream);
|
|
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 "";
|
|
}
|
|
},
|
|
|
|
// Importiert von einem String
|
|
async importFromString(code, shouldReload = true) {
|
|
try {
|
|
const binary = atob(code);
|
|
const bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
|
|
const stream = new Blob([bytes]).stream();
|
|
const decompressedStream = stream.pipeThrough(new DecompressionStream('gzip'));
|
|
const response = new Response(decompressedStream);
|
|
const resultText = await response.text();
|
|
|
|
const importedArray = JSON.parse(resultText);
|
|
this.presets = importedArray.map(item => ({ name: item[0], url: item[1] }));
|
|
this.save();
|
|
|
|
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);
|
|
return false;
|
|
}
|
|
},
|
|
|
|
exportToFile() {
|
|
const presetsJSON = JSON.stringify(this.presets, null, 2);
|
|
const blob = new Blob([presetsJSON], { type: 'application/json' });
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = 'radio_presets.json';
|
|
a.click();
|
|
URL.revokeObjectURL(url);
|
|
}
|
|
}; |