1/* 2* Google reCAPTCHA V3 3* File: google_recaptcha_v3.js 4* Documentazione Ufficiale: https://developers.google.com/recaptcha/docs/v3 5* Autore: GrowUp - https://www.growupweb.it 6* Creato: 01/04/2020 7* Aggiornamenti: ----- 8* 9* Installazione dello script 10* 0. Registrare il sito e recuperare le ApiKey : https://www.google.com/recaptcha/admin/create 11* 12* 1. Nel footer di ogni pagina del sito inserire il richiamo alle api.js di google 13*
vendor: 88 bytes, line 13
13 <script src="https://www.google.com/recaptcha/api.js?render=XXX_API_KEY_XXX"></script>
14* <script src="_url_questo_script.js?apikey=XXX_API_KEY_XXX"></script> 15* 16* 2. Modificare il validatore del form inserendo le funzioni del reCAPTCHA 17* Se la validazione ha esito positivo, prima di eseguire un redirect o il salvataggio dei dati va eseguita la GRecaptchaVerify() 18* action: l'azione che viene eseguita (es login) - possono esser pesonalizzate 19* callback: si occuperà del redirect e/o del salvataggio finale dei dati, in modo che prima di qualsiasi cosa la reCAPTCHA verifichi lo score 20* 21*/ 22 23var reCAPTCHAV3Config = { 24 script_tag_id : 'reCAPTCHAv3ScripTag', 25 url_server_verify : '/web/google_recaptcha_v3.php?act=VERIFY', 26} 27 28/***********************************************/ 29 30function GRecaptchaGetApiKey(idscript){ 31 var script_tag = document.getElementById(idscript); 32 33 var query = script_tag.src.replace(/^[^\?]+\??/,''); 34 // Parse the querystring into arguments and parameters 35 var vars = query.split("&"); 36 var args = {}; 37 for (var i=0; i<vars.length; i++) { 38 var pair = vars[i].split("="); 39 // decodeURI doesn't expand "+" to a space. 40 args[pair[0]] = decodeURI(pair[1]).replace(/\+/g, ' '); 41 } 42 return args['apikey']; 43} 44 45function GRecaptchaGetScore(token, callback){ 46 47 fetch(reCAPTCHAV3Config.url_server_verify,{ 48 method: "post", 49 headers: new Headers({ 50 "Content-Type": "application/json" 51 }), 52 body: JSON.stringify({ 53 'g-recaptcha-response' : token 54 }) 55 }) 56 .then((response) => { 57 return response.json(); 58 }) 59 .then((data) => { 60 61 console.log('data response: ', data); 62 if(data.success){ 63 if(data.score > 0.5){ 64 65 if(typeof callback == 'function'){ 66 console.log('eseguo la callback', callback); 67 callback(); 68 }else{ 69 //nessuna callback; 70 } 71 }else{ 72 console.log('score basso', data); 73 return false; 74 } 75 }else{ 76 return false; 77 } 78 79 return false; 80 }); 81 82 return false; 83 84} 85 86 87 88function GRecaptchaVerifyOld(azione, callback){ 89 grecaptcha.ready(function() { 90 var api_key = GRecaptchaGetApiKey(reCAPTCHAV3Config.script_tag_id); 91 grecaptcha.execute(api_key, {action: azione}) 92 .then(function(token) { 93 GRecaptchaGetScore(token, callback); 94 }); 95 }); 96} 97 98function GRecaptchaVerifyForm(form, azione, callback){ 99 grecaptcha.ready(function() { 100 var api_key = GRecaptchaGetApiKey(reCAPTCHAV3Config.script_tag_id); 101 grecaptcha.execute(api_key, {action: azione}) 102 .then(function(token) { 103 if($(form).find('input[name=g-recaptcha-response]').length > 0) $(form).find('input[name=g-recaptcha-response]').remove(); 104 $(form).append('<input type="hidden" name="g-recaptcha-response" value="'+token+'">'); 105 callback(); 106 }); 107 }); 108} 109
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.