svelte-recaptcha-v2
Version:
Google ReCaptcha v2 implementation for Svelte and SvelteKit
2 lines (1 loc) • 14.1 kB
JavaScript
import e from"tty";import t from"util";function n(){}function r(e){return e()}function o(){return Object.create(null)}function s(e){e.forEach(r)}function c(e){return"function"==typeof e}function i(e,t){return e!=e?t==t:e!==t||e&&"object"==typeof e||"function"==typeof e}function a(e){e.parentNode.removeChild(e)}function u(e,t,n){null==n?e.removeAttribute(t):e.getAttribute(t)!==n&&e.setAttribute(t,n)}let l;function d(e){l=e}function p(){if(!l)throw new Error("Function called outside component initialization");return l}function f(){const e=p();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 m=[],g=[],C=[],h=[],y=Promise.resolve();let w=!1;function b(e){C.push(e)}let F=!1;const v=new Set;function $(){if(!F){F=!0;do{for(let e=0;e<m.length;e+=1){const t=m[e];d(t),x(t.$$)}for(d(null),m.length=0;g.length;)g.pop()();for(let e=0;e<C.length;e+=1){const t=C[e];v.has(t)||(v.add(t),t())}C.length=0}while(m.length);for(;h.length;)h.pop()();w=!1,F=!1,v.clear()}}function x(e){if(null!==e.fragment){e.update(),s(e.before_update);const t=e.dirty;e.dirty=[-1],e.fragment&&e.fragment.p(e.ctx,t),e.after_update.forEach(b)}}const k=new Set;function E(e,t){-1===e.$$.dirty[0]&&(m.push(e),w||(w=!0,y.then($)),e.$$.dirty.fill(0)),e.$$.dirty[t/31|0]|=1<<t%31}function O(e,t,i,u,p,f,m,g=[-1]){const C=l;d(e);const h=e.$$={fragment:null,ctx:null,props:f,update:n,not_equal:p,bound:o(),on_mount:[],on_destroy:[],on_disconnect:[],before_update:[],after_update:[],context:new Map(t.context||(C?C.$$.context:[])),callbacks:o(),dirty:g,skip_bound:!1,root:t.target||C.$$.root};m&&m(h.root);let y=!1;if(h.ctx=i?i(e,t.props||{},((t,n,...r)=>{const o=r.length?r[0]:n;return h.ctx&&p(h.ctx[t],h.ctx[t]=o)&&(!h.skip_bound&&h.bound[t]&&h.bound[t](o),y&&E(e,t)),n})):[],h.update(),y=!0,s(h.before_update),h.fragment=!!u&&u(h.ctx),t.target){if(t.hydrate){const e=function(e){return Array.from(e.childNodes)}(t.target);h.fragment&&h.fragment.l(e),e.forEach(a)}else h.fragment&&h.fragment.c();t.intro&&((w=e.$$.fragment)&&w.i&&(k.delete(w),w.i(F))),function(e,t,n,o){const{fragment:i,on_mount:a,on_destroy:u,after_update:l}=e.$$;i&&i.m(t,n),o||b((()=>{const t=a.map(r).filter(c);u?u.push(...t):s(t),e.$$.on_mount=[]})),l.forEach(b)}(e,t.target,t.anchor,t.customElement),$()}var w,F;d(C)}const _=()=>{var e,t,n=new Promise(((n,r)=>{e=n,t=r}));return n.resolve=e,n.reject=t,n},j="object"==typeof window&&"object"==typeof window.document;var A={exports:{}},T=1e3,S=60*T,I=60*S,z=24*I,L=7*z,M=365.25*z,N=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*M;case"weeks":case"week":case"w":return n*L;case"days":case"day":case"d":return n*z;case"hours":case"hour":case"hrs":case"hr":case"h":return n*I;case"minutes":case"minute":case"mins":case"min":case"m":return n*S;case"seconds":case"second":case"secs":case"sec":case"s":return n*T;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>=z)return D(e,t,z,"day");if(t>=I)return D(e,t,I,"hour");if(t>=S)return D(e,t,S,"minute");if(t>=T)return D(e,t,T,"second");return e+" ms"}(e):function(e){var t=Math.abs(e);if(t>=z)return Math.round(e/z)+"d";if(t>=I)return Math.round(e/I)+"h";if(t>=S)return Math.round(e/S)+"m";if(t>=T)return Math.round(e/T)+"s";return e+"ms"}(e);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(e))};function D(e,t,n,r){var o=t>=1.5*n;return Math.round(e/n)+" "+r+(o?"s":"")}var R=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=N,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=R(t);const{formatters:n}=e.exports;n.j=function(e){try{return JSON.stringify(e)}catch(e){return"[UnexpectedJSONParseError]: "+e.message}}}(A,A.exports);var B={exports:{}};function P(e){let t;return{c(){var e;e="div",t=document.createElement(e),u(t,"id","googleRecaptchaDiv"),u(t,"class","g-recaptcha")},m(e,n){!function(e,t,n){e.insertBefore(t,n||null)}(e,t,n)},p:n,i:n,o:n,d(e){e&&a(t)}}}let U;!function(n,r){const o=e,s=t;r.init=function(e){e.inspectOpts={};const t=Object.keys(r.inspectOpts);for(let n=0;n<t.length;n++)e.inspectOpts[t[n]]=r.inspectOpts[t[n]]},r.log=function(...e){return process.stderr.write(s.format(...e)+"\n")},r.formatArgs=function(e){const{namespace:t,useColors:o}=this;if(o){const r=this.color,o="[3"+(r<8?r:"8;5;"+r),s=` ${o};1m${t} [0m`;e[0]=s+e[0].split("\n").join("\n"+s),e.push(o+"m+"+n.exports.humanize(this.diff)+"[0m")}else e[0]=function(){if(r.inspectOpts.hideDate)return"";return(new Date).toISOString()+" "}()+t+" "+e[0]},r.save=function(e){e?process.env.DEBUG=e:delete process.env.DEBUG},r.load=function(){return process.env.DEBUG},r.useColors=function(){return"colors"in r.inspectOpts?Boolean(r.inspectOpts.colors):o.isatty(process.stderr.fd)},r.destroy=s.deprecate((()=>{}),"Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`."),r.colors=[6,2,3,4,5,1];try{const e=require("supports-color");e&&(e.stderr||e).level>=2&&(r.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){}r.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}),{}),n.exports=R(r);const{formatters:c}=n.exports;c.o=function(e){return this.inspectOpts.colors=this.useColors,s.inspect(e,this.inspectOpts).split("\n").map((e=>e.trim())).join(" ")},c.O=function(e){return this.inspectOpts.colors=this.useColors,s.inspect(e,this.inspectOpts)}}(B,B.exports),"undefined"==typeof process||"renderer"===process.type||!0===process.browser||process.__nwjs;let H=_(),q="google.com/recaptcha/api2/bframe";function G(e,t,n){const r=f();let{sitekey:o}=t,{badge:s="top"}=t,{size:c="invisible"}=t,{sleepTime:i=0}=t,a=null,u=null,l=null,d=null,m=null;const g={onExpired:async()=>{U.reset(a)},onError:async e=>{r("error",{msg:"please check your site key"}),C.errors.push("empty"),U.reset(a)},onSuccess:async e=>{r("success",{msg:"ok",token:e}),setTimeout((()=>U.reset(a)),1e3),H=_()},onReady:()=>{r("ready")},onOpen:e=>{r("open")},onClose:e=>{j&&1===e.length&&!window.grecaptcha.getResponse()&&r("close")}},C={ready:!1,errors:[],retryTimer:!1,isLoaded:()=>(C.ready=!!(j&&window&&window.grecaptcha&&window.grecaptcha.ready&&document.getElementsByTagName("iframe").find((e=>e.src.includes(q)))),C.ready),stopTimer:()=>{clearInterval(C.retryTimer)},startTimer:()=>{C.retryTimer=setInterval((()=>{C.isLoaded()&&(C.stopTimer(),C.modal(),C.openHandle(),C.closeHandle(),g.onReady()),C.errors.length>3&&C.wipe()}),1e3)},modal:()=>{const e=document.getElementsByTagName("iframe");l=e.find((e=>e.src.includes(q))).parentNode.parentNode},openHandle:()=>{d=new MutationObserver((e=>1==l.style.opacity&&g.onOpen(e))),d.observe(l,{attributes:!0,attributeFilter:["style"]})},closeHandle:()=>{m=new MutationObserver((e=>0==l.style.opacity&&g.onClose(e))),m.observe(l,{attributes:!0,attributeFilter:["style"]})},inject:()=>{U=window.grecaptcha,window.grecaptcha.ready((()=>{a=grecaptcha.render("googleRecaptchaDiv",{badge:s,sitekey:o,callback:g.onSuccess,"expired-callback":g.onExpired,"error-callback":g.onError,size:c})}))},wipe:()=>{try{j&&(clearInterval(C.retryTimer),U&&(U.reset(a),delete window.grecaptcha,delete window.apiLoaded,delete window.recaptchaCloseListener,d&&d.disconnect(),m&&m.disconnect(),document.querySelectorAll("script[src*=recaptcha]").forEach((e=>{e.remove()})),document.querySelectorAll("iframe[src*=recaptcha]").forEach((e=>{e.remove()}))))}catch(e){}}},h=async()=>{u.resolve(!0)};var y;y=async()=>{if(j&&(window.apiLoaded=h),i&&await w(i),j){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)}u=_(),await Promise.resolve(u),j&&C.inject(),j&&(HTMLCollection.prototype.find=Array.prototype.find),C.startTimer()},p().$$.on_mount.push(y),function(e){p().$$.on_destroy.push(e)}((async()=>{C.wipe()}));const w=e=>new Promise((t=>setTimeout(t,1e3*e))).catch((e=>{}));return e.$$set=e=>{"sitekey"in e&&n(0,o=e.sitekey),"badge"in e&&n(1,s=e.badge),"size"in e&&n(2,c=e.size),"sleepTime"in e&&n(3,i=e.sleepTime)},[o,s,c,i]}class J extends class{$destroy(){!function(e,t){const n=e.$$;null!==n.fragment&&(s(n.on_destroy),n.fragment&&n.fragment.d(t),n.on_destroy=n.fragment=null,n.ctx=[])}(this,1),this.$destroy=n}$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(),O(this,e,G,P,i,{sitekey:0,badge:1,size:2,sleepTime:3})}}export{J as Recaptcha,H as observer,U as recaptcha};