Video_downloader/web/src/lib/api/turnstile.ts
wukko b38cb77952
web/turnstile: refresh turnstile if it expires in background
also renamed `turnstileLoaded` to `turnstileSolved` for more clarity
2024-11-18 21:05:47 +06:00

42 lines
1014 B
TypeScript

import { turnstileSolved } from "$lib/state/turnstile";
const getResponse = () => {
const turnstileElement = document.getElementById("turnstile-widget");
if (turnstileElement) {
return window?.turnstile?.getResponse(turnstileElement);
}
return null;
}
const update = () => {
const turnstileElement = document.getElementById("turnstile-widget");
if (turnstileElement) {
turnstileSolved.set(false);
return window?.turnstile?.reset(turnstileElement);
}
return null;
}
const refreshIfExpired = () => {
const turnstileElement = document.getElementById("turnstile-widget");
if (turnstileElement) {
const isExpired = window?.turnstile?.isExpired(turnstileElement);
if (isExpired) {
console.log("expired, refreshing the turnstile widget rn");
return update();
}
console.log("turnstile not expired, nothing to do");
}
}
export default {
getResponse,
update,
refreshIfExpired,
}