(function() { const errorText = document.getElementById("error"); const sitePassword = document.getElementById("sitePassword"); const identity = document.getElementById("identity"); const userName = document.getElementById("userName"); const masterPassword = document.getElementById("masterPassword"); const version = document.getElementById("version"); const site = document.getElementById("site"); const siteName = document.getElementById("siteName"); const siteCounter = document.getElementById("siteCounter"); const siteType = document.getElementById("siteType"); let mpw; function generateKey(e) { e.preventDefault(); mpw = new MPW(userName.value, masterPassword.value, version.value); update(() => { identity.classList.remove('active'); site.classList.add('active'); siteName.select(); }); } function generatePassword() { update(() => mpw.generatePassword(siteName.value, siteCounter.valueAsNumber, siteType.value)) .then(pass => sitePassword.textContent = pass) .catch(_ => sitePassword.textContent = null); } function logout() { if (mpw) { mpw.invalidate(); mpw = null; } userName.value = null; masterPassword.value = null; siteName.value = null; sitePassword.textContent = null; site.classList.remove('active'); identity.classList.add('active'); } function update(accept, reject) { errorText.textContent = null; main.classList.add("working"); return mpw.key .then(accept || (x => x)) .catch(error => Promise.reject(errorText.textContent = error)) .catch(reject || (x => Promise.reject(x))) .finally(() => main.classList.remove("working")) ; } function selectText() { if (document.body.createTextRange) { //ms const range = document.body.createTextRange(); range.moveToElementText(this); range.select(); } else if (window.getSelection) { //all others const range = document.createRange(); range.selectNodeContents(this); const selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); } } identity.addEventListener('submit', generateKey); site.addEventListener('input', generatePassword); document.getElementById("logout").addEventListener('click', logout); sitePassword.addEventListener('click', selectText); const rememberName = document.getElementById("rememberName"); rememberName.addEventListener("change", e => userName.autocomplete = (e.target.checked ? "name" : "nope")); rememberName.dispatchEvent(new Event("change")); for (const input of document.querySelectorAll("input[placeholder]")) { input.title = input.placeholder; } // Clear values on page refresh logout(); })();