2023-02-05 00:54:18 +04:00
|
|
|
|
const g_captcha_timeout_seconds = 600;
|
|
|
|
|
|
|
|
|
|
const e_captcha = document.getElementsByClassName("captcha")[0];
|
|
|
|
|
|
|
|
|
|
let g_captcha_timeout_remain = g_captcha_timeout_seconds;
|
|
|
|
|
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 = 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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 15:03:39 +04:00
|
|
|
|
e_captcha.children[3].innerHTML = document.cookie.includes("lang=ru") ?
|
|
|
|
|
`<button id="refresh">Обновит</button>ся через <b><span id="remain">600</span></b> секунд(у).`
|
|
|
|
|
: `<button id="refresh">Refresh</button>ed in <b><span id="remain">600</span></b> seconds.`;
|
2023-02-05 00:54:18 +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();
|
|
|
|
|
e.target.disabled = true;
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
|
e.target.disabled = false;
|
2023-05-09 23:38:23 +04:00
|
|
|
|
await fetch(`/api/captcha/${g_current_captcha_id}?remove`, { method: "POST"}); }, 3000);
|
2023-02-05 00:54:18 +04:00
|
|
|
|
e_captcha.children[2].value = "";
|
|
|
|
|
await getNewCaptcha();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Remove unused CAPTCHA on a server.
|
2023-05-09 23:38:23 +04:00
|
|
|
|
window.addEventListener("unload", () => fetch(`/api/captcha/${g_current_captcha_id}?remove`, { method: "POST"}));
|
2023-02-05 00:54:18 +04:00
|
|
|
|
|
|
|
|
|
setInterval(async () => {
|
|
|
|
|
captcha_remain.innerText = --g_captcha_timeout_remain;
|
|
|
|
|
if (g_captcha_timeout_remain == 0)
|
|
|
|
|
await getNewCaptcha();
|
|
|
|
|
}, 1000);
|