1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00
maloja/website/javascript/cookies.js
2019-06-24 15:02:38 +02:00

90 lines
1.8 KiB
JavaScript

apikeycorrect = false;
var cookies = {};
function getCookies() {
cookiestrings = decodeURIComponent(document.cookie).split(';');
for(var i = 0; i <cookiestrings.length; i++) {
cookiestrings[i] = cookiestrings[i].trim();
[key,value] = cookiestrings[i].split("=");
cookies[key] = value;
}
}
// always on document load, but call specifically when needed early
document.addEventListener("load",getCookies);
function setCookie(key,val) {
cookies[key] = val;
document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(val);
}
function saveCookies() {
for (var c in cookies) {
document.cookie = encodeURIComponent(c) + "=" + encodeURIComponent(cookies[c]);
}
}
/// RANGE SELECTORS
// in rangeselect.js
/// API KEY
function insertAPIKeyFromCookie() {
element = document.getElementById("apikey")
if (element != null && element != undefined) {
getCookies();
key = cookies["apikey"];
if (key != null && key != undefined) {
element.value = key;
checkAPIkey();
}
}
}
window.addEventListener("load",insertAPIKeyFromCookie);
function saveAPIkey() {
key = APIkey();
setCookie("apikey",key);
}
function checkAPIkey() {
url = "/api/test?key=" + APIkey()
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && (this.status == 204 || this.status == 205)) {
document.getElementById("apikey").style.backgroundColor = "lawngreen"
apikeycorrect = true
saveAPIkey();
}
else {
document.getElementById("apikey").style.backgroundColor = "red"
apikeycorrect = false
}
};
try {
xhttp.open("GET",url,true);
xhttp.send();
}
catch (e) {
document.getElementById("apikey").style.backgroundColor = "red"
apikeycorrect = false
}
}
function APIkey() {
return document.getElementById("apikey").value;
}