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