mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-08 19:06:15 -08:00
Update storage.js
This commit is contained in:
1 parent
97dbdf16a5
commit
324672a243
1 file changed
+27
-10
@@ -1,27 +1,44 @@
|
||||
export default class Storage {
|
||||
constructor() {
|
||||
this.name = 'artplayer_settings';
|
||||
this.settings = {};
|
||||
}
|
||||
|
||||
get(key) {
|
||||
const storage = JSON.parse(localStorage.getItem(this.name)) || {};
|
||||
return key ? storage[key] : storage;
|
||||
try {
|
||||
const storage = JSON.parse(window.localStorage.getItem(this.name)) || {};
|
||||
return key ? storage[key] : storage;
|
||||
} catch (error) {
|
||||
return key ? this.settings[key] : this.settings;
|
||||
}
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
const storage = Object.assign({}, this.get(), {
|
||||
[key]: value,
|
||||
});
|
||||
localStorage.setItem(this.name, JSON.stringify(storage));
|
||||
try {
|
||||
const storage = Object.assign({}, this.get(), {
|
||||
[key]: value,
|
||||
});
|
||||
window.localStorage.setItem(this.name, JSON.stringify(storage));
|
||||
} catch (error) {
|
||||
this.settings[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
del(key) {
|
||||
const storage = this.get();
|
||||
delete storage[key];
|
||||
localStorage.setItem(this.name, JSON.stringify(storage));
|
||||
try {
|
||||
const storage = this.get();
|
||||
delete storage[key];
|
||||
window.localStorage.setItem(this.name, JSON.stringify(storage));
|
||||
} catch (error) {
|
||||
delete this.settings[key];
|
||||
}
|
||||
}
|
||||
|
||||
clean() {
|
||||
localStorage.removeItem(this.name);
|
||||
try {
|
||||
window.localStorage.removeItem(this.name);
|
||||
} catch (error) {
|
||||
this.settings = {};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user