1/** 2 * @file 3 * Recaptcha v3 behaviors. 4 */ 5 6(($, Drupal) => { 7 function updateTokenElement(element) { 8 let timer; 9 // Wait for grecaptcha to be loaded. 10 if (typeof grecaptcha === 'undefined') { 11 timer = setInterval(() => { 12 if (typeof grecaptcha !== 'undefined' || !element) { 13 clearInterval(timer); 14 15 if (element) { 16 doUpdateTokenElement(element); 17 } 18 } 19 }, 500); 20 } 21 else { 22 doUpdateTokenElement(element); 23 } 24 } 25 26 function doUpdateTokenElement(element) { 27 let $element = $(element); 28 29 grecaptcha.ready(() => { 30 if (!element) { 31 return; 32 } 33 34 grecaptcha.execute($element.data('recaptchaV3SiteKey'), { 35 action: $element.data('recaptchaV3Action') 36 }).then((token) => { 37 $element.val(token); 38 $element.trigger('change'); 39 }); 40 }); 41 } 42 43 /** 44 * Attach recaptcha response token from google with form. 45 * 46 * @type {{attach: Drupal.behaviors.reCaptchaV3.attach}} 47 */ 48 Drupal.behaviors.reCaptchaV3 = { 49 attach: (context) => { 50 once('recaptcha-v3-token', '.recaptcha-v3-token', context).forEach( 51 (element) => { 52 let interval; 53 54 updateTokenElement(element); 55 56 // Update the recaptcha tokens every 90 seconds. 57 // This seems to be the most robust way to always have valid recaptcha 58 // tokens when you don't have control over how the forms are being 59 // submitted. For example normal form submits are synchronous while 60 // Google Recaptcha v3 is asynchonous. 61 // A recaptcha token has a maximum lifetime of 120 seconds. 62 // https://developers.google.com/recaptcha/docs/v3 63 interval = setInterval(() => { 64 if (!element) { 65 clearInterval(interval); 66 } else { 67 updateTokenElement(element); 68 } 69 }, 90000); 70 } 71 ); 72 } 73 }; 74})(jQuery, Drupal);
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.