mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-26 08:51:20 -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 {
|
export default class Storage {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.name = 'artplayer_settings';
|
this.name = 'artplayer_settings';
|
||||||
|
this.settings = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
get(key) {
|
get(key) {
|
||||||
const storage = JSON.parse(localStorage.getItem(this.name)) || {};
|
try {
|
||||||
return key ? storage[key] : storage;
|
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) {
|
set(key, value) {
|
||||||
const storage = Object.assign({}, this.get(), {
|
try {
|
||||||
[key]: value,
|
const storage = Object.assign({}, this.get(), {
|
||||||
});
|
[key]: value,
|
||||||
localStorage.setItem(this.name, JSON.stringify(storage));
|
});
|
||||||
|
window.localStorage.setItem(this.name, JSON.stringify(storage));
|
||||||
|
} catch (error) {
|
||||||
|
this.settings[key] = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
del(key) {
|
del(key) {
|
||||||
const storage = this.get();
|
try {
|
||||||
delete storage[key];
|
const storage = this.get();
|
||||||
localStorage.setItem(this.name, JSON.stringify(storage));
|
delete storage[key];
|
||||||
|
window.localStorage.setItem(this.name, JSON.stringify(storage));
|
||||||
|
} catch (error) {
|
||||||
|
delete this.settings[key];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clean() {
|
clean() {
|
||||||
localStorage.removeItem(this.name);
|
try {
|
||||||
|
window.localStorage.removeItem(this.name);
|
||||||
|
} catch (error) {
|
||||||
|
this.settings = {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user