Map storage encoding

This commit is contained in:
Official Noob
2020-01-08 22:22:18 +00:00
committed by GitHub
parent bfa0472f84
commit a314139302

23
SB.js
View File

@@ -1,5 +1,22 @@
SB = {};
Map.prototype.toJSON = function() {
return Array.from(this.entries());
};
function storeEncode(data) {
if(!(data instanceof Map)) return data;
return JSON.stringify(data);
}
function strParser(data) {
try {
return new Map(JSON.parse(data));
} catch(e) {
return data
}
}
function configProxy() {
chrome.storage.onChanged.addListener((changes, namespace) => {
for (key in changes) {
@@ -9,11 +26,11 @@ function configProxy() {
var handler = {
set: function(obj, prop, value) {
chrome.storage.sync.set({
[prop]: value
[prop]: storeEncode(value)
});
},
get: function(obj, prop) {
return Reflect.get(SB.localconfig, prop);
return strParser(Reflect.get(SB.localconfig, prop));
}
};
@@ -42,8 +59,6 @@ async function config() {
addDefaults();
SB.config = configProxy();
migrate();
}
SB.defaults = {