From bec7ed6da6e4e6077066a81163e76b547d2c1bae Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Wed, 27 Oct 2021 19:57:15 +0200 Subject: [PATCH] Fix loading of themes from external sources --- Logic/DetermineLayout.ts | 18 ++++++++---------- Models/ThemeConfig/LayoutConfig.ts | 1 - index.ts | 2 -- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Logic/DetermineLayout.ts b/Logic/DetermineLayout.ts index 9af285952e..476131732d 100644 --- a/Logic/DetermineLayout.ts +++ b/Logic/DetermineLayout.ts @@ -18,7 +18,6 @@ export default class DetermineLayout { */ public static async GetLayout(): Promise<[LayoutConfig, string]> { - const loadCustomThemeParam = QueryParameters.GetQueryParameter("userlayout", "false", "If not 'false', a custom (non-official) theme is loaded. This custom layout can be done in multiple ways: \n\n- The hash of the URL contains a base64-encoded .json-file containing the theme definition\n- The hash of the URL contains a lz-compressed .json-file, as generated by the custom theme generator\n- The parameter itself is an URL, in which case that URL will be downloaded. It should point to a .json of a theme") const layoutFromBase64 = decodeURIComponent(loadCustomThemeParam.data); @@ -73,17 +72,13 @@ export default class DetermineLayout { try { - const data = await Utils.downloadJson(link) + const parsed = await Utils.downloadJson(link) + console.log("Got ", parsed) try { - let parsed = data; - if (typeof parsed == "string") { - parsed = JSON.parse(parsed); - } - // Overwrite the id to the url parsed.id = link; - return new LayoutConfig(parsed, false).patchImages(link, data); + return new LayoutConfig(parsed, false).patchImages(link, JSON.stringify(parsed)); } catch (e) { - + console.error(e) DetermineLayout.ShowErrorOnCustomTheme( `${link} is invalid:`, new FixedUiElement(e) @@ -92,6 +87,7 @@ export default class DetermineLayout { } } catch (e) { + console.erorr(e) DetermineLayout.ShowErrorOnCustomTheme( `${link} is invalid - probably not found or invalid JSON:`, new FixedUiElement(e) @@ -107,7 +103,7 @@ export default class DetermineLayout { try { // layoutFromBase64 contains the name of the theme. This is partly to do tracking with goat counter const dedicatedHashFromLocalStorage = LocalStorageSource.Get( - "user-layout-" + userLayoutParam.data.replace(" ", "_") + "user-layout-" + userLayoutParam.data?.replace(" ", "_") ); if (dedicatedHashFromLocalStorage.data?.length < 10) { dedicatedHashFromLocalStorage.setData(undefined); @@ -134,6 +130,7 @@ export default class DetermineLayout { try { json = JSON.parse(Utils.UnMinify(LZString.decompressFromBase64(hash))) } catch (e) { + console.error(e) DetermineLayout.ShowErrorOnCustomTheme("Could not decode the hash", new FixedUiElement("Not a valid (LZ-compressed) JSON")) return null; } @@ -143,6 +140,7 @@ export default class DetermineLayout { userLayoutParam.setData(layoutToUse.id); return [layoutToUse, btoa(Utils.MinifyJSON(JSON.stringify(json)))]; } catch (e) { + console.error(e) if (hash === undefined || hash.length < 10) { DetermineLayout.ShowErrorOnCustomTheme("Could not load a theme from the hash", new FixedUiElement("Hash does not contain data")) } diff --git a/Models/ThemeConfig/LayoutConfig.ts b/Models/ThemeConfig/LayoutConfig.ts index 1d89a104d7..54e2a64543 100644 --- a/Models/ThemeConfig/LayoutConfig.ts +++ b/Models/ThemeConfig/LayoutConfig.ts @@ -304,7 +304,6 @@ export default class LayoutConfig { } rewriting.forEach((value, key) => { console.log("Rewriting", key, "==>", value) - originalJson = originalJson.replace(new RegExp(key, "g"), value) }) return new LayoutConfig(JSON.parse(originalJson), false, "Layout rewriting") diff --git a/index.ts b/index.ts index 92ba9da66b..a383eaac26 100644 --- a/index.ts +++ b/index.ts @@ -33,8 +33,6 @@ if (location.href.startsWith("http://buurtnatuur.be")) { class Init { - - public static Init(layoutToUse: LayoutConfig, encoded: string) { if(layoutToUse === null){