From 2c980a269db4ebf9c6e3461dbac47ae91dda9718 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Thu, 26 Mar 2020 12:18:52 -0400 Subject: [PATCH] Fixed sponsorTimes data on export and import --- src/config.ts | 16 ++++++++++++---- src/options.ts | 12 +++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/config.ts b/src/config.ts index 47d99fcf..2a007dce 100644 --- a/src/config.ts +++ b/src/config.ts @@ -33,6 +33,10 @@ interface SBObject { defaults: SBConfig; localConfig: SBConfig; config: SBConfig; + + // Functions + encodeStoredItem(data: T): T | Array; + convertJSON(): void; } // Allows a SBMap to be conveted into json form @@ -119,7 +123,11 @@ var Config: SBObject = { mobileUpdateShowCount: 0 }, localConfig: null, - config: null + config: null, + + // Functions + encodeStoredItem, + convertJSON }; // Function setup @@ -130,7 +138,7 @@ var Config: SBObject = { * * @param data */ -function encodeStoredItem(data) { +function encodeStoredItem(data: T): T | Array { // if data is SBMap convert to json for storing if(!(data instanceof SBMap)) return data; return Array.from(data.entries()); @@ -142,7 +150,7 @@ function encodeStoredItem(data) { * * @param {*} data */ -function decodeStoredItem(id: string, data) { +function decodeStoredItem(id: string, data: T): T | SBMap { if (!Config.defaults[id]) return data; if (Config.defaults[id] instanceof SBMap) { @@ -239,7 +247,7 @@ function resetConfig() { Config.config = Config.defaults; }; -function convertJSON() { +function convertJSON(): void { Object.keys(Config.localConfig).forEach(key => { Config.localConfig[key] = decodeStoredItem(key, Config.localConfig[key]); }); diff --git a/src/options.ts b/src/options.ts index 62b55c6e..0a5373cb 100644 --- a/src/options.ts +++ b/src/options.ts @@ -379,7 +379,12 @@ function activatePrivateTextChange(element: HTMLElement) { // See if anything extra must be done switch (option) { case "*": - result = JSON.stringify(Config.localConfig); + let jsonData = JSON.parse(JSON.stringify(Config.localConfig)); + + // Fix sponsorTimes data as it is destroyed from the JSON stringify + jsonData.sponsorTimes = Config.encodeStoredItem(Config.localConfig.sponsorTimes); + + result = JSON.stringify(jsonData); break; } @@ -399,7 +404,9 @@ function activatePrivateTextChange(element: HTMLElement) { for (const key in newConfig) { Config.config[key] = newConfig[key]; } + Config.convertJSON(); + // Reload options on page init(); if (newConfig.supportInvidious) { @@ -457,6 +464,9 @@ function copyDebugOutputToClipboard() { }, config: JSON.parse(JSON.stringify(Config.localConfig)) // Deep clone config object }; + + // Fix sponsorTimes data as it is destroyed from the JSON stringify + output.config.sponsorTimes = Config.encodeStoredItem(Config.localConfig.sponsorTimes); // Sanitise sensitive user config values delete output.config.userID;