UNPKG

@gladesinger/vue3-yandex-smartcaptcha

Version:
2 lines (1 loc) 3.66 kB
(function(s,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],n):(s=typeof globalThis<"u"?globalThis:s||self,n(s.Vue3YandexSmartcaptcha={},s.Vue))})(this,function(s,n){"use strict";const b=n.defineComponent({name:"YandexSmartCaptcha",__name:"YandexSmartCaptcha",props:{siteKey:{},language:{default:"ru"},test:{type:Boolean,default:!1},webview:{type:Boolean,default:!1},invisible:{type:Boolean,default:!1},shieldPosition:{default:"bottom-right"},hideShield:{type:Boolean,default:!1},loadWidget:{type:Boolean,default:!0},defer:{type:Boolean,default:!0},async:{type:Boolean,default:!0}},emits:["callback","onChallengeVisible","onChallengeHidden","onNetworkError","onJavaScriptError","onSuccess","onTokenExpired"],setup(v,{expose:y,emit:C}){const o=v,i=C,e=n.ref(null),c=`smart-captcha-${Math.random().toString(36).substring(2,15)}`;let t;const d=()=>{var a,r,u,p,h,m,w;if(window&&document&&(window!=null&&window.smartCaptcha)){const f=document==null?void 0:document.getElementById(c);f&&(e.value=(a=window==null?void 0:window.smartCaptcha)==null?void 0:a.render(f,{sitekey:o.siteKey,hl:o.language,test:o.test,webview:o.webview,invisible:o.invisible,shieldPosition:o.shieldPosition,hideShield:o.hideShield,callback:g}),(r=window==null?void 0:window.smartCaptcha)==null||r.subscribe(e.value,"success",B),(u=window==null?void 0:window.smartCaptcha)==null||u.subscribe(e.value,"challenge-visible",x),(p=window==null?void 0:window.smartCaptcha)==null||p.subscribe(e.value,"challenge-hidden",k),(h=window==null?void 0:window.smartCaptcha)==null||h.subscribe(e.value,"network-error",E),(m=window==null?void 0:window.smartCaptcha)==null||m.subscribe(e.value,"javascript-error",S),(w=window==null?void 0:window.smartCaptcha)==null||w.subscribe(e.value,"token-expired",j))}},l=()=>{if(window&&(window!=null&&window.smartCaptcha)&&e.value!==null)try{window.smartCaptcha.destroy(e.value)}catch{}document&&document.getElementsByClassName("smart-captcha").length==1&&t&&t.remove()},g=a=>{i("callback",a)},x=()=>{i("onChallengeVisible")},k=()=>{i("onChallengeHidden")},E=()=>{i("onNetworkError",{message:"Network error occured"})},S=a=>{i("onJavaScriptError",a)},B=a=>{i("onSuccess",a)},j=()=>{i("onTokenExpired")},T=()=>{window!=null&&window.smartCaptcha&&e.value!==null&&window.smartCaptcha.execute(e.value)},_=()=>{if(window!=null&&window.smartCaptcha&&e.value!==null)return window.smartCaptcha.getResponse(e.value)},N=()=>{window!=null&&window.smartCaptcha&&e.value!==null&&window.smartCaptcha.reset(e.value)},V=()=>{l()},Y=(a,r)=>{window!=null&&window.smartCaptcha&&e.value!==null&&window.smartCaptcha.subscribe(e.value,a,r)},P=n.computed(()=>o.invisible?{height:0,width:0}:{width:"100px"});n.onMounted(()=>{o.loadWidget&&(t=Array.from(document.getElementsByTagName("script")).find(a=>a.src.includes("smartcaptcha.yandexcloud.net/captcha.js")),t?t.loaded?d():t.addEventListener("load",function a(){t&&(t.removeEventListener("load",a),t.loaded=!0,d())}):(t=document.createElement("script"),t.src="https://smartcaptcha.yandexcloud.net/captcha.js?render=onload",t.type="text/javascript",t.crossOrigin="anonymous",o.defer&&(t.defer=!0),o.async&&(t.async=!0),document.head.appendChild(t),t.onload=function(){t.loaded=!0,d()}))}),n.onBeforeUnmount(()=>{l()}),y({widgetId:e,containerId:c,subscribe:Y,execute:T,getResponse:_,reset:N,destroy:V});const H=()=>n.h("div",{style:P.value,id:c,class:"smart-captcha","data-widget-id":e.value,"data-sitekey":o.siteKey});return(a,r)=>(n.openBlock(),n.createBlock(H))}});s.YandexSmartCaptcha=b,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});