Update preset-manager.js
This commit is contained in:
parent
a9fe23cf5e
commit
bfae0c5c79
1 changed files with 41 additions and 20 deletions
|
|
@ -2,19 +2,32 @@
|
||||||
window.PresetManager = {
|
window.PresetManager = {
|
||||||
presets: [],
|
presets: [],
|
||||||
|
|
||||||
// Lädt Presets aus LocalStorage oder der JSON-Datei
|
// Lädt Presets: URL-Hash hat Vorrang vor LocalStorage
|
||||||
async load(callback) {
|
async load(callback) {
|
||||||
this.presets = JSON.parse(localStorage.getItem('presets')) || [];
|
// 1. Check: Ist ein Hash in der URL vorhanden?
|
||||||
if (this.presets.length === 0) {
|
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 {
|
try {
|
||||||
const response = await fetch('webradio_presets.json');
|
this.presets = JSON.parse(stored);
|
||||||
if (!response.ok) throw new Error('Datei konnte nicht geladen werden');
|
} catch (e) {
|
||||||
this.presets = await response.json();
|
console.error("Fehler beim Parsen von LocalStorage", e);
|
||||||
this.save();
|
this.presets = [];
|
||||||
} catch (error) {
|
|
||||||
if (window.UI) window.UI.showAlert("Fehler", 'Fehler beim Laden der Presets: ' + error.message);
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// 3. Fallback: Leere Liste, wenn gar nichts gefunden wurde
|
||||||
|
this.presets = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (callback) callback(this.presets);
|
if (callback) callback(this.presets);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -22,10 +35,10 @@ window.PresetManager = {
|
||||||
localStorage.setItem('presets', JSON.stringify(this.presets));
|
localStorage.setItem('presets', JSON.stringify(this.presets));
|
||||||
},
|
},
|
||||||
|
|
||||||
// --- EXPORT/IMPORT (Gzip/Base64) ---
|
// Generiert den Code und gibt ihn zurück
|
||||||
async generateExportCode() {
|
async generateExportCode(updateField = true) {
|
||||||
try {
|
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 compactData = this.presets.map(p => [p.name, p.url]);
|
||||||
const jsonString = JSON.stringify(compactData);
|
const jsonString = JSON.stringify(compactData);
|
||||||
|
|
||||||
|
|
@ -35,14 +48,19 @@ window.PresetManager = {
|
||||||
const buffer = await response.arrayBuffer();
|
const buffer = await response.arrayBuffer();
|
||||||
const base64String = btoa(String.fromCharCode(...new Uint8Array(buffer)));
|
const base64String = btoa(String.fromCharCode(...new Uint8Array(buffer)));
|
||||||
|
|
||||||
|
if (updateField) {
|
||||||
const exportField = document.getElementById('export-string-field');
|
const exportField = document.getElementById('export-string-field');
|
||||||
if (exportField) exportField.value = base64String;
|
if (exportField) exportField.value = base64String;
|
||||||
|
}
|
||||||
|
return base64String;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Export-Fehler:", e);
|
console.error("Export-Fehler:", e);
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async importFromString(code) {
|
// Importiert von einem String
|
||||||
|
async importFromString(code, shouldReload = true) {
|
||||||
try {
|
try {
|
||||||
const binary = atob(code);
|
const binary = atob(code);
|
||||||
const bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
|
const bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
|
||||||
|
|
@ -54,10 +72,15 @@ window.PresetManager = {
|
||||||
const importedArray = JSON.parse(resultText);
|
const importedArray = JSON.parse(resultText);
|
||||||
this.presets = importedArray.map(item => ({ name: item[0], url: item[1] }));
|
this.presets = importedArray.map(item => ({ name: item[0], url: item[1] }));
|
||||||
this.save();
|
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) {
|
} catch (e) {
|
||||||
console.error("Import-Fehler:", 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');
|
const a = document.createElement('a');
|
||||||
a.href = url;
|
a.href = url;
|
||||||
a.download = 'radio_presets.json';
|
a.download = 'radio_presets.json';
|
||||||
document.body.appendChild(a);
|
|
||||||
a.click();
|
a.click();
|
||||||
document.body.removeChild(a);
|
|
||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Loading…
Reference in a new issue