diff --git a/src/background.ts b/src/background.ts index 62d0827e..330481e7 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1,5 +1,8 @@ import * as Types from "./types"; + import Config from "./config"; +// Make the config public for debugging purposes +( window).SB = Config; import Utils from "./utils"; var utils = new Utils({ diff --git a/src/config.ts b/src/config.ts index 635fba55..47d99fcf 100644 --- a/src/config.ts +++ b/src/config.ts @@ -84,13 +84,8 @@ class SBMap extends Map { return result; } - - toJSON() { - return Array.from(this.entries()); - } } - var Config: SBObject = { /** * Callback function when an option is updated @@ -131,14 +126,14 @@ var Config: SBObject = { /** * A SBMap cannot be stored in the chrome storage. - * This data will be encoded into an array instead as specified by the toJSON function. + * This data will be encoded into an array instead * * @param data */ function encodeStoredItem(data) { // if data is SBMap convert to json for storing if(!(data instanceof SBMap)) return data; - return JSON.stringify(data); + return Array.from(data.entries()); } /** @@ -148,18 +143,30 @@ function encodeStoredItem(data) { * @param {*} data */ function decodeStoredItem(id: string, data) { - if(typeof data !== "string") return data; - - try { - let str = JSON.parse(data); - - if(!Array.isArray(str)) return data; - return new SBMap(id, str); - } catch(e) { + if (!Config.defaults[id]) return data; - // If all else fails, return the data - return data; + if (Config.defaults[id] instanceof SBMap) { + try { + let jsonData: any = data; + + // Check if data is stored in the old format for SBMap (a JSON string) + if (typeof data === "string") { + try { + jsonData = JSON.parse(data); + } catch(e) { + // Continue normally (out of this if statement) + } + } + + if (!Array.isArray(jsonData)) return data; + return new SBMap(id, jsonData); + } catch(e) { + console.error("Failed to parse SBMap: " + id); + } } + + // If all else fails, return the data + return data; } function configProxy(): any { diff --git a/src/options.ts b/src/options.ts index 1836c6c3..0836090c 100644 --- a/src/options.ts +++ b/src/options.ts @@ -1,4 +1,6 @@ import Config from "./config"; +// Make the config public for debugging purposes +( window).SB = Config; import Utils from "./utils"; var utils = new Utils();