2022-08-27 04:21:15 +04:00
|
|
|
const g_captcha_timeout_seconds = 600;
|
|
|
|
|
2022-10-24 23:09:07 +04:00
|
|
|
const e_captcha = document.getElementsByClassName("captcha")[0];
|
2022-08-27 04:21:15 +04:00
|
|
|
|
2022-09-23 00:36:43 +04:00
|
|
|
let g_captcha_timeout_remain = g_captcha_timeout_seconds;
|
2022-10-24 23:09:07 +04:00
|
|
|
let g_current_captcha_id = e_captcha.children[0].value;
|
2022-10-19 21:05:24 +04:00
|
|
|
|
2022-08-27 04:21:15 +04:00
|
|
|
async function getNewCaptcha() {
|
2022-10-21 02:10:43 +04:00
|
|
|
const id = await fetch("/api/captcha/", { method: "POST"}).then(r => r.text());
|
2022-10-24 23:09:07 +04:00
|
|
|
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);
|
2022-10-19 21:20:34 +04:00
|
|
|
g_captcha_timeout_remain = g_captcha_timeout_seconds;
|
2022-08-27 04:21:15 +04:00
|
|
|
}
|
|
|
|
|
2022-10-24 23:09:07 +04:00
|
|
|
e_captcha.classList.toggle("refresh");
|
|
|
|
e_captcha.children[3].innerHTML =
|
2022-10-19 21:20:34 +04:00
|
|
|
`<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 => {
|
2022-10-19 21:20:34 +04:00
|
|
|
e.preventDefault();
|
2022-10-24 23:09:07 +04:00
|
|
|
e.target.disabled = true;
|
2022-10-19 21:20:34 +04:00
|
|
|
setTimeout(async () => {
|
2022-10-24 23:09:07 +04:00
|
|
|
e.target.disabled = false;
|
2022-10-21 02:10:43 +04:00
|
|
|
await fetch(`/api/captcha/${g_current_captcha_id}?remove`); }, 3000);
|
2022-10-24 23:09:07 +04:00
|
|
|
e_captcha.children[2].value = "";
|
2022-10-19 21:20:34 +04:00
|
|
|
await getNewCaptcha();
|
2022-08-25 03:24:52 +04:00
|
|
|
});
|
|
|
|
|
2022-08-29 22:57:46 +04:00
|
|
|
// Remove unused CAPTCHA on a server.
|
2022-10-21 02:10:43 +04:00
|
|
|
window.addEventListener("unload", () => fetch(`/api/captcha/${g_current_captcha_id}?remove`));
|
2022-08-29 22:57:46 +04:00
|
|
|
|
2022-08-25 03:24:52 +04:00
|
|
|
setInterval(async () => {
|
2022-10-19 21:20:34 +04:00
|
|
|
captcha_remain.innerText = --g_captcha_timeout_remain;
|
|
|
|
if (g_captcha_timeout_remain == 0)
|
|
|
|
await getNewCaptcha();
|
2022-08-25 03:24:52 +04:00
|
|
|
}, 1000);
|