From 98afcdec1aa1885fa813910b31c7585cb3eefd45 Mon Sep 17 00:00:00 2001 From: Javaxman Date: Wed, 29 Jul 2026 16:52:28 +0200 Subject: [PATCH] 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 --- src/scripts/nx-contact-form.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/scripts/nx-contact-form.ts b/src/scripts/nx-contact-form.ts index 2c8c9f7..cfa2808 100644 --- a/src/scripts/nx-contact-form.ts +++ b/src/scripts/nx-contact-form.ts @@ -59,6 +59,29 @@ function showError(message: string): void { 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('.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( + 'input[name="cf-turnstile-response"]', + ); + if (tokenInput) tokenInput.value = ''; +} + function bind(): void { const form = document.getElementById('nx-contact-form') as HTMLFormElement | null; if (!form) return; @@ -99,6 +122,7 @@ function bind(): void { (data && typeof data.error === 'string' && data.error) || 'Invio non riuscito. Riprova tra poco.', ); + if (hasTurnstile) resetTurnstile(form); return; } const target = @@ -108,6 +132,7 @@ function bind(): void { window.location.assign(target); } catch { showError('Errore di rete. Controlla la connessione e riprova.'); + if (hasTurnstile) resetTurnstile(form); } finally { if (submitBtn) submitBtn.disabled = false; }