Add loading from a random URL; fixes #208
This commit is contained in:
parent
09599e9f15
commit
7ee3a6065c
2 changed files with 14 additions and 46 deletions
|
@ -22,31 +22,11 @@ export default class SharePanel extends UIElement {
|
|||
"<h2>Share</h2>",
|
||||
"Share the following link with friends:<br/>",
|
||||
new VariableUiElement(liveUrl.map(url => `<a href='${url}' target="_blank">${url}</a>`)),
|
||||
"<h2>Publish on OSM Wiki</h2>",
|
||||
"In the list of 'hacks upon hacks to make MapComplete work', it is now possible to put the JSON-file onto a Wikipage*.<br/>" +
|
||||
"This is a <i>very stable</i> and <i>very well-tested</i> solution. Using wikipages as source control! What could possibly go wrong???? /s<br/><br/>",
|
||||
"<h2>Publish on some website</h2>",
|
||||
|
||||
"Why to publish the layout as a wikipage?",
|
||||
"<ul>",
|
||||
...["If something breaks, it can be fixed centrally",
|
||||
"If someone has a remark about your preset, the talk page can be used to point this out and discuss the preset",
|
||||
"In case of a grave error, everyone can step in to fix the prest"].map(li => `<li>${li}</li>`),
|
||||
"</ul>",
|
||||
|
||||
"In order to make this work:",
|
||||
"<ol>",
|
||||
|
||||
...[`Create a new page on the OSM-wiki, e.g. <a href='https://wiki.osm.org/wiki/${proposedName}' target="_blank">${proposedName}</a>`,
|
||||
"Click 'create page'",
|
||||
"Type <span class='literal-code'><nowiki></span>, a few newlines and <span class='literal-code'></nowiki></span>",
|
||||
"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 <span class='literal-code'>userlayout=wiki:YOURWIKIPAGE</span>, e.g. " +
|
||||
`<a href='./custom?userlayout=${proposedNameEnc}' target='_blank'>https://${window.location.host}?userlayout=${proposedNameEnc}</a>`
|
||||
].map(li => `<li>${li}</li>`),
|
||||
|
||||
"</ol>",
|
||||
"(* This has made a lot of people very angry and been widely regarded as a bad move.)",
|
||||
"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://<your-url-here>.json`,
|
||||
"Please note: it used to be possible to load from the wiki - this is not possible anymore due to technical reasons.",
|
||||
"</div>"
|
||||
]);
|
||||
}
|
||||
|
|
30
index.ts
30
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 <a href="${link}">link</a>...`)
|
||||
.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 = "<div class=\"mw-parser-output\">";
|
||||
const start = data.indexOf(startTrigger);
|
||||
data = data.substr(start,
|
||||
data.indexOf("<div class=\"printfooter\">") - start)
|
||||
data = data.substr(0, data.lastIndexOf("</p>"))
|
||||
data = data.substr(data.indexOf("<p>") + 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(`<a href="${cleanUrl}">${themeName}</a> is invalid:<br/>${e}`)
|
||||
new FixedUiElement(`<a href="${link}">${link}</a> is invalid:<br/>${e}<br/> <a href='https://${window.location.host}/'>Go back</a>")`)
|
||||
.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(`<a href="${cleanUrl}">${themeName}</a> is invalid:<br/>Could not download - wrong URL?<br/>` +
|
||||
new FixedUiElement(`<a href="${link}">${link}</a> is invalid:<br/>Could not download - wrong URL?<br/>` +
|
||||
error +
|
||||
"<a href='https://${window.location.host}/'>Go back</a>")
|
||||
.SetClass("clickable")
|
||||
|
|
Loading…
Reference in a new issue