|
| 1 | +(function() { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + // Register the handler with the form system when it's ready |
| 5 | + const registerRecaptchaHandler = function() { |
| 6 | + if (window.GravFormXHR && window.GravFormXHR.captcha) { |
| 7 | + window.GravFormXHR.captcha.register('recaptcha', { |
| 8 | + reset: function(container, form) { |
| 9 | + if (!form || !form.id) { |
| 10 | + console.warn('Cannot reset reCAPTCHA: form is invalid or missing ID'); |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + const formId = form.id; |
| 15 | + console.log(`Attempting to reset reCAPTCHA for form: ${formId}`); |
| 16 | + |
| 17 | + // First try the expected ID pattern from the Twig template |
| 18 | + const recaptchaId = `g-recaptcha-${formId}`; |
| 19 | + // We need to look more flexibly for the container |
| 20 | + let widgetContainer = document.getElementById(recaptchaId); |
| 21 | + |
| 22 | + // If not found by ID, look for the div inside the captcha provider container |
| 23 | + if (!widgetContainer) { |
| 24 | + // Try to find it inside the captcha provider container |
| 25 | + widgetContainer = container.querySelector('.g-recaptcha'); |
| 26 | + |
| 27 | + if (!widgetContainer) { |
| 28 | + // If that fails, look more broadly in the form |
| 29 | + widgetContainer = form.querySelector('.g-recaptcha'); |
| 30 | + |
| 31 | + if (!widgetContainer) { |
| 32 | + // Last resort - create a new container if needed |
| 33 | + console.warn(`reCAPTCHA container #${recaptchaId} not found. Creating a new one.`); |
| 34 | + widgetContainer = document.createElement('div'); |
| 35 | + widgetContainer.id = recaptchaId; |
| 36 | + widgetContainer.className = 'g-recaptcha'; |
| 37 | + container.appendChild(widgetContainer); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + console.log(`Found reCAPTCHA container for form: ${formId}`); |
| 43 | + |
| 44 | + // Get configuration from data attributes |
| 45 | + const parentContainer = container.closest('[data-captcha-provider="recaptcha"]'); |
| 46 | + if (!parentContainer) { |
| 47 | + console.warn('Cannot find reCAPTCHA parent container with data-captcha-provider attribute.'); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const sitekey = parentContainer.dataset.sitekey; |
| 52 | + const version = parentContainer.dataset.version || '2-checkbox'; |
| 53 | + const isV3 = version.startsWith('3'); |
| 54 | + const isInvisible = version === '2-invisible'; |
| 55 | + |
| 56 | + if (!sitekey) { |
| 57 | + console.warn('Cannot reinitialize reCAPTCHA - missing sitekey attribute'); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + console.log(`Re-rendering reCAPTCHA widget for form: ${formId}, version: ${version}`); |
| 62 | + |
| 63 | + // Handle V3 reCAPTCHA differently |
| 64 | + if (isV3) { |
| 65 | + try { |
| 66 | + // For v3, we don't need to reset anything visible, just make sure we have the API |
| 67 | + if (typeof grecaptcha !== 'undefined' && typeof grecaptcha.execute === 'function') { |
| 68 | + // Create a new execution context for the form |
| 69 | + const actionName = `form_${formId}`; |
| 70 | + const tokenInput = form.querySelector('input[name="token"]') || |
| 71 | + form.querySelector('input[name="data[token]"]'); |
| 72 | + const actionInput = form.querySelector('input[name="action"]') || |
| 73 | + form.querySelector('input[name="data[action]"]'); |
| 74 | + |
| 75 | + if (tokenInput && actionInput) { |
| 76 | + // Clear previous token |
| 77 | + tokenInput.value = ''; |
| 78 | + |
| 79 | + // Set the action name |
| 80 | + actionInput.value = actionName; |
| 81 | + |
| 82 | + console.log(`reCAPTCHA v3 ready for execution on form: ${formId}`); |
| 83 | + } else { |
| 84 | + console.warn(`Cannot find token or action inputs for reCAPTCHA v3 in form: ${formId}`); |
| 85 | + } |
| 86 | + } else { |
| 87 | + console.warn('reCAPTCHA v3 API not properly loaded.'); |
| 88 | + } |
| 89 | + } catch (e) { |
| 90 | + console.error(`Error setting up reCAPTCHA v3: ${e.message}`); |
| 91 | + } |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + // For v2, handle visible widget reset |
| 96 | + // Clear the container to ensure fresh rendering |
| 97 | + widgetContainer.innerHTML = ''; |
| 98 | + |
| 99 | + // Check if reCAPTCHA API is available |
| 100 | + if (typeof grecaptcha !== 'undefined' && typeof grecaptcha.render === 'function') { |
| 101 | + try { |
| 102 | + // Render with a slight delay to ensure DOM is settled |
| 103 | + setTimeout(() => { |
| 104 | + grecaptcha.render(widgetContainer.id || widgetContainer, { |
| 105 | + 'sitekey': sitekey, |
| 106 | + 'theme': parentContainer.dataset.theme || 'light', |
| 107 | + 'size': isInvisible ? 'invisible' : 'normal', |
| 108 | + 'callback': function(token) { |
| 109 | + console.log(`reCAPTCHA verification completed for form: ${formId}`); |
| 110 | + |
| 111 | + // If it's invisible reCAPTCHA, submit the form automatically |
| 112 | + if (isInvisible && window.GravFormXHR && typeof window.GravFormXHR.submit === 'function') { |
| 113 | + window.GravFormXHR.submit(form); |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + console.log(`Successfully rendered reCAPTCHA for form: ${formId}`); |
| 118 | + }, 100); |
| 119 | + } catch (e) { |
| 120 | + console.error(`Error rendering reCAPTCHA widget: ${e.message}`); |
| 121 | + widgetContainer.innerHTML = '<p style="color:red;">Error initializing reCAPTCHA.</p>'; |
| 122 | + } |
| 123 | + } else { |
| 124 | + console.warn('reCAPTCHA API not available. Attempting to reload...'); |
| 125 | + |
| 126 | + // Remove existing script if any |
| 127 | + const existingScript = document.querySelector('script[src*="google.com/recaptcha/api.js"]'); |
| 128 | + if (existingScript) { |
| 129 | + existingScript.parentNode.removeChild(existingScript); |
| 130 | + } |
| 131 | + |
| 132 | + // Create new script element |
| 133 | + const script = document.createElement('script'); |
| 134 | + script.src = `https://www.google.com/recaptcha/api.js${isV3 ? '?render=' + sitekey : ''}`; |
| 135 | + script.async = true; |
| 136 | + script.defer = true; |
| 137 | + script.onload = function() { |
| 138 | + console.log('reCAPTCHA API loaded, retrying widget render...'); |
| 139 | + setTimeout(() => { |
| 140 | + const retryContainer = document.querySelector(`[data-captcha-provider="recaptcha"]`); |
| 141 | + if (retryContainer && form) { |
| 142 | + window.GravFormXHR.captcha.getProvider('recaptcha').reset(retryContainer, form); |
| 143 | + } |
| 144 | + }, 200); |
| 145 | + }; |
| 146 | + document.head.appendChild(script); |
| 147 | + } |
| 148 | + } |
| 149 | + }); |
| 150 | + console.log('reCAPTCHA XHR handler registered successfully'); |
| 151 | + } else { |
| 152 | + console.error('GravFormXHR.captcha not found. Make sure the Form plugin is loaded correctly.'); |
| 153 | + } |
| 154 | + }; |
| 155 | + |
| 156 | + // Try to register the handler immediately if GravFormXHR is already available |
| 157 | + if (window.GravFormXHR && window.GravFormXHR.captcha) { |
| 158 | + registerRecaptchaHandler(); |
| 159 | + } else { |
| 160 | + // Otherwise, wait for the DOM to be fully loaded |
| 161 | + document.addEventListener('DOMContentLoaded', function() { |
| 162 | + // Give a small delay to ensure GravFormXHR is initialized |
| 163 | + setTimeout(registerRecaptchaHandler, 100); |
| 164 | + }); |
| 165 | + } |
| 166 | +})(); |
0 commit comments