diff --git a/dist/easycaptcha.js b/dist/easycaptcha.js index 9a8e2fa..c749401 100644 --- a/dist/easycaptcha.js +++ b/dist/easycaptcha.js @@ -182,37 +182,37 @@ function handleOneChild($group, i, options, AUTO_INIT) { if (meta.length !== 0) { settings.ReCAPTCHA_API_KEY_CLIENT = meta.attr('content'); } - if ($this.attr('data-okbtn-selector') !== "") { + if (attrExist($this.attr('data-okbtn-selector'))) { settings.autoVerification.okBtn = $this.attr('data-okbtn-selector'); } - if ($this.attr('data-recaptcha-apikey') !== "") { + if (attrExist($this.attr('data-recaptcha-apikey'))) { settings.ReCAPTCHA_API_KEY_CLIENT = $this.attr('data-recaptcha-apikey'); } - if ($this.attr('data-required-msg-example-selector') !== "") { + if (attrExist($this.attr('data-required-msg-example-selector'))) { let e = $($this.attr('data-required-msg-example-selector')).clone(); e.removeClass('hidden'); e.removeClass('d-none'); settings.autoVerification.requiredMsg = e; } - if ($this.attr('data-loading-msg-example-selector') !== "") { + if (attrExist($this.attr('data-loading-msg-example-selector'))) { let e = $($this.attr('data-loading-msg-example-selector')).clone(); e.removeClass('hidden'); e.removeClass('d-none'); settings.apiScriptLoading.loadingMsg = e; } - if ($this.attr('data-error-msg-example-selector') !== "") { + if (attrExist($this.attr('data-error-msg-example-selector'))) { let e = $($this.attr('data-error-msg-example-selector')).clone(); e.removeClass('hidden'); e.removeClass('d-none'); settings.apiScriptLoading.errorMsg = e; } - if ($this.attr('data-theme') !== "") { + if (attrExist($this.attr('data-theme'))) { settings.theme = $this.attr('data-theme'); } data.settings = settings; changeBtnState(data, false); let idSuffix = Math.floor((Math.random() * 1000) + 100); - if ($this.attr('id') !== "") { + if (attrExist($this.attr('id'))) { $this.attr('id', idSuffix + "_ReCaptchaTargetParent"); } data.parentId = $this.attr('id'); @@ -225,6 +225,7 @@ function handleOneChild($group, i, options, AUTO_INIT) { let $hiddenInput = $(''); $hiddenInput.appendTo($this); } + $this.data('EasyCaptcha', data); startCheckingGoogleReCaptchaScript(data).then( function () { handleOneChild($group, i+1, options, AUTO_INIT); @@ -233,7 +234,6 @@ function handleOneChild($group, i, options, AUTO_INIT) { settings.failure(error??"Unknown error"); } ); - $this.data('EasyCaptcha', data); } else { handleOneChild($group, i+1, options, AUTO_INIT); } @@ -344,6 +344,10 @@ function changeBtnState(data, enable) { else $okBtn.removeClass('disabled'); } } + +function attrExist(attr) { + return typeof attr !== 'undefined' && attr !== null && attr !== ""; +} ;// CONCATENATED MODULE: ./index.js diff --git a/dist/easycaptcha.min.js b/dist/easycaptcha.min.js index 0bcf605..7a8e286 100644 --- a/dist/easycaptcha.min.js +++ b/dist/easycaptcha.min.js @@ -4,4 +4,4 @@ * Released under the MIT License. * Github: https://github.com/HichemTab-tech/EasyCaptchaJS */ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.EasyCaptchaJS=t():e.EasyCaptchaJS=t()}(this,(()=>(()=>{"use strict";var e={d:(t,a)=>{for(var r in a)e.o(a,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:a[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{EasyCaptcha:()=>a});const a=function(e={},...t){let a=null===e;if(null===e&&(e={}),0===$(this).length&&!a)throw"no ReCaptcha div parent.";let n=[];const s={verify:function(e,t,a){if(0!==a.length){if(a[0].startsWith("#")&&(a[0]=a[0].substring(1)),a[0]===t.parentId){let a={verified:""!==i.getResponse(t.widgetId)};e.push(a)}}else{let a={parentElement:$("#"+t.parentId),verified:""!==i.getResponse(t.widgetId)};e.push(a)}return e},response:function(e,t,a){if(0!==a.length){if(a[0].startsWith("#")&&(a[0]=a[0].substring(1)),a[0]===t.parentId){let a={token:i.getResponse(t.widgetId)};e.push(a)}}else{let a={parentElement:$("#"+t.parentId),token:i.getResponse(t.widgetId)};e.push(a)}return e},reset:function(e,t,a){return 0!==a.length?(a[0].startsWith("#")&&(a[0]=a[0].substring(1)),a[0]===t.parentId&&i.reset(t.widgetId)):i.reset(),e},destroy:function(e,t){for(let e=0;e<$(this).length;e++)$("#"+t.parentId).empty(),$($(this)[e]).removeData("EasyCaptcha");return e}};if("object"==typeof e)r($(this),0,e,a);else for(let a=0;a<$(this).length;a++){let r=$($(this)[a]).data("EasyCaptcha");r&&void 0!==s[e]&&(n=s[e](n,r,[...t]))}return n.length>1?n:0===n.length?null:n[0]};function r(e,t,a,i){if(e.length===t)return;let s,l=$(e[t]),d=l.data("EasyCaptcha");if(d)r(e,t+1,a,i);else{d={},d.AUTO_INIT=i,s=$.extend({ReCAPTCHA_API_KEY_CLIENT:null,ReCaptchaSubmit:{success:()=>{},failed:()=>{},expired:()=>{}},autoVerification:{okBtn:null,requiredMsg:"
Please verify that you are not a robot.
"},apiScriptLoading:{loadingMsg:'
',error:()=>{},errorMsg:"
Error while loading Api Script. retry
"},theme:"light",failure:e=>{console.error(e)}},a);let c=$('meta[name="ReCAPTCHA_API_KEY_CLIENT"]');if(0!==c.length&&(s.ReCAPTCHA_API_KEY_CLIENT=c.attr("content")),""!==l.attr("data-okbtn-selector")&&(s.autoVerification.okBtn=l.attr("data-okbtn-selector")),""!==l.attr("data-recaptcha-apikey")&&(s.ReCAPTCHA_API_KEY_CLIENT=l.attr("data-recaptcha-apikey")),""!==l.attr("data-required-msg-example-selector")){let e=$(l.attr("data-required-msg-example-selector")).clone();e.removeClass("hidden"),e.removeClass("d-none"),s.autoVerification.requiredMsg=e}if(""!==l.attr("data-loading-msg-example-selector")){let e=$(l.attr("data-loading-msg-example-selector")).clone();e.removeClass("hidden"),e.removeClass("d-none"),s.apiScriptLoading.loadingMsg=e}if(""!==l.attr("data-error-msg-example-selector")){let e=$(l.attr("data-error-msg-example-selector")).clone();e.removeClass("hidden"),e.removeClass("d-none"),s.apiScriptLoading.errorMsg=e}""!==l.attr("data-theme")&&(s.theme=l.attr("data-theme")),d.settings=s,o(d,!1);let p=Math.floor(1e3*Math.random()+100);if(""!==l.attr("id")&&l.attr("id",p+"_ReCaptchaTargetParent"),d.parentId=l.attr("id"),d.elementId=d.parentId+"__child",$('
').appendTo(l),$('
').appendTo(l),d.AUTO_INIT){$('').appendTo(l)}n(d).then((function(){r(e,t+1,a,i)}),(function(e){s.failure(e??"Unknown error")})),l.data("EasyCaptcha",d)}}async function n(e){await new Promise((function(t,a){!async function(e,t,a){const r="https://www.google.com/recaptcha/api.js?onload=EasyCaptchaScriptCallback&render=explicit";let i=$('script[src="'+r+'"]');if(0===i.length||"true"===i.attr("loaded")){0!==i.length&&i.remove();let s=$("#"+a.parentId).find(".alertsParent");s.empty(),$(a.settings.apiScriptLoading.loadingMsg).appendTo(s),window.EasyCaptchaScriptCallback=()=>{$('script[src="'+r+'"]').attr("loaded","true"),e(),s.empty()};let o=document.createElement("script");o.src=r,o.async=!0,o.defer=!0,o.onerror=function(){t("EasyCaptchaScript_FAILED"),s.empty();let e=$(a.settings.apiScriptLoading.errorMsg);e.appendTo(s),e.find(".retry-load-api-script").on("click",(function(){n(a)}))},$("head")[0].appendChild(o)}else e()}(t,a,e)})).then((function(){!function(e){let t;i=window.grecaptcha,"string"==typeof e.settings.theme?t=e.settings.theme:"function"==typeof e.settings.theme&&(t=e.settings.theme());"light"!==t&&"dark"!==t&&(t="light");e.widgetId=i.render(e.elementId,{sitekey:e.settings.ReCAPTCHA_API_KEY_CLIENT,callback:()=>function(e){e.settings.ReCaptchaSubmit.success(),o(e,!0),s(e,i.getResponse(e.widgetId))}(e),"expired-callback":()=>function(e){e.settings.ReCaptchaSubmit.expired(),o(e,!1),s(e,"")}(e),"error-callback":()=>function(e){e.settings.ReCaptchaSubmit.failed(),o(e,!1),s(e,"")}(e),theme:t})}(e)}),(function(t){e.settings.apiScriptLoading.error(t)}))}let i;function s(e,t){e.AUTO_INIT&&$("#"+e.parentId).find('[name="g-recaptcha-response"]').val(t)}function o(e,t){if(null!=e.settings.autoVerification.okBtn){let a=$(e.settings.autoVerification.okBtn);a.prop("disabled",!t),t?a.removeClass("disabled"):a.addClass("disabled")}}return jQuery.fn.EasyCaptcha=a,$("[data-auto-easycaptcha]").EasyCaptcha(null),t})())); \ No newline at end of file +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.EasyCaptchaJS=t():e.EasyCaptchaJS=t()}(this,(()=>(()=>{"use strict";var e={d:(t,a)=>{for(var r in a)e.o(a,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:a[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{EasyCaptcha:()=>a});const a=function(e={},...t){let a=null===e;if(null===e&&(e={}),0===$(this).length&&!a)throw"no ReCaptcha div parent.";let n=[];const s={verify:function(e,t,a){if(0!==a.length){if(a[0].startsWith("#")&&(a[0]=a[0].substring(1)),a[0]===t.parentId){let a={verified:""!==i.getResponse(t.widgetId)};e.push(a)}}else{let a={parentElement:$("#"+t.parentId),verified:""!==i.getResponse(t.widgetId)};e.push(a)}return e},response:function(e,t,a){if(0!==a.length){if(a[0].startsWith("#")&&(a[0]=a[0].substring(1)),a[0]===t.parentId){let a={token:i.getResponse(t.widgetId)};e.push(a)}}else{let a={parentElement:$("#"+t.parentId),token:i.getResponse(t.widgetId)};e.push(a)}return e},reset:function(e,t,a){return 0!==a.length?(a[0].startsWith("#")&&(a[0]=a[0].substring(1)),a[0]===t.parentId&&i.reset(t.widgetId)):i.reset(),e},destroy:function(e,t){for(let e=0;e<$(this).length;e++)$("#"+t.parentId).empty(),$($(this)[e]).removeData("EasyCaptcha");return e}};if("object"==typeof e)r($(this),0,e,a);else for(let a=0;a<$(this).length;a++){let r=$($(this)[a]).data("EasyCaptcha");r&&void 0!==s[e]&&(n=s[e](n,r,[...t]))}return n.length>1?n:0===n.length?null:n[0]};function r(e,t,a,i){if(e.length===t)return;let s,d=$(e[t]),c=d.data("EasyCaptcha");if(c)r(e,t+1,a,i);else{c={},c.AUTO_INIT=i,s=$.extend({ReCAPTCHA_API_KEY_CLIENT:null,ReCaptchaSubmit:{success:()=>{},failed:()=>{},expired:()=>{}},autoVerification:{okBtn:null,requiredMsg:"
Please verify that you are not a robot.
"},apiScriptLoading:{loadingMsg:'
',error:()=>{},errorMsg:"
Error while loading Api Script. retry
"},theme:"light",failure:e=>{console.error(e)}},a);let p=$('meta[name="ReCAPTCHA_API_KEY_CLIENT"]');if(0!==p.length&&(s.ReCAPTCHA_API_KEY_CLIENT=p.attr("content")),l(d.attr("data-okbtn-selector"))&&(s.autoVerification.okBtn=d.attr("data-okbtn-selector")),l(d.attr("data-recaptcha-apikey"))&&(s.ReCAPTCHA_API_KEY_CLIENT=d.attr("data-recaptcha-apikey")),l(d.attr("data-required-msg-example-selector"))){let e=$(d.attr("data-required-msg-example-selector")).clone();e.removeClass("hidden"),e.removeClass("d-none"),s.autoVerification.requiredMsg=e}if(l(d.attr("data-loading-msg-example-selector"))){let e=$(d.attr("data-loading-msg-example-selector")).clone();e.removeClass("hidden"),e.removeClass("d-none"),s.apiScriptLoading.loadingMsg=e}if(l(d.attr("data-error-msg-example-selector"))){let e=$(d.attr("data-error-msg-example-selector")).clone();e.removeClass("hidden"),e.removeClass("d-none"),s.apiScriptLoading.errorMsg=e}l(d.attr("data-theme"))&&(s.theme=d.attr("data-theme")),c.settings=s,o(c,!1);let g=Math.floor(1e3*Math.random()+100);if(l(d.attr("id"))&&d.attr("id",g+"_ReCaptchaTargetParent"),c.parentId=d.attr("id"),c.elementId=c.parentId+"__child",$('
').appendTo(d),$('
').appendTo(d),c.AUTO_INIT){$('').appendTo(d)}d.data("EasyCaptcha",c),n(c).then((function(){r(e,t+1,a,i)}),(function(e){s.failure(e??"Unknown error")}))}}async function n(e){await new Promise((function(t,a){!async function(e,t,a){const r="https://www.google.com/recaptcha/api.js?onload=EasyCaptchaScriptCallback&render=explicit";let i=$('script[src="'+r+'"]');if(0===i.length||"true"===i.attr("loaded")){0!==i.length&&i.remove();let s=$("#"+a.parentId).find(".alertsParent");s.empty(),$(a.settings.apiScriptLoading.loadingMsg).appendTo(s),window.EasyCaptchaScriptCallback=()=>{$('script[src="'+r+'"]').attr("loaded","true"),e(),s.empty()};let o=document.createElement("script");o.src=r,o.async=!0,o.defer=!0,o.onerror=function(){t("EasyCaptchaScript_FAILED"),s.empty();let e=$(a.settings.apiScriptLoading.errorMsg);e.appendTo(s),e.find(".retry-load-api-script").on("click",(function(){n(a)}))},$("head")[0].appendChild(o)}else e()}(t,a,e)})).then((function(){!function(e){let t;i=window.grecaptcha,"string"==typeof e.settings.theme?t=e.settings.theme:"function"==typeof e.settings.theme&&(t=e.settings.theme());"light"!==t&&"dark"!==t&&(t="light");e.widgetId=i.render(e.elementId,{sitekey:e.settings.ReCAPTCHA_API_KEY_CLIENT,callback:()=>function(e){e.settings.ReCaptchaSubmit.success(),o(e,!0),s(e,i.getResponse(e.widgetId))}(e),"expired-callback":()=>function(e){e.settings.ReCaptchaSubmit.expired(),o(e,!1),s(e,"")}(e),"error-callback":()=>function(e){e.settings.ReCaptchaSubmit.failed(),o(e,!1),s(e,"")}(e),theme:t})}(e)}),(function(t){e.settings.apiScriptLoading.error(t)}))}let i;function s(e,t){e.AUTO_INIT&&$("#"+e.parentId).find('[name="g-recaptcha-response"]').val(t)}function o(e,t){if(null!=e.settings.autoVerification.okBtn){let a=$(e.settings.autoVerification.okBtn);a.prop("disabled",!t),t?a.removeClass("disabled"):a.addClass("disabled")}}function l(e){return null!=e&&""!==e}return jQuery.fn.EasyCaptcha=a,$("[data-auto-easycaptcha]").EasyCaptcha(null),t})())); \ No newline at end of file diff --git a/src/easycaptcha.js b/src/easycaptcha.js index 3041140..761844c 100644 --- a/src/easycaptcha.js +++ b/src/easycaptcha.js @@ -120,37 +120,37 @@ function handleOneChild($group, i, options, AUTO_INIT) { if (meta.length !== 0) { settings.ReCAPTCHA_API_KEY_CLIENT = meta.attr('content'); } - if ($this.attr('data-okbtn-selector') !== "") { + if (attrExist($this.attr('data-okbtn-selector'))) { settings.autoVerification.okBtn = $this.attr('data-okbtn-selector'); } - if ($this.attr('data-recaptcha-apikey') !== "") { + if (attrExist($this.attr('data-recaptcha-apikey'))) { settings.ReCAPTCHA_API_KEY_CLIENT = $this.attr('data-recaptcha-apikey'); } - if ($this.attr('data-required-msg-example-selector') !== "") { + if (attrExist($this.attr('data-required-msg-example-selector'))) { let e = $($this.attr('data-required-msg-example-selector')).clone(); e.removeClass('hidden'); e.removeClass('d-none'); settings.autoVerification.requiredMsg = e; } - if ($this.attr('data-loading-msg-example-selector') !== "") { + if (attrExist($this.attr('data-loading-msg-example-selector'))) { let e = $($this.attr('data-loading-msg-example-selector')).clone(); e.removeClass('hidden'); e.removeClass('d-none'); settings.apiScriptLoading.loadingMsg = e; } - if ($this.attr('data-error-msg-example-selector') !== "") { + if (attrExist($this.attr('data-error-msg-example-selector'))) { let e = $($this.attr('data-error-msg-example-selector')).clone(); e.removeClass('hidden'); e.removeClass('d-none'); settings.apiScriptLoading.errorMsg = e; } - if ($this.attr('data-theme') !== "") { + if (attrExist($this.attr('data-theme'))) { settings.theme = $this.attr('data-theme'); } data.settings = settings; changeBtnState(data, false); let idSuffix = Math.floor((Math.random() * 1000) + 100); - if ($this.attr('id') !== "") { + if (attrExist($this.attr('id'))) { $this.attr('id', idSuffix + "_ReCaptchaTargetParent"); } data.parentId = $this.attr('id'); @@ -163,6 +163,7 @@ function handleOneChild($group, i, options, AUTO_INIT) { let $hiddenInput = $(''); $hiddenInput.appendTo($this); } + $this.data('EasyCaptcha', data); startCheckingGoogleReCaptchaScript(data).then( function () { handleOneChild($group, i+1, options, AUTO_INIT); @@ -171,7 +172,6 @@ function handleOneChild($group, i, options, AUTO_INIT) { settings.failure(error??"Unknown error"); } ); - $this.data('EasyCaptcha', data); } else { handleOneChild($group, i+1, options, AUTO_INIT); } @@ -281,4 +281,8 @@ function changeBtnState(data, enable) { if (!enable) $okBtn.addClass('disabled'); else $okBtn.removeClass('disabled'); } +} + +function attrExist(attr) { + return typeof attr !== 'undefined' && attr !== null && attr !== ""; } \ No newline at end of file