Resetta Turnstile dopo un invio contatti fallito.

Il token viene consumato al primo POST: senza reset i retry falliscono la verifica anti-spam.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Javaxman
2026-07-29 16:52:28 +02:00
parent 6339325e6a
commit 98afcdec1a

View File

@ -59,6 +59,29 @@ function showError(message: string): void {
err.classList.remove('hidden'); err.classList.remove('hidden');
} }
/** Dopo un POST fallito il token Turnstile è già consumato: ne serve uno nuovo. */
function resetTurnstile(form: HTMLFormElement): void {
const widget = form.querySelector<HTMLElement>('.cf-turnstile');
const turnstile = (
window as Window & {
turnstile?: { reset: (target?: string | HTMLElement) => void };
}
).turnstile;
if (turnstile?.reset) {
try {
if (widget) turnstile.reset(widget);
else turnstile.reset();
return;
} catch {
// fall through
}
}
const tokenInput = form.querySelector<HTMLInputElement>(
'input[name="cf-turnstile-response"]',
);
if (tokenInput) tokenInput.value = '';
}
function bind(): void { function bind(): void {
const form = document.getElementById('nx-contact-form') as HTMLFormElement | null; const form = document.getElementById('nx-contact-form') as HTMLFormElement | null;
if (!form) return; if (!form) return;
@ -99,6 +122,7 @@ function bind(): void {
(data && typeof data.error === 'string' && data.error) || (data && typeof data.error === 'string' && data.error) ||
'Invio non riuscito. Riprova tra poco.', 'Invio non riuscito. Riprova tra poco.',
); );
if (hasTurnstile) resetTurnstile(form);
return; return;
} }
const target = const target =
@ -108,6 +132,7 @@ function bind(): void {
window.location.assign(target); window.location.assign(target);
} catch { } catch {
showError('Errore di rete. Controlla la connessione e riprova.'); showError('Errore di rete. Controlla la connessione e riprova.');
if (hasTurnstile) resetTurnstile(form);
} finally { } finally {
if (submitBtn) submitBtn.disabled = false; if (submitBtn) submitBtn.disabled = false;
} }