From fbbc5ad93188e71e337ad6d2587b284be4e96756 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Mon, 5 Apr 2021 00:37:53 +0200 Subject: load survey from (and submit result to) path given as fragment syntax: http(s)://domain.tld/path/to/file/index.html#[upload url] The upload url may be relative to the site's position, or absolute. Survey results will be POSTed to [upload path]/upload. --- site/index.html | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/site/index.html b/site/index.html index 7fef9e9..1378bdf 100644 --- a/site/index.html +++ b/site/index.html @@ -21,6 +21,7 @@ /// polymorphism which would otherwise be hard to get in javascript. let root = document.getElementById("root"); + let surveyUrl = window.location.hash.slice(1); // appends multiple children to an element. returns the root // element to be composable @@ -151,8 +152,7 @@ { type: 'application/octet-stream' } ); - // TODO! - fetch(`http://localhost:8000/upload`, {method:"POST", body:blobData}) + fetch("/upload", {method:"POST", body:blobData}) .then(response => console.log(response.text())) } @@ -206,27 +206,39 @@ // initialise the web assembly parts of this await init(); const Http = new XMLHttpRequest(); - const url="http://127.0.0.1:8000/pubcrypt.age"; + const url= surveyUrl; Http.open("GET", url); Http.responseType = "arraybuffer"; Http.send(); Http.onreadystatechange = (e) => { - if (Http.readyState == 4 && Http.status == 200) { - let bytearray = new Uint8Array (Http.response); - let string = String.fromCharCode.apply(null, bytearray); - let survey = null; - try { - survey = JSON.parse(string); - } catch (e) { - console.log ("survey appears to be encrypted"); - askPassphrase(bytearray); - } - /// if the survey was unencrypted, start it here. If it - /// was encrypted, we need to wait for user action via - /// a js callback (handled in askPassphrase). - if (survey !== null) { - mkSurvey(survey); + if (Http.readyState == 4) { + if (Http.status == 200) { + let bytearray = new Uint8Array (Http.response); + let string = String.fromCharCode.apply(null, bytearray); + let survey = null; + try { + survey = JSON.parse(string); + } catch (e) { + console.log ("survey appears to be encrypted"); + askPassphrase(bytearray); + } + /// if the survey was unencrypted, start it here. If it + /// was encrypted, we need to wait for user action via + /// a js callback (handled in askPassphrase). + if (survey !== null) { + mkSurvey(survey); + } + // couldn't load survey json, show error message + } else { + appendChildren(root, [ + mkElement("h1", "Error"), + mkElement("p", "Could not load this survey; are you sure that it exists?"), + appendChildren( + mkElement("p","attempted path: "), + [mkElement("tt", surveyUrl)] + ) + ]); } } } -- cgit v1.2.3