vue3-smart-captcha
Version:
Yandex Smart Captcha for Vue3 projects
2 lines (1 loc) • 3.66 kB
JavaScript
var R=Object.defineProperty;var T=(a,e,s)=>e in a?R(a,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):a[e]=s;var v=(a,e,s)=>T(a,typeof e!="symbol"?e+"":e,s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const l=require("vue");class m{constructor(){v(this,"SCRIPT_RENDER_ONLOAD_SRC","https://smartcaptcha.yandexcloud.net/captcha.js?render=onload")}execute(e){var s;(s=window.smartCaptcha)==null||s.execute(e)}reset(e){var s;(s=window.smartCaptcha)==null||s.reset(e)}destroy(e){var s;(s=window.smartCaptcha)==null||s.destroy(e)}getResponse(e){var s;return(s=window.smartCaptcha)==null?void 0:s.getResponse(e)}subscribe(e,s,c){var i;(i=window.smartCaptcha)==null||i.subscribe(e,s,c)}}const b=(a,e,s=!0,c=2e3)=>{const i=new m,o=l.ref(),r=l.ref(),n=l.ref(),d={success:[],"javascript-error":[],"network-error":[],"token-expired":[],"challenge-hidden":[],"challenge-visible":[]},_=()=>{typeof a=="string"?n.value=document.querySelector(a):a instanceof HTMLElement?n.value=a:a.value&&(n.value=a.value)},f=()=>{const t=document.createElement("script");t.src=i.SCRIPT_RENDER_ONLOAD_SRC,t.defer=!0,t.type="text/javascript",t.crossOrigin="anonymous",t.dataset.captcha="true",document.head.appendChild(t)},w=()=>{if(typeof e=="string")return{sitekey:e};const t=(e==null?void 0:e.sitekey)||void 0;if(!t)throw new Error("Captcha key is not defined. Please provide value or specify `VITE_SMARTCAPTCHA_KEY` environment variable");return{...e,sitekey:t}},C=t=>{r.value=t},y=t=>{d[t].length>0&&d[t].forEach(u=>{i.subscribe(o.value,t,u)})},g=()=>{o.value!==void 0&&(i.subscribe(o.value,"success",C),Array.from(Object.keys(d)).forEach(t=>{y(t)}))},k=()=>{n.value&&window.smartCaptcha!==void 0&&(o.value=window.smartCaptcha.render(n.value,w()),g())};l.onMounted(()=>{_(),window.smartCaptcha===void 0&&s&&f();const t=10,u=setInterval(()=>{let E=0;if(++E===t){console.warn(`The captcha could not be initialized in ${c}ms. Make sure the widget script has been loaded`),clearInterval(u);return}window.smartCaptcha!==void 0&&(clearInterval(u),k())},c/t)}),l.onUnmounted(()=>{var t;s&&((t=document.querySelector("script[data-captcha]"))==null||t.remove())});const S=()=>{i.execute(o.value)},h=()=>{o.value=void 0,r.value=void 0};return{widgetId:o,token:r,execute:S,reset:()=>{i.reset(o.value),h()},destroy:()=>{i.destroy(o.value),h()},getResponse:()=>i.getResponse(o.value),subscribeTo:(t,u)=>{d[t].push(u)}}},p=l.defineComponent({__name:"SmartCaptcha",props:{sitekey:{},callback:{},hl:{},test:{type:Boolean},webview:{type:Boolean},invisible:{type:Boolean},shieldPosition:{},hideShield:{type:Boolean},loadWidget:{type:Boolean,default:!0},timeout:{default:2e3}},emits:["challengeHidden","challengeVisible","javascriptError","networkError","tokenExpired","success","initialized"],setup(a,{emit:e}){const s=l.ref(),c=a,{subscribeTo:i,widgetId:o}=b(s,{sitekey:c.sitekey,callback:c.callback,hl:c.hl,test:c.test,webview:c.webview,invisible:c.invisible,shieldPosition:c.shieldPosition,hideShield:c.hideShield},c.loadWidget,c.timeout),r=e;return l.watch(o,n=>{n!==void 0&&r("initialized",n)}),i("challenge-hidden",()=>{r("challengeHidden")}),i("challenge-visible",()=>{r("challengeVisible")}),i("javascript-error",n=>{r("javascriptError",n)}),i("network-error",()=>{r("networkError")}),i("token-expired",()=>{r("tokenExpired")}),i("success",n=>{r("success",n)}),(n,d)=>(l.openBlock(),l.createElementBlock("div",{ref_key:"container",ref:s},null,512))}}),x={install:a=>{a.component("SmartCaptcha",p)}};exports.SmartCaptcha=p;exports.SmartCaptchaPlugin=x;exports.SmartCaptchaUtils=m;exports.default=p;exports.useSmartCaptcha=b;
;