From 8896c5707a2e430fb331c1823d8d05f5da76120f Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Tue, 4 Feb 2020 22:16:40 +0000 Subject: [PATCH 1/7] Made decodeStoredItem detect item type Not tested because SB.config cant be used anymore :( --- src/config.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/config.ts b/src/config.ts index a77a3807..ddf13fff 100644 --- a/src/config.ts +++ b/src/config.ts @@ -128,7 +128,7 @@ var Config: SBObject = { 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()); } /** @@ -138,18 +138,19 @@ 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.localConfig[id]) return data; - // If all else fails, return the data - return data; + if(Config.localConfig[id] instanceof SBMap) { + try { + if(!Array.isArray(data)) return data; + return new SBMap(id, data); + } catch(e) { + console.error("Failed to parse SBMap: "+ id); + } } + + // If all else fails, return the data + return data; } function configProxy(): any { @@ -240,4 +241,4 @@ function addDefaults() { // Sync config setupConfig(); -export default Config; \ No newline at end of file +export default Config; From 1c17464c94265e9a4f9fa0f43c5890c650c43169 Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Tue, 4 Feb 2020 23:29:11 +0000 Subject: [PATCH 2/7] config => defaults --- src/config.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index ddf13fff..411c9732 100644 --- a/src/config.ts +++ b/src/config.ts @@ -138,9 +138,9 @@ function encodeStoredItem(data) { * @param {*} data */ function decodeStoredItem(id: string, data) { - if(!Config.localConfig[id]) return data; + if(!Config.defaults[id]) return data; - if(Config.localConfig[id] instanceof SBMap) { + if(Config.defaults[id] instanceof SBMap) { try { if(!Array.isArray(data)) return data; return new SBMap(id, data); From be3a4a4e918cbaef44891c6c1ffb3f5582df8dce Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 8 Feb 2020 20:08:34 -0500 Subject: [PATCH 3/7] Added support for old format. --- src/config.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/config.ts b/src/config.ts index 411c9732..b2eda3be 100644 --- a/src/config.ts +++ b/src/config.ts @@ -148,6 +148,15 @@ function decodeStoredItem(id: string, data) { console.error("Failed to parse SBMap: "+ id); } } + + // This is the old format for SBMap (a JSON string) + if (typeof data === "string") { + let str = JSON.parse(data); + + if(Array.isArray(data)) { + return new SBMap(id, str) + } + } // If all else fails, return the data return data; From 94af8ab301fb81ee7a9c15b8b54affd3488695ee Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 8 Feb 2020 20:15:49 -0500 Subject: [PATCH 4/7] Prevent all strings from being parsed as JSON. --- src/config.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/config.ts b/src/config.ts index b2eda3be..8fb2802d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -84,7 +84,6 @@ class SBMap extends Map { } } - var Config: SBObject = { /** * Callback function when an option is updated @@ -138,11 +137,11 @@ function encodeStoredItem(data) { * @param {*} data */ function decodeStoredItem(id: string, data) { - if(!Config.defaults[id]) return data; + if (!Config.defaults[id]) return data; - if(Config.defaults[id] instanceof SBMap) { + if (Config.defaults[id] instanceof SBMap) { try { - if(!Array.isArray(data)) return data; + if (!Array.isArray(data)) return data; return new SBMap(id, data); } catch(e) { console.error("Failed to parse SBMap: "+ id); @@ -151,10 +150,14 @@ function decodeStoredItem(id: string, data) { // This is the old format for SBMap (a JSON string) if (typeof data === "string") { - let str = JSON.parse(data); + try { + let str = JSON.parse(data); - if(Array.isArray(data)) { - return new SBMap(id, str) + if (Array.isArray(data)) { + return new SBMap(id, str); + } + } catch(e) { + // Continue normally (out of this if statement) } } From 8cdbebd6de1db74b7ac0b08b183201ce72ddb032 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 8 Feb 2020 20:16:26 -0500 Subject: [PATCH 5/7] Added the config as a global variable. --- src/config.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/config.ts b/src/config.ts index 8fb2802d..7ad42afd 100644 --- a/src/config.ts +++ b/src/config.ts @@ -254,3 +254,6 @@ function addDefaults() { setupConfig(); export default Config; + +// Make the config public for debugging purposes +( window).SB = Config; \ No newline at end of file From 88a8fda566da1e5bd7c8c83e9ac50b0519274c61 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Fri, 14 Feb 2020 23:16:01 -0500 Subject: [PATCH 6/7] Moved window.SB creation for security reasons. --- src/background.ts | 3 +++ src/config.ts | 5 +---- src/options.ts | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/background.ts b/src/background.ts index 89516287..304902ad 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 555aadcd..7fa11084 100644 --- a/src/config.ts +++ b/src/config.ts @@ -263,7 +263,4 @@ function addDefaults() { // Sync config setupConfig(); -export default Config; - -// Make the config public for debugging purposes -( window).SB = Config; \ No newline at end of file +export default Config; \ No newline at end of file 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(); From 8d82a6a3e662c46e1004120d441455ed211ba25b Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Fri, 14 Feb 2020 23:20:11 -0500 Subject: [PATCH 7/7] Fixed data old format migration. --- src/config.ts | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/config.ts b/src/config.ts index 7fa11084..abbd202a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -84,10 +84,6 @@ class SBMap extends Map { return result; } - - toJSON() { - return Array.from(this.entries()); - } } var Config: SBObject = { @@ -130,7 +126,7 @@ 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 */ @@ -151,26 +147,24 @@ function decodeStoredItem(id: string, data) { if (Config.defaults[id] instanceof SBMap) { try { - if (!Array.isArray(data)) return data; - return new SBMap(id, data); - } catch(e) { - console.error("Failed to parse SBMap: "+ id); - } - } + let jsonData: any = data; - // This is the old format for SBMap (a JSON string) - if (typeof data === "string") { - try { - let str = JSON.parse(data); - - if (Array.isArray(data)) { - return new SBMap(id, str); + // 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) { - // Continue normally (out of this if statement) + console.error("Failed to parse SBMap: " + id); } } - + // If all else fails, return the data return data; }