From 883d5171ef945fc6312cb7be9e4c771199f005bd Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Wed, 25 Oct 2017 12:11:37 +0200 Subject: Added shared javascript to a header-file --- script.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ webPage/script.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 script.h create mode 100644 webPage/script.js diff --git a/script.h b/script.h new file mode 100644 index 0000000..abccd7e --- /dev/null +++ b/script.h @@ -0,0 +1,47 @@ +const char script[] PROGMEM = R"=====( +function fixNoNetwork() { + if (getParameterByName(\"l\") != null) { + document.getElementById(\"homePageAddr\").href = \"/?l=1\"; + document.getElementById(\"settingsPageAddr\").href = \"/settings?l=1\"; + return; + } + + try { + var xhttp = new XMLHttpRequest(); + xhttp.onerror = function onError(e) { + //alert(\"Error \" + e.target.status + \" occurred while receiving the document.\"); + var hrefAddr = window.location.href + \"?l=1\"; + window.location.replace(hrefAddr); + }; + xhttp.open(\"GET\", \"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css\", true); + xhttp.setRequestHeader(\"Content-type\", \"application/x-www-form-urlencoded\"); + xhttp.send(); + } catch (e) { + console.log(e); + } +} + +function ajax_request(adress, callback_function) { + /* A simple ajax request wrapper + Doesn´t return anything else than the callback */ + var xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + callback_function(this.responseText); + } + }; + xhttp.open(\"POST\", adress, true); + xhttp.setRequestHeader(\"Content-type\", \"application/x-www-form-urlencoded\"); + xhttp.send(); +} + +function getParameterByName(name, url) { + if (!url) url = window.location.href; + name = name.replace(/[\[\]]/g, \"\\$&\"); + var regex = new RegExp(\"[?&]\" + name + \"(=([^&#]*)|&|#|$)\"), + results = regex.exec(url); + if (!results) return null; + if (!results[2]) return ''; + return decodeURIComponent(results[2].replace(/\+/g, \" \")); +} +)====="; diff --git a/webPage/script.js b/webPage/script.js new file mode 100644 index 0000000..8de1803 --- /dev/null +++ b/webPage/script.js @@ -0,0 +1,45 @@ +function fixNoNetwork() { + if (getParameterByName("l") != null) { + document.getElementById("homePageAddr").href = "/?l=1"; + document.getElementById("settingsPageAddr").href = "/settings?l=1"; + return; + } + + try { + var xhttp = new XMLHttpRequest(); + xhttp.onerror = function onError(e) { + //alert("Error " + e.target.status + " occurred while receiving the document."); + var hrefAddr = window.location.href + "?l=1"; + window.location.replace(hrefAddr); + }; + xhttp.open("GET", "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css", true); + xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + xhttp.send(); + } catch (e) { + console.log(e); + } +} + +function ajax_request(adress, callback_function) { + /* A simple ajax request wrapper + Doesn´t return anything else than the callback */ + var xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + callback_function(this.responseText); + } + }; + xhttp.open("POST", adress, true); + xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + xhttp.send(); +} + +function getParameterByName(name, url) { + if (!url) url = window.location.href; + name = name.replace(/[\[\]]/g, "\\$&"); + var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), + results = regex.exec(url); + if (!results) return null; + if (!results[2]) return ''; + return decodeURIComponent(results[2].replace(/\+/g, " ")); +} -- cgit v1.2.3