From c114f79080358e36f0265c9d5b464b318128f656 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Thu, 24 Dec 2020 23:15:54 -0600 Subject: [PATCH] refactor: centralized updatePassword code --- src/preload/rememberMe.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/preload/rememberMe.js b/src/preload/rememberMe.js index bd28d9d..c0fb3c7 100644 --- a/src/preload/rememberMe.js +++ b/src/preload/rememberMe.js @@ -4,6 +4,17 @@ function setup() { const form = document.querySelector('form[action="SignIn"]'); const username = document.querySelector('input[name="login"]'); const password = document.querySelector('input[name="password"]'); + + function updatePassword(user) { + if (!user) return; + getPassword(user).then((value) => { + if (value) { + username.value = user; + password.value = value; + } + }) + } + form.addEventListener('submit', () => { const save = document.querySelector('input[name="stayConnected"]').checked; const user = username.value; @@ -15,20 +26,9 @@ function setup() { } }); - password.addEventListener('focus', () => { - const user = username.value; - if (user) getPassword(user).then((value) => password.value = (value || '')); - }); + password.addEventListener('focus', () => updatePassword(username.value)); - const lastUser = localStorage.getItem('underscript.login.lastUser'); - if (lastUser) { - getPassword(lastUser).then((value) => { - if (value) { - username.value = lastUser; - password.value = value; - } - }); - } + updatePassword(localStorage.getItem('underscript.login.lastUser')); } function getPassword(username) {