// origin url parameter name var ORIGIN_PAR = 'ORIGIN'; // url address of page that invoked this login dialog var ORIGIN_URL_BASE64 = gup(ORIGIN_PAR); // true if the origin parameter is given var ORIGIN_GIVEN = ORIGIN_URL_BASE64 !== '' ? true : false; // send POST to GEN=TOKEN function fetchToken() { var data = JSON.stringify({ username: document.getElementById("usr").value, password: document.getElementById("psw").value }); var xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.addEventListener("readystatechange", function () { if (this.readyState === 4) { var response = JSON.parse(this.responseText); if (typeof response.Token === 'string') { callOriginPage( document.getElementById("usr").value, response.Token ); } else { alert('Špatné přihlašovací údaje.'); } } }); xhr.open("POST", "https://webmap.dppcr.cz/dpp_cr/editor.dll?GEN=TOKEN&FORMAT=json"); xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader("cache-control", "no-cache"); xhr.send(data); return false; } function callOriginPage(usr, token) { if (!ORIGIN_GIVEN) { console.log('ORIGIN URL NOT GIVEN!'); window.location.href = "https://webmap.dppcr.cz/dpp_cr/editor.dll"; } else { var originUrl = window.atob(ORIGIN_URL_BASE64); originUrl += originUrl.indexOf('?') > -1 ? '&' : '?'; originUrl += 'USERNAME=' + usr + '&TOKEN=' + token; window.location.href = originUrl; } } // get url parameter function gup (name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(window.location.href); if (results == null) { return ""; } else { return results[1].replace(/\+/g, ' '); } };