var PIN_CLOUDFLARE_TURNSTILE_SITE_KEY="0x4AAAAAAGfY12eTIZSINVBi"; (() => { // Cloudflare Turnstile. const TURNSTILE_SRC = 'https://challenges.cloudflare.com/turnstile/v0/api.js'; function load_turnstile() { const existing = document.querySelector(`script[src^="${TURNSTILE_SRC}"]`); if(existing) { return existing; } const script = document.createElement('script'); script.src = TURNSTILE_SRC; script.async = true; script.defer = true; (document.head || document.body || document.documentElement).appendChild(script); return script; } // Setup Infusionsoft Forms. function setup_infusionsoft_forms() { // Look for the Infusionsoft Forms. const forms = document.querySelectorAll('form.infusion-form'); if (!forms.length) return; forms.forEach((form) => { if(form.dataset.pin_cf) { return; } form.dataset.pin_cf = 'true'; const submitBtn = form.querySelector('button[type="submit"]'); const verifyUrl = form.dataset.verifyUrl || '/system/cloudflare/turnstile/verify.php'; // Add the Cloudflare DIV eleemnt before the Button. let element = document.createElement('div'); element.classList.add('cf-turnstile'); element.dataset.sitekey = PIN_CLOUDFLARE_TURNSTILE_SITE_KEY; element.style.textAlign = 'center'; submitBtn.parentNode.insertBefore(element, submitBtn); // Add the Status. element = document.createElement('p'); element.classList.add('inf-form-status'); element.style.color = 'red'; element.style.fontWeight = 'bold'; form.insertBefore(element, null); const statusEl = form.querySelector('.inf-form-status'); const turnstileEl = form.querySelector('.cf-turnstile'); const setStatus = (msg) => { if (statusEl) { statusEl.textContent = msg; } }; const getWidgetTarget = () => { if (!turnstileEl) return undefined; // Turnstile stores the rendered widget ID in dataset.turnstileWidgetId or similar internal state; // passing the container element itself works with turnstile.getResponse() and turnstile.reset(). return turnstileEl; }; form.addEventListener('submit', async (event) => { event.preventDefault(); setStatus(''); const widgetTarget = getWidgetTarget(); const token = (typeof turnstile !== 'undefined' && widgetTarget) ? turnstile.getResponse(widgetTarget) : ''; if (!token) { setStatus('Please complete the security check before submitting.'); return; } if (submitBtn) { submitBtn.disabled = true; } // setStatus('Verifying...'); try { const payload = new FormData(form); payload.set('cf-turnstile-response', token); const res = await fetch(verifyUrl, { method: 'POST', body: payload, credentials: 'same-origin', }); const result = await res.json(); if(!res.ok || !result.valid) { submitBtn.disabled = false; throw new Error(result.message || 'Verification failed. Please try again.'); } const tokenInput = form.querySelector('input[name="cf-turnstile-response"]'); if(tokenInput) { tokenInput.remove(); } HTMLFormElement.prototype.submit.call(form); } catch (err) { setStatus(err.message || 'Unable to verify submission. Please try again.'); if (submitBtn) { submitBtn.disabled = false; } if (typeof turnstile !== 'undefined' && widgetTarget) { turnstile.reset(widgetTarget); } } }); }); } // Exit if we do not have any Infusion Forms. if(!document.querySelector('form.infusion-form')) { console.log('No Infusionsoft Forms were found.'); return; } // Initialise. load_turnstile(); setup_infusionsoft_forms(); })();