Reduced size for CAPTCHA js code.

This commit is contained in:
Alexander Andreev 2022-10-24 23:09:07 +04:00
parent 0f9c658d10
commit d35d7e0803
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -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 =
`<button id="refresh">Refresh</button>ed in <b><span id="remain">600</span></b> 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();
});