mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2024-11-10 09:07:45 +01:00
Added docs and removed reflection. Also removed inheritance.
This commit is contained in:
parent
a2e9688418
commit
d0e7213cc4
1 changed files with 19 additions and 14 deletions
33
SB.js
33
SB.js
|
@ -4,10 +4,8 @@ Map.prototype.toJSON = function() {
|
|||
return Array.from(this.entries());
|
||||
};
|
||||
|
||||
class MapIO extends Map {
|
||||
class MapIO {
|
||||
constructor(id) {
|
||||
super();
|
||||
|
||||
this.id = id;
|
||||
this.map = SB.localconfig[this.id];
|
||||
}
|
||||
|
@ -57,27 +55,31 @@ function storeEncode(data) {
|
|||
return JSON.stringify(data);
|
||||
}
|
||||
|
||||
function mapDecode(data, key) {
|
||||
/**
|
||||
* A Map cannot be stored in the chrome storage.
|
||||
* This data will be decoded from the array it is stored in
|
||||
*
|
||||
* @param {*} data
|
||||
*/
|
||||
function decodeStoredItem(data) {
|
||||
if(typeof data !== "string") return data;
|
||||
|
||||
try {
|
||||
let str = JSON.parse(data);
|
||||
let str = JSON.parse(data);
|
||||
|
||||
if(!Array.isArray(str)) return data;
|
||||
return new Map(str);
|
||||
} catch(e) {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
function mapProxy(data, key) {
|
||||
if(!(data instanceof Map)) return data;
|
||||
return new MapIO(key);
|
||||
// If all else fails, return the data
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
function configProxy() {
|
||||
chrome.storage.onChanged.addListener((changes, namespace) => {
|
||||
for (key in changes) {
|
||||
Reflect.set(SB.localconfig, key, mapDecode(changes[key].newValue, key));
|
||||
SB.localconfig[key] = decodeStoredItem(changes[key].newValue);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -88,7 +90,10 @@ function configProxy() {
|
|||
});
|
||||
},
|
||||
get: function(obj, prop) {
|
||||
return obj[prop] || mapProxy(Reflect.get(SB.localconfig, prop), prop);
|
||||
let data = SB.localconfig[prop];
|
||||
if(data instanceof Map) data = new MapIO(prop);
|
||||
|
||||
return obj[prop] || data;
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -145,7 +150,7 @@ function resetConfig() {
|
|||
|
||||
function convertJson() {
|
||||
Object.keys(SB.defaults).forEach(key => {
|
||||
SB.localconfig[key] = mapDecode(SB.localconfig[key], key);
|
||||
SB.localconfig[key] = decodeStoredItem(SB.localconfig[key], key);
|
||||
});
|
||||
}
|
||||
// Add defaults
|
||||
|
|
Loading…
Reference in a new issue