From d35d7e08037e2a49b81a2a851224bc67779278b7 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 24 Oct 2022 23:09:07 +0400 Subject: [PATCH] Reduced size for CAPTCHA js code. --- homepage/static/assets/js/captcha_refresh.js | 27 ++++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/homepage/static/assets/js/captcha_refresh.js b/homepage/static/assets/js/captcha_refresh.js index 4f7cc6c..c937113 100644 --- a/homepage/static/assets/js/captcha_refresh.js +++ b/homepage/static/assets/js/captcha_refresh.js @@ -1,27 +1,20 @@ const g_captcha_timeout_seconds = 600; -const captcha_hidden_field = document.forms[0].children[4].children[0]; +const e_captcha = document.getElementsByClassName("captcha")[0]; let g_captcha_timeout_remain = g_captcha_timeout_seconds; -let g_current_captcha_id = captcha_hidden_field.value; - -function getColorScheme() { - return window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark"; -} +let g_current_captcha_id = e_captcha.children[0].value; async function getNewCaptcha() { const id = await fetch("/api/captcha/", { method: "POST"}).then(r => r.text()); - g_current_captcha_id = id; - captcha_hidden_field.value = id; - setTimeout(() => { captcha_img.src = `/api/captcha/${id}/image?style=${getColorScheme()}` }, 600); + g_current_captcha_id = e_captcha.children[0].value = id; + const style = window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark"; + setTimeout(() => { e_captcha.children[1].src = `/api/captcha/${id}/image?style=${style}` }, 600); g_captcha_timeout_remain = g_captcha_timeout_seconds; } -const captcha_span = document.getElementsByClassName("captcha")[0]; -const captcha_img = captcha_span.children[1]; - -captcha_span.classList.toggle("refresh"); -captcha_span.children[3].innerHTML = +e_captcha.classList.toggle("refresh"); +e_captcha.children[3].innerHTML = `ed in 600 seconds.`; const captcha_refresh = document.getElementById("refresh"); @@ -30,11 +23,11 @@ const captcha_remain = document.getElementById("remain"); captcha_refresh.classList.toggle("refresh"); captcha_refresh.addEventListener("click", async e => { e.preventDefault(); - captcha_refresh.disabled = true; + e.target.disabled = true; setTimeout(async () => { - captcha_refresh.disabled = false; + e.target.disabled = false; await fetch(`/api/captcha/${g_current_captcha_id}?remove`); }, 3000); - document.getElementsByName("captcha_answer")[0].value = ""; + e_captcha.children[2].value = ""; await getNewCaptcha(); });