google-recaptcha-v2
Version:
A simple library of captcha based on Google reCAPTCHA v2.
1 lines • 3.01 kB
JavaScript
!function(){var l=function(e){return e instanceof l?e:this instanceof l?void(this.GoogleReCaptchaWrapped=e):new l(e)};function o(e,a){a=a||"log";console.hasOwnProperty(a)?console[a]('Module "google-recaptcha-v2" '+a+" message => "+e):console.log('Module "google-recaptcha-v2" debug message => '+e)}"undefined"!=typeof exports?(exports="undefined"!=typeof module&&module.exports?module.exports=l:exports).GoogleReCaptcha=l:this.GoogleReCaptcha=l;var t="the JSSDK of Google reCAPTCHA load successfully!";l.size="invisible",l.init=function(e){"object"==typeof e&&null!==e?(l.host="string"==typeof e.host?e.host:"www.google.com",l.id="string"==typeof e.id?e.id:"",l.siteKey="string"==typeof e.siteKey?e.siteKey:"",l.callback="function"==typeof e.callback?e.callback:null,l.expiredCallback="function"==typeof e.expiredCallback?e.expiredCallback:null,l.errorCallback="function"==typeof e.errorCallback?e.errorCallback:null,l.debug="boolean"==typeof e.debug&&e.debug,l.siteKey?l.callback?(l.loaded=!1,l.id?l.explicitRender():l.defaultRender()):o('method "init" must pass the value of "callback" on options params, please check again!',"error"):o('method "init" must pass the value of "siteKey" on options params, please check again!',"error")):o('method "init" must pass a object variable, please check again!',"error")},l.defaultRender=function(){var e=document.querySelector(".g-recaptcha");e.setAttribute("data-size",l.size),e.setAttribute("data-sitekey",l.siteKey),l.callback&&(window.GoogleReCaptchaCallback=l.callback,e.setAttribute("data-callback","GoogleReCaptchaCallback")),l.expiredCallback&&(window.GoogleReCaptchaExpiredCallback=l.expiredCallback,e.setAttribute("data-expired-callback","GoogleReCaptchaExpiredCallback")),l.errorCallback&&(window.GoogleReCaptchaErrorCallback=l.errorCallback,e.setAttribute("data-error-callback","GoogleReCaptchaErrorCallback")),l.loadJSSDK()},l.explicitRender=function(){l.setGlobalOnloadCallback(),l.loadJSSDK()},l.setGlobalOnloadCallback=function(){"function"!=typeof window.GoogleReCaptchaOnloadCallback&&(window.GoogleReCaptchaOnloadCallback=function(){l.debug&&o(t),l.loaded=!0,grecaptcha.render(l.id,{sitekey:l.siteKey,size:l.size,callback:function(e){l.callback&&l.callback(e)},"expired-callback":function(){l.expiredCallback&&l.expiredCallback()},"error-callback":function(){l.errorCallback&&l.errorCallback({code:10001,message:"Google reCAPTCHA encounters an error(usually network connectivity), please try again!"})}})})},l.loadJSSDK=function(){var e=document.createElement("script"),a="https://"+l.host+"/recaptcha/api.js";l.id?a+="?onload=GoogleReCaptchaOnloadCallback&render=explicit":e.onload=function(){l.debug&&o(t),l.loaded=!0},e.type="text/javascript",e.src=a,e.async="async",e.defer="defer",document.body.appendChild(e)},l.validate=function(e){e.preventDefault(),l.loaded?(grecaptcha.reset(),grecaptcha.execute()):l.errorCallback&&l.errorCallback({code:-1,message:"The JSSDK of Google reCAPTCHA load unsuccessfully, please check again!"})}}.call(this);