open-story-teller/story-player-web/classes/storage.js
anthony@rabine.fr 0ad614699c
Some checks failed
build-story-editor / build_linux (push) Has been cancelled
build-story-editor / build_win32 (push) Has been cancelled
Deploy / deploy (push) Has been cancelled
dedicated directory for story web version
2025-01-10 22:04:39 +01:00

48 lines
1.2 KiB
JavaScript

class Storage {
constructor(prefix = '') {
this.prefix = prefix;
}
setItem(key, value) {
try {
const data = JSON.stringify(value);
localStorage.setItem(this.prefix + key, data);
} catch (error) {
console.error('Error saving to localStorage', error);
}
}
getItem(key) {
try {
const data = localStorage.getItem(this.prefix + key);
return data ? JSON.parse(data) : null;
} catch (error) {
console.error('Error reading from localStorage', error);
return null;
}
}
removeItem(key) {
try {
localStorage.removeItem(this.prefix + key);
} catch (error) {
console.error('Error removing from localStorage', error);
}
}
clear() {
try {
const keys = Object.keys(localStorage);
keys.forEach(key => {
if (key.startsWith(this.prefix)) {
localStorage.removeItem(key);
}
});
} catch (error) {
console.error('Error clearing localStorage', error);
}
}
}
// Exemple d'utilisation
export default new Storage('ost_player_v1_');