This commit is contained in:
PlOszukiwacz 2025-02-01 17:25:09 +01:00
parent 6796c5e10d
commit fb3d89a0bf
Signed by: ploszukiwacz
GPG key ID: E059F5C88034386C
4 changed files with 810 additions and 2 deletions

View file

@ -10,6 +10,7 @@ export class SettingsManager {
init() {
this.attachEventListeners();
this.loadSavedSettings();
this.setupDataManagement();
}
attachEventListeners() {
@ -70,6 +71,78 @@ export class SettingsManager {
customStyle.textContent = customCSSEditor.value;
document.head.appendChild(customStyle);
}
setupDataManagement() {
document.getElementById('exportData').addEventListener('click', () => this.exportAllData());
document.getElementById('importData').addEventListener('click', () => {
document.getElementById('importDataFile').click();
});
document.getElementById('importDataFile').addEventListener('change', (e) => {
if (e.target.files[0]) this.importAllData(e.target.files[0]);
});
}
async exportAllData() {
const data = {
version: '1.0.0',
exportDate: new Date().toISOString(),
settings: {
theme: localStorage.getItem('theme'),
layout: localStorage.getItem('layout'),
font: localStorage.getItem('font'),
linkSize: localStorage.getItem('linkSize'),
username: localStorage.getItem('username'),
background: localStorage.getItem('background'),
customCSS: localStorage.getItem('customCSS')
},
links: JSON.parse(localStorage.getItem('customLinks') || '{}'),
shortcuts: JSON.parse(localStorage.getItem('shortcuts') || '{}'),
searchEngines: JSON.parse(localStorage.getItem('searchEngines') || '[]'),
customIcons: JSON.parse(localStorage.getItem('customIcons') || '{}')
};
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `startpage-backup-${new Date().toISOString().split('T')[0]}.json`;
a.click();
URL.revokeObjectURL(url);
}
async importAllData(file) {
try {
const text = await file.text();
const data = JSON.parse(text);
if (!data.version) {
throw new Error('Invalid backup file format');
}
// Confirm import
if (!confirm('This will override all your current settings. Continue?')) {
return;
}
// Import settings
Object.entries(data.settings).forEach(([key, value]) => {
if (value !== null) localStorage.setItem(key, value);
});
// Import data
localStorage.setItem('customLinks', JSON.stringify(data.links));
localStorage.setItem('shortcuts', JSON.stringify(data.shortcuts));
localStorage.setItem('searchEngines', JSON.stringify(data.searchEngines));
localStorage.setItem('customIcons', JSON.stringify(data.customIcons));
// Reload page to apply changes
alert('Settings imported successfully. The page will now reload.');
window.location.reload();
} catch (error) {
console.error('Failed to import data:', error);
alert('Failed to import settings. Make sure the file is a valid backup.');
}
}
}
class ThemeManager {