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

25
SB.js
View File

@@ -1,19 +1,36 @@
SB = {}; 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() { function configProxy() {
chrome.storage.onChanged.addListener((changes, namespace) => { chrome.storage.onChanged.addListener((changes, namespace) => {
for (key in changes) { for (key in changes) {
Reflect.set(SB.localconfig, key, changes[key].newValue); Reflect.set(SB.localconfig, key, changes[key].newValue);
} }
}); });
var handler = { var handler = {
set: function(obj, prop, value) { set: function(obj, prop, value) {
chrome.storage.sync.set({ chrome.storage.sync.set({
[prop]: value [prop]: storeEncode(value)
}); });
}, },
get: function(obj, prop) { 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(); addDefaults();
SB.config = configProxy(); SB.config = configProxy();
migrate(); migrate();
} }
SB.defaults = { SB.defaults = {