(function () { const recaptchaConfigURL = "/getrecaptchaconfig/"; let siteKey = null; function getReCAPTCHAConfig(url, callback) { $.ajax({ type: "GET", url: url, dataType: "json" }).done(function (json) { if (json && json.siteKey) { siteKey = json.siteKey; callback(siteKey); } }).fail(function (xhr, status, error) { console.error("Failed to fetch reCAPTCHA config:", status, error); }); } function injectReCaptcha(siteKey) { const script = document.createElement("script"); script.src = "https://www.google.com/recaptcha/api.js?render=" + siteKey; script.async = true; document.head.appendChild(script); } function waitForGrecaptcha(callback) { if (typeof grecaptcha !== 'undefined' && grecaptcha.ready) { grecaptcha.ready(callback); } else { setTimeout(() => waitForGrecaptcha(callback), 100); } } // Initialize getReCAPTCHAConfig(recaptchaConfigURL, function (siteKey) { injectReCaptcha(siteKey); const tokenField = $('#enwl_recaptchatoken'); if (tokenField.length) { tokenField.closest('tr').hide(); } }); window.executeReCaptcha = function (action, targetFieldId, callback) { if (!siteKey) { console.error("reCAPTCHA site key is not yet loaded."); return; } waitForGrecaptcha(() => { grecaptcha.execute(siteKey, { action: action }).then(function (token) { if (targetFieldId) { $('#' + targetFieldId).val(token); } if (callback) callback(token); }); }); }; })();