1/** 2 * Communicate with Google reCAPTCHA Enterprise to get a token and add the token 3 * to the form field to be validated (assessed) in the backend after submitting. 4 * Has to happen within 2 minutes after the token is generated. 5 * 6 * @param {Event} event Form submit event 7 * @param {string} siteKey reCAPTCHA site key 8 * @param {string} tagAction reCAPTCHA tag action 9 * @link https://cloud.google.com/recaptcha/docs/create-assessment-website 10 */ 11function getTokenAndValidateRecaptcha(formId, siteKey, tagAction) { 12 const recaptchaTokenClass = 'mautic-recaptcha-token'; 13 const form = document.getElementById(formId); 14 if (!form) { 15 throw new Error(`Form with id '${formId}' not found`); 16 } 17 inputElement = form.querySelector(`input.${recaptchaTokenClass}`); 18 if (!inputElement) { 19 throw new Error(`No input element found with class '${recaptchaTokenClass}'`); 20 } 21 grecaptcha.enterprise.ready(async () => { 22 const token = await grecaptcha.enterprise.execute(siteKey, { action: tagAction }); 23 24 inputElement.value = token; 25 console.debug('Recaptcha: Token received and added to form field. Refreshing token in 110 seconds.'); 26 }); 27} 28 29/** 30 * 31 * @param {string} formId Mautic form name (alias) 32 * @param {string} siteKey reCAPTCHA site key 33 * @param {string} tagAction reCAPTCHA tag action 34 */ 35function validateFormByRecaptcha(formId, siteKey, tagAction) { 36 37 getTokenAndValidateRecaptcha(formId, siteKey, tagAction); 38 // This code sets up a recurring function call using setInterval. 39 // Every 110 seconds, the function getTokenAndValidateRecaptcha 40 // is called with the arguments event, siteKey, and tagAction. 41 const startTime = Date.now(); 42 const intervalId = setInterval(function () { 43 const elapsedTime = Date.now() - startTime; 44 if (elapsedTime >= 3600000) { // 1 hour in milliseconds = 3600000 45 clearInterval(intervalId); 46 console.debug('Recaptcha: Interval cleared after 1 hour.'); 47 return; 48 } 49 getTokenAndValidateRecaptcha(formId, siteKey, tagAction); 50 }, 110000); 51 52};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.