From 79055470a32105b34293c7e81d5ce844149cb61d Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Thu, 25 Aug 2022 03:24:52 +0400 Subject: [PATCH] A JS script for CAPTCHA refreshing. --- homepage/static/assets/js/captcha_refresh.js | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 homepage/static/assets/js/captcha_refresh.js diff --git a/homepage/static/assets/js/captcha_refresh.js b/homepage/static/assets/js/captcha_refresh.js new file mode 100644 index 0000000..76a6615 --- /dev/null +++ b/homepage/static/assets/js/captcha_refresh.js @@ -0,0 +1,30 @@ +const captcha_span = document.getElementsByClassName("captcha")[0]; +const captcha_img = captcha_span.children[1]; + +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(); +}); + +setInterval(async () => { + captcha_remain.innerText = g_captcha_timeout_remain--; + if (g_captcha_timeout_remain == 0) + await newCaptcha(); +}, 1000); \ No newline at end of file