|
1 | 1 | <script> |
| 2 | + import { onMount } from 'svelte'; |
2 | 3 | /** @type {{ color: string, text: string, subscribeForm?: boolean }} */ |
3 | 4 | let { color, text, subscribeForm = false } = $props(); |
4 | 5 |
|
|
11 | 12 | // Spam prevention variables |
12 | 13 | let formStartTime = Date.now(); |
13 | 14 | let turnstileResponse = ''; |
| 15 | + /** @type {string | null} */ |
14 | 16 | let turnstileWidgetId = null; |
15 | 17 | let isTurnstileLoaded = false; |
16 | 18 |
|
| 19 | + /** @returns {boolean} */ |
| 20 | + function isFormTooFast() { |
| 21 | + return Date.now() - formStartTime < 3000; |
| 22 | + } |
| 23 | +
|
| 24 | + /** @type {any} */ |
| 25 | + const win = typeof window !== 'undefined' ? window : null; |
| 26 | +
|
17 | 27 | onMount(() => { |
18 | | - if (!window.turnstile) { |
| 28 | + if (!win.turnstile) { |
19 | 29 | console.log('Cargando script de Turnstile...'); |
20 | 30 | const script = document.createElement('script'); |
21 | 31 | script.src = 'https://challenges.cloudflare.com/turnstile/v0/api.js'; |
|
42 | 52 | // Widget para el Captcha de Cloudflare Turnstile |
43 | 53 | function initTurnstile() { |
44 | 54 | console.log('Intentando inicializar Turnstile...'); |
45 | | - console.log('window.turnstile:', window.turnstile); |
| 55 | + console.log('window.turnstile:', win.turnstile); |
46 | 56 | console.log('isTurnstileLoaded:', isTurnstileLoaded); |
47 | 57 |
|
48 | | - if (window.turnstile && isTurnstileLoaded) { |
| 58 | + if (win.turnstile && isTurnstileLoaded) { |
49 | 59 | try { |
50 | 60 | console.log('Renderizando widget de Turnstile...'); |
51 | | - turnstileWidgetId = window.turnstile.render('#turnstile-container', { |
| 61 | + turnstileWidgetId = win.turnstile.render('#turnstile-container', { |
52 | 62 | sitekey: '0x4AAAAAAB6c7nCtT4Pnd6ZP', |
53 | | - callback: function (token) { |
| 63 | + callback: function (/** @type {string} */ token) { |
54 | 64 | console.log('Callback de Turnstile recibido:', token); |
55 | 65 | turnstileResponse = token; |
56 | 66 | }, |
57 | 67 | 'expired-callback': function () { |
58 | 68 | console.log('Turnstile caducado'); |
59 | 69 | turnstileResponse = ''; |
60 | 70 | }, |
61 | | - 'error-callback': function (error) { |
| 71 | + 'error-callback': function (/** @type {string} */ error) { |
62 | 72 | console.error('Error de Turnstile:', error); |
63 | 73 | turnstileResponse = ''; |
64 | 74 | } |
|
73 | 83 | } |
74 | 84 |
|
75 | 85 | // Validate form submission |
76 | | - function validateForm(event) { |
77 | | - const target = event.target; |
| 86 | + function validateForm(/** @type {SubmitEvent} */ event) { |
| 87 | + const target = /** @type {HTMLFormElement} */ (event.target); |
78 | 88 |
|
79 | 89 | // Check honeypot field |
80 | | - const honeypot = target.querySelector('input[name="website"]'); |
| 90 | + const honeypot = /** @type {HTMLInputElement | null} */ (target.querySelector('input[name="website"]')); |
81 | 91 | if (honeypot && honeypot.value !== '') { |
82 | 92 | event.preventDefault(); |
83 | 93 | console.log('Bot detectado: campo honeypot llenado'); |
|
132 | 142 | {@html text} |
133 | 143 | </p> |
134 | 144 | </div> |
135 | | - {#if subscribeForm} |
136 | 145 | {#if subscribeForm} |
137 | 146 | <div class="flex justify-center mx-auto my-3"> |
138 | 147 | <form class="w-full max-w-sm validate" action={breveo_url} method="post" id="newsletter-form" target="_blank" > |
|
0 commit comments