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

98 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-03-24 18:04:44 +03:00
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,session=true) {
cookies[key] = val;
if (!session) {
var d = new Date();
d.setTime(d.getTime() + (500*24*60*60*1000));
expirestr = "expires=" + d.toUTCString();
}
else {
expirestr = ""
}
document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(val) + ";" + expirestr;
}
function saveCookies() {
for (var c in cookies) {
document.cookie = encodeURIComponent(c) + "=" + encodeURIComponent(cookies[c]);
2019-03-24 18:04:44 +03:00
}
}
/// RANGE SELECTORS
// in rangeselect.js
/// API KEY
function insertAPIKeyFromCookie() {
2019-06-24 16:02:38 +03:00
element = document.getElementById("apikey")
if (element != null && element != undefined) {
getCookies();
key = cookies["apikey"];
if (key != null && key != undefined) {
element.value = key;
checkAPIkey();
}
2019-06-18 11:32:29 +03:00
}
2019-06-24 16:02:38 +03:00
}
2019-06-24 16:02:38 +03:00
window.addEventListener("load",insertAPIKeyFromCookie);
2019-03-24 18:04:44 +03:00
function saveAPIkey() {
2019-06-18 11:32:29 +03:00
key = APIkey();
setCookie("apikey",key,false);
2019-03-24 18:04:44 +03:00
}
function checkAPIkey() {
url = "/api/test?key=" + APIkey()
2019-03-24 18:04:44 +03:00
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
2019-06-18 11:32:29 +03:00
saveAPIkey();
2019-03-24 18:04:44 +03:00
}
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
}
}
2019-05-14 13:50:31 +03:00
function APIkey() {
return document.getElementById("apikey").value;
}