UNPKG

svelte-recaptcha-v2

Version:

Google ReCaptcha v2 implementation for Svelte and SvelteKit

2 lines (1 loc) 14.6 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("tty"),require("util")):"function"==typeof define&&define.amd?define(["exports","tty","util"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).window=e.window||{},e.require$$0,e.require$$1)}(this,(function(e,t,n){"use strict";function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=r(t),s=r(n);function c(){}function i(e){return e()}function a(){return Object.create(null)}function u(e){e.forEach(i)}function l(e){return"function"==typeof e}function d(e,t){return e!=e?t==t:e!==t||e&&"object"==typeof e||"function"==typeof e}function p(e){e.parentNode.removeChild(e)}function f(e,t,n){null==n?e.removeAttribute(t):e.getAttribute(t)!==n&&e.setAttribute(t,n)}let m;function h(e){m=e}function g(){if(!m)throw new Error("Function called outside component initialization");return m}function C(){const e=g();return(t,n)=>{const r=e.$$.callbacks[t];if(r){const o=function(e,t,n=!1){const r=document.createEvent("CustomEvent");return r.initCustomEvent(e,n,!1,t),r}(t,n);r.slice().forEach((t=>{t.call(e,o)}))}}}const y=[],b=[],w=[],F=[],v=Promise.resolve();let $=!1;function x(e){w.push(e)}let k=!1;const E=new Set;function O(){if(!k){k=!0;do{for(let e=0;e<y.length;e+=1){const t=y[e];h(t),_(t.$$)}for(h(null),y.length=0;b.length;)b.pop()();for(let e=0;e<w.length;e+=1){const t=w[e];E.has(t)||(E.add(t),t())}w.length=0}while(y.length);for(;F.length;)F.pop()();$=!1,k=!1,E.clear()}}function _(e){if(null!==e.fragment){e.update(),u(e.before_update);const t=e.dirty;e.dirty=[-1],e.fragment&&e.fragment.p(e.ctx,t),e.after_update.forEach(x)}}const j=new Set;function A(e,t){-1===e.$$.dirty[0]&&(y.push(e),$||($=!0,v.then(O)),e.$$.dirty.fill(0)),e.$$.dirty[t/31|0]|=1<<t%31}function T(e,t,n,r,o,s,d,f=[-1]){const g=m;h(e);const C=e.$$={fragment:null,ctx:null,props:s,update:c,not_equal:o,bound:a(),on_mount:[],on_destroy:[],on_disconnect:[],before_update:[],after_update:[],context:new Map(t.context||(g?g.$$.context:[])),callbacks:a(),dirty:f,skip_bound:!1,root:t.target||g.$$.root};d&&d(C.root);let y=!1;if(C.ctx=n?n(e,t.props||{},((t,n,...r)=>{const s=r.length?r[0]:n;return C.ctx&&o(C.ctx[t],C.ctx[t]=s)&&(!C.skip_bound&&C.bound[t]&&C.bound[t](s),y&&A(e,t)),n})):[],C.update(),y=!0,u(C.before_update),C.fragment=!!r&&r(C.ctx),t.target){if(t.hydrate){const e=function(e){return Array.from(e.childNodes)}(t.target);C.fragment&&C.fragment.l(e),e.forEach(p)}else C.fragment&&C.fragment.c();t.intro&&((b=e.$$.fragment)&&b.i&&(j.delete(b),b.i(w))),function(e,t,n,r){const{fragment:o,on_mount:s,on_destroy:c,after_update:a}=e.$$;o&&o.m(t,n),r||x((()=>{const t=s.map(i).filter(l);c?c.push(...t):u(t),e.$$.on_mount=[]})),a.forEach(x)}(e,t.target,t.anchor,t.customElement),O()}var b,w;h(g)}const S=()=>{var e,t,n=new Promise(((n,r)=>{e=n,t=r}));return n.resolve=e,n.reject=t,n},I="object"==typeof window&&"object"==typeof window.document;var M={exports:{}},z=1e3,L=60*z,N=60*L,R=24*N,D=7*R,q=365.25*R,B=function(e,t){t=t||{};var n=typeof e;if("string"===n&&e.length>0)return function(e){if((e=String(e)).length>100)return;var t=/^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(e);if(!t)return;var n=parseFloat(t[1]);switch((t[2]||"ms").toLowerCase()){case"years":case"year":case"yrs":case"yr":case"y":return n*q;case"weeks":case"week":case"w":return n*D;case"days":case"day":case"d":return n*R;case"hours":case"hour":case"hrs":case"hr":case"h":return n*N;case"minutes":case"minute":case"mins":case"min":case"m":return n*L;case"seconds":case"second":case"secs":case"sec":case"s":return n*z;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return n;default:return}}(e);if("number"===n&&isFinite(e))return t.long?function(e){var t=Math.abs(e);if(t>=R)return P(e,t,R,"day");if(t>=N)return P(e,t,N,"hour");if(t>=L)return P(e,t,L,"minute");if(t>=z)return P(e,t,z,"second");return e+" ms"}(e):function(e){var t=Math.abs(e);if(t>=R)return Math.round(e/R)+"d";if(t>=N)return Math.round(e/N)+"h";if(t>=L)return Math.round(e/L)+"m";if(t>=z)return Math.round(e/z)+"s";return e+"ms"}(e);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(e))};function P(e,t,n,r){var o=t>=1.5*n;return Math.round(e/n)+" "+r+(o?"s":"")}var U=function(e){function t(e){let r,o,s,c=null;function i(...e){if(!i.enabled)return;const n=i,o=Number(new Date),s=o-(r||o);n.diff=s,n.prev=r,n.curr=o,r=o,e[0]=t.coerce(e[0]),"string"!=typeof e[0]&&e.unshift("%O");let c=0;e[0]=e[0].replace(/%([a-zA-Z%])/g,((r,o)=>{if("%%"===r)return"%";c++;const s=t.formatters[o];if("function"==typeof s){const t=e[c];r=s.call(n,t),e.splice(c,1),c--}return r})),t.formatArgs.call(n,e);(n.log||t.log).apply(n,e)}return i.namespace=e,i.useColors=t.useColors(),i.color=t.selectColor(e),i.extend=n,i.destroy=t.destroy,Object.defineProperty(i,"enabled",{enumerable:!0,configurable:!1,get:()=>null!==c?c:(o!==t.namespaces&&(o=t.namespaces,s=t.enabled(e)),s),set:e=>{c=e}}),"function"==typeof t.init&&t.init(i),i}function n(e,t){const n=void 0;return n.log=this.log,n}function r(e){return e.toString().substring(2,e.toString().length-2).replace(/\.\*\?$/,"*")}return t.debug=t,t.default=t,t.coerce=function(e){if(e instanceof Error)return e.stack||e.message;return e},t.disable=function(){const e=[...t.names.map(r),...t.skips.map(r).map((e=>"-"+e))].join(",");return t.enable(""),e},t.enable=function(e){let n;t.save(e),t.namespaces=e,t.names=[],t.skips=[];const r=("string"==typeof e?e:"").split(/[\s,]+/),o=r.length;for(n=0;n<o;n++)r[n]&&("-"===(e=r[n].replace(/\*/g,".*?"))[0]?t.skips.push(new RegExp("^"+e.substr(1)+"$")):t.names.push(new RegExp("^"+e+"$")))},t.enabled=function(e){if("*"===e[e.length-1])return!0;let n,r;for(n=0,r=t.skips.length;n<r;n++)if(t.skips[n].test(e))return!1;for(n=0,r=t.names.length;n<r;n++)if(t.names[n].test(e))return!0;return!1},t.humanize=B,t.destroy=function(){console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.")},Object.keys(e).forEach((n=>{t[n]=e[n]})),t.names=[],t.skips=[],t.formatters={},t.selectColor=function(e){let n=0;for(let t=0;t<e.length;t++)n=(n<<5)-n+e.charCodeAt(t),n|=0;return t.colors[Math.abs(n)%t.colors.length]},t.enable(t.load()),t};!function(e,t){t.formatArgs=function(t){if(t[0]=(this.useColors?"%c":"")+this.namespace+(this.useColors?" %c":" ")+t[0]+(this.useColors?"%c ":" ")+"+"+e.exports.humanize(this.diff),!this.useColors)return;const n="color: "+this.color;t.splice(1,0,n,"color: inherit");let r=0,o=0;t[0].replace(/%[a-zA-Z%]/g,(e=>{"%%"!==e&&(r++,"%c"===e&&(o=r))})),t.splice(o,0,n)},t.save=function(e){try{e?t.storage.setItem("debug",e):t.storage.removeItem("debug")}catch(e){}},t.load=function(){let e;try{e=t.storage.getItem("debug")}catch(e){}!e&&"undefined"!=typeof process&&"env"in process&&(e=process.env.DEBUG);return e},t.useColors=function(){if("undefined"!=typeof window&&window.process&&("renderer"===window.process.type||window.process.__nwjs))return!0;if("undefined"!=typeof navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/))return!1;return"undefined"!=typeof document&&document.documentElement&&document.documentElement.style&&document.documentElement.style.WebkitAppearance||"undefined"!=typeof window&&window.console&&(window.console.firebug||window.console.exception&&window.console.table)||"undefined"!=typeof navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)&&parseInt(RegExp.$1,10)>=31||"undefined"!=typeof navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/)},t.storage=function(){try{return localStorage}catch(e){}}(),t.destroy=(()=>{let e=!1;return()=>{e||(e=!0,console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`."))}})(),t.colors=["#0000CC","#0000FF","#0033CC","#0033FF","#0066CC","#0066FF","#0099CC","#0099FF","#00CC00","#00CC33","#00CC66","#00CC99","#00CCCC","#00CCFF","#3300CC","#3300FF","#3333CC","#3333FF","#3366CC","#3366FF","#3399CC","#3399FF","#33CC00","#33CC33","#33CC66","#33CC99","#33CCCC","#33CCFF","#6600CC","#6600FF","#6633CC","#6633FF","#66CC00","#66CC33","#9900CC","#9900FF","#9933CC","#9933FF","#99CC00","#99CC33","#CC0000","#CC0033","#CC0066","#CC0099","#CC00CC","#CC00FF","#CC3300","#CC3333","#CC3366","#CC3399","#CC33CC","#CC33FF","#CC6600","#CC6633","#CC9900","#CC9933","#CCCC00","#CCCC33","#FF0000","#FF0033","#FF0066","#FF0099","#FF00CC","#FF00FF","#FF3300","#FF3333","#FF3366","#FF3399","#FF33CC","#FF33FF","#FF6600","#FF6633","#FF9900","#FF9933","#FFCC00","#FFCC33"],t.log=console.debug||console.log||(()=>{}),e.exports=U(t);const{formatters:n}=e.exports;n.j=function(e){try{return JSON.stringify(e)}catch(e){return"[UnexpectedJSONParseError]: "+e.message}}}(M,M.exports);var H={exports:{}};function G(e){let t;return{c(){var e;e="div",t=document.createElement(e),f(t,"id","googleRecaptchaDiv"),f(t,"class","g-recaptcha")},m(e,n){!function(e,t,n){e.insertBefore(t,n||null)}(e,t,n)},p:c,i:c,o:c,d(e){e&&p(t)}}}!function(e,t){const n=o.default,r=s.default;t.init=function(e){e.inspectOpts={};const n=Object.keys(t.inspectOpts);for(let r=0;r<n.length;r++)e.inspectOpts[n[r]]=t.inspectOpts[n[r]]},t.log=function(...e){return process.stderr.write(r.format(...e)+"\n")},t.formatArgs=function(n){const{namespace:r,useColors:o}=this;if(o){const t=this.color,o="[3"+(t<8?t:"8;5;"+t),s=` ${o};1m${r} `;n[0]=s+n[0].split("\n").join("\n"+s),n.push(o+"m+"+e.exports.humanize(this.diff)+"")}else n[0]=function(){if(t.inspectOpts.hideDate)return"";return(new Date).toISOString()+" "}()+r+" "+n[0]},t.save=function(e){e?process.env.DEBUG=e:delete process.env.DEBUG},t.load=function(){return process.env.DEBUG},t.useColors=function(){return"colors"in t.inspectOpts?Boolean(t.inspectOpts.colors):n.isatty(process.stderr.fd)},t.destroy=r.deprecate((()=>{}),"Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`."),t.colors=[6,2,3,4,5,1];try{const e=require("supports-color");e&&(e.stderr||e).level>=2&&(t.colors=[20,21,26,27,32,33,38,39,40,41,42,43,44,45,56,57,62,63,68,69,74,75,76,77,78,79,80,81,92,93,98,99,112,113,128,129,134,135,148,149,160,161,162,163,164,165,166,167,168,169,170,171,172,173,178,179,184,185,196,197,198,199,200,201,202,203,204,205,206,207,208,209,214,215,220,221])}catch(e){}t.inspectOpts=Object.keys(process.env).filter((e=>/^debug_/i.test(e))).reduce(((e,t)=>{const n=t.substring(6).toLowerCase().replace(/_([a-z])/g,((e,t)=>t.toUpperCase()));let r=process.env[t];return r=!!/^(yes|on|true|enabled)$/i.test(r)||!/^(no|off|false|disabled)$/i.test(r)&&("null"===r?null:Number(r)),e[n]=r,e}),{}),e.exports=U(t);const{formatters:c}=e.exports;c.o=function(e){return this.inspectOpts.colors=this.useColors,r.inspect(e,this.inspectOpts).split("\n").map((e=>e.trim())).join(" ")},c.O=function(e){return this.inspectOpts.colors=this.useColors,r.inspect(e,this.inspectOpts)}}(H,H.exports),"undefined"==typeof process||"renderer"===process.type||!0===process.browser||process.__nwjs,e.recaptcha=void 0,e.observer=S();let J="google.com/recaptcha/api2/bframe";function Z(t,n,r){const o=C();let{sitekey:s}=n,{badge:c="top"}=n,{size:i="invisible"}=n,{sleepTime:a=0}=n,u=null,l=null,d=null,p=null,f=null;const m={onExpired:async()=>{e.recaptcha.reset(u)},onError:async t=>{o("error",{msg:"please check your site key"}),h.errors.push("empty"),e.recaptcha.reset(u)},onSuccess:async t=>{o("success",{msg:"ok",token:t}),setTimeout((()=>e.recaptcha.reset(u)),1e3),e.observer=S()},onReady:()=>{o("ready")},onOpen:e=>{o("open")},onClose:e=>{I&&1===e.length&&!window.grecaptcha.getResponse()&&o("close")}},h={ready:!1,errors:[],retryTimer:!1,isLoaded:()=>(h.ready=!!(I&&window&&window.grecaptcha&&window.grecaptcha.ready&&document.getElementsByTagName("iframe").find((e=>e.src.includes(J)))),h.ready),stopTimer:()=>{clearInterval(h.retryTimer)},startTimer:()=>{h.retryTimer=setInterval((()=>{h.isLoaded()&&(h.stopTimer(),h.modal(),h.openHandle(),h.closeHandle(),m.onReady()),h.errors.length>3&&h.wipe()}),1e3)},modal:()=>{const e=document.getElementsByTagName("iframe");d=e.find((e=>e.src.includes(J))).parentNode.parentNode},openHandle:()=>{p=new MutationObserver((e=>1==d.style.opacity&&m.onOpen(e))),p.observe(d,{attributes:!0,attributeFilter:["style"]})},closeHandle:()=>{f=new MutationObserver((e=>0==d.style.opacity&&m.onClose(e))),f.observe(d,{attributes:!0,attributeFilter:["style"]})},inject:()=>{e.recaptcha=window.grecaptcha,window.grecaptcha.ready((()=>{u=grecaptcha.render("googleRecaptchaDiv",{badge:c,sitekey:s,callback:m.onSuccess,"expired-callback":m.onExpired,"error-callback":m.onError,size:i})}))},wipe:()=>{try{I&&(clearInterval(h.retryTimer),e.recaptcha&&(e.recaptcha.reset(u),delete window.grecaptcha,delete window.apiLoaded,delete window.recaptchaCloseListener,p&&p.disconnect(),f&&f.disconnect(),document.querySelectorAll("script[src*=recaptcha]").forEach((e=>{e.remove()})),document.querySelectorAll("iframe[src*=recaptcha]").forEach((e=>{e.remove()}))))}catch(e){}}},y=async()=>{l.resolve(!0)};var b;b=async()=>{if(I&&(window.apiLoaded=y),a&&await w(a),I){const e=document.createElement("script");e.id="googleRecaptchaScript",e.src="https://www.google.com/recaptcha/api.js?render=explicit&sitekey{sitekey}&onload=apiLoaded",e.async=!0,e.defer=!0,document.head.appendChild(e)}l=S(),await Promise.resolve(l),I&&h.inject(),I&&(HTMLCollection.prototype.find=Array.prototype.find),h.startTimer()},g().$$.on_mount.push(b),function(e){g().$$.on_destroy.push(e)}((async()=>{h.wipe()}));const w=e=>new Promise((t=>setTimeout(t,1e3*e))).catch((e=>{}));return t.$$set=e=>{"sitekey"in e&&r(0,s=e.sitekey),"badge"in e&&r(1,c=e.badge),"size"in e&&r(2,i=e.size),"sleepTime"in e&&r(3,a=e.sleepTime)},[s,c,i,a]}e.Recaptcha=class extends class{$destroy(){!function(e,t){const n=e.$$;null!==n.fragment&&(u(n.on_destroy),n.fragment&&n.fragment.d(t),n.on_destroy=n.fragment=null,n.ctx=[])}(this,1),this.$destroy=c}$on(e,t){const n=this.$$.callbacks[e]||(this.$$.callbacks[e]=[]);return n.push(t),()=>{const e=n.indexOf(t);-1!==e&&n.splice(e,1)}}$set(e){var t;this.$$set&&(t=e,0!==Object.keys(t).length)&&(this.$$.skip_bound=!0,this.$$set(e),this.$$.skip_bound=!1)}}{constructor(e){super(),T(this,e,Z,G,d,{sitekey:0,badge:1,size:2,sleepTime:3})}},Object.defineProperty(e,"__esModule",{value:!0})}));