From 7ee3a6065ca9db6fc31b54cd917bdc52a223ae0e Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Sat, 20 Feb 2021 02:37:47 +0100 Subject: [PATCH] Add loading from a random URL; fixes #208 --- UI/CustomGenerator/SharePanel.ts | 30 +++++------------------------- index.ts | 30 +++++++++--------------------- 2 files changed, 14 insertions(+), 46 deletions(-) diff --git a/UI/CustomGenerator/SharePanel.ts b/UI/CustomGenerator/SharePanel.ts index db479ecd8..bc30696cb 100644 --- a/UI/CustomGenerator/SharePanel.ts +++ b/UI/CustomGenerator/SharePanel.ts @@ -22,31 +22,11 @@ export default class SharePanel extends UIElement { "

Share

", "Share the following link with friends:
", new VariableUiElement(liveUrl.map(url => `${url}`)), - "

Publish on OSM Wiki

", - "In the list of 'hacks upon hacks to make MapComplete work', it is now possible to put the JSON-file onto a Wikipage*.
" + - "This is a very stable and very well-tested solution. Using wikipages as source control! What could possibly go wrong???? /s

", - - "Why to publish the layout as a wikipage?", - "", - - "In order to make this work:", - "
    ", - - ...[`Create a new page on the OSM-wiki, e.g. ${proposedName}`, - "Click 'create page'", - "Type <nowiki>, a few newlines and </nowiki>", - "Copy the json configuration from the 'save-tab', paste it between the 'nowiki'-tags in the Wiki", - "Click 'save' to save the wiki page", - "Share the link with the url parameter userlayout=wiki:YOURWIKIPAGE, e.g. " + - `https://${window.location.host}?userlayout=${proposedNameEnc}` - ].map(li => `
  1. ${li}
  2. `), - - "
", - "(* This has made a lot of people very angry and been widely regarded as a bad move.)", + "

Publish on some website

", + + "It is possible to load a JSON-file from the wide internet, but you'll need some (public CORS-enabled) server.", + `Put the raw json online, and use ${window.location.host}?userlayout=https://.json`, + "Please note: it used to be possible to load from the wiki - this is not possible anymore due to technical reasons.", "" ]); } diff --git a/index.ts b/index.ts index 48eb22d35..0a912a38d 100644 --- a/index.ts +++ b/index.ts @@ -64,36 +64,24 @@ document.getElementById('centermessage').innerText = ''; document.getElementById("decoration-desktop").remove(); -if (layoutFromBase64.startsWith("wiki:")) { - console.log("Downloading map theme from the wiki"); - const themeName = layoutFromBase64.substr("wiki:".length); - new FixedUiElement(`Downloading ${themeName} from the wiki...`) +if (layoutFromBase64.startsWith("http")) { + const link = layoutFromBase64; + console.log("Downloading map theme from ", link); + new FixedUiElement(`Downloading the theme from the link...`) .AttachTo("centermessage"); - const cleanUrl = `https://wiki.openstreetmap.org/wiki/${themeName}`; - const url = `https://cors-anywhere.herokuapp.com/` + cleanUrl; // ~NOT~ VERY SAFE AND HACKER-PROOF! - // We use cors-anywhere because the wiki from openstreetmap is locked-down :( - /*/ - const url = cleanUrl; // MUCH SAFER! //*/ $.ajax({ - url: url, + url: link, success: function (data) { - // Hacky McHackFace has been working here. This'll probably break in the future - const startTrigger = "
"; - const start = data.indexOf(startTrigger); - data = data.substr(start, - data.indexOf("
") - start) - data = data.substr(0, data.lastIndexOf("

")) - data = data.substr(data.indexOf("

") + 3) - console.log(data) + try { const parsed = JSON.parse(data); // Overwrite the id to the wiki:value - parsed.id = layoutFromBase64.replace(/[: \/]/g, '-') + parsed.id = link; const layout = new LayoutConfig(parsed); InitUiElements.InitAll(layout, layoutFromBase64, testing, layoutFromBase64, btoa(data)); } catch (e) { - new FixedUiElement(`${themeName} is invalid:
${e}`) + new FixedUiElement(`${link} is invalid:
${e}
Go back")`) .SetClass("clickable") .AttachTo("centermessage"); console.error("Could not parse the text", data) @@ -102,7 +90,7 @@ if (layoutFromBase64.startsWith("wiki:")) { }, }).fail((_, textstatus, error) => { console.error("Could not download the wiki theme:", textstatus, error) - new FixedUiElement(`${themeName} is invalid:
Could not download - wrong URL?
` + + new FixedUiElement(`${link} is invalid:
Could not download - wrong URL?
` + error + "Go back") .SetClass("clickable")