From f24ad3d8832fabff3bcf6a07a80cb25f967dbcf6 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 27 Aug 2022 04:21:15 +0400 Subject: [PATCH] Made a fully working captcha refreshing script. --- homepage/static/assets/js/captcha_refresh.js | 37 +++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/homepage/static/assets/js/captcha_refresh.js b/homepage/static/assets/js/captcha_refresh.js index 76a6615..f3bee68 100644 --- a/homepage/static/assets/js/captcha_refresh.js +++ b/homepage/static/assets/js/captcha_refresh.js @@ -1,30 +1,41 @@ +const g_captcha_timeout_seconds = 600; +let g_captcha_timeout_remain = g_captcha_timeout_seconds; + +const captcha_hidden_field = document.forms[0].children[4].children[0]; + +let g_current_captcha_id = captcha_hidden_field.value; + +async function getNewCaptcha() { + const id = await fetch("/captcha/").then(r => r.text()); + g_current_captcha_id = id; + captcha_hidden_field.value = id; + setTimeout(() => { captcha_img.src = `/captcha/${id}/image` }, 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 = `ed in 600 seconds.`; const captcha_refresh = document.getElementById("refresh"); const captcha_remain = document.getElementById("remain"); -const g_captcha_timeout_seconds = 600; -let g_captcha_timeout_remain = g_captcha_timeout_seconds; - -async function newCaptcha() { - const id = await fetch("/captcha/").then(r => r.text()); - setTimeout(() => { captcha_img.src = `/captcha/${id}/image` }, 400); - g_captcha_timeout_remain = g_captcha_timeout_seconds; -} - -captcha_span.classList.toggle("refresh"); captcha_refresh.classList.toggle("refresh"); captcha_refresh.addEventListener("click", async e => { e.preventDefault(); - await newCaptcha(); + captcha_refresh.disabled = true; + // Checking if CAPTCHA is solved deletes it. + setTimeout(async () => { + captcha_refresh.disabled = false; + await fetch(`/captcha/${g_current_captcha_id}`); }, 3000); + await getNewCaptcha(); }); setInterval(async () => { - captcha_remain.innerText = g_captcha_timeout_remain--; + captcha_remain.innerText = --g_captcha_timeout_remain; if (g_captcha_timeout_remain == 0) - await newCaptcha(); + await getNewCaptcha(); }, 1000); \ No newline at end of file