2022-08-27 04:21:15 +04:00
|
|
|
const g_captcha_timeout_seconds = 600;
|
|
|
|
|
|
|
|
const captcha_hidden_field = document.forms[0].children[4].children[0];
|
|
|
|
|
2022-09-23 00:36:43 +04:00
|
|
|
let g_captcha_timeout_remain = g_captcha_timeout_seconds;
|
2022-08-27 04:21:15 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-25 03:24:52 +04:00
|
|
|
const captcha_span = document.getElementsByClassName("captcha")[0];
|
|
|
|
const captcha_img = captcha_span.children[1];
|
|
|
|
|
2022-08-27 04:21:15 +04:00
|
|
|
captcha_span.classList.toggle("refresh");
|
2022-08-29 22:57:46 +04:00
|
|
|
captcha_span.children[3].innerHTML =
|
|
|
|
`<button id="refresh">Refresh</button>ed in <b><span id="remain">600</span></b> seconds.`;
|
2022-08-25 03:24:52 +04:00
|
|
|
|
|
|
|
const captcha_refresh = document.getElementById("refresh");
|
|
|
|
const captcha_remain = document.getElementById("remain");
|
|
|
|
|
|
|
|
captcha_refresh.classList.toggle("refresh");
|
|
|
|
captcha_refresh.addEventListener("click", async e => {
|
|
|
|
e.preventDefault();
|
2022-08-27 04:21:15 +04:00
|
|
|
captcha_refresh.disabled = true;
|
|
|
|
setTimeout(async () => {
|
|
|
|
captcha_refresh.disabled = false;
|
|
|
|
await fetch(`/captcha/${g_current_captcha_id}`); }, 3000);
|
|
|
|
await getNewCaptcha();
|
2022-08-25 03:24:52 +04:00
|
|
|
});
|
|
|
|
|
2022-08-29 22:57:46 +04:00
|
|
|
// Remove unused CAPTCHA on a server.
|
|
|
|
window.addEventListener("unload", () => fetch(`/captcha/${g_current_captcha_id}`));
|
|
|
|
|
2022-08-25 03:24:52 +04:00
|
|
|
setInterval(async () => {
|
2022-08-27 04:21:15 +04:00
|
|
|
captcha_remain.innerText = --g_captcha_timeout_remain;
|
2022-08-25 03:24:52 +04:00
|
|
|
if (g_captcha_timeout_remain == 0)
|
2022-08-27 04:21:15 +04:00
|
|
|
await getNewCaptcha();
|
2022-08-25 03:24:52 +04:00
|
|
|
}, 1000);
|