UNPKG

@gladesinger/vue3-yandex-smartcaptcha

Version:
2 lines (1 loc) 3.47 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={__name:"YandexSmartCaptcha",props:{siteKey:{type:String,required:!0},language:{type:String,default:"ru",validator:c=>["ru","en","be","kk","tt","uk","uz","tr"].includes(c)},test:{type:Boolean,default:!1},webview:{type:Boolean,default:!1},invisible:{type:Boolean,default:!1},shieldPosition:{type:String,default:"bottom-right"},hideShield:{type:Boolean,default:!1},loadWidget:{type:Boolean,default:!0}},emits:["callback","onChallengeVisible","onChallengeHidden","onNetworkError","onJavaScriptError","onSuccess","onTokenExpired"],setup(c,{expose:l,emit:g}){const o=c,i=g,e=n.ref("captcha-container");let t;const d=()=>{var a,r,u,p,h,w,m;if(window&&document&&(window!=null&&window.smartCaptcha)&&e.value){const f=document==null?void 0:document.getElementById(e.value);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:v}),(r=window==null?void 0:window.smartCaptcha)==null||r.subscribe(e.value,"success",S),(u=window==null?void 0:window.smartCaptcha)==null||u.subscribe(e.value,"challenge-visible",y),(p=window==null?void 0:window.smartCaptcha)==null||p.subscribe(e.value,"challenge-hidden",C),(h=window==null?void 0:window.smartCaptcha)==null||h.subscribe(e.value,"network-error",k),(w=window==null?void 0:window.smartCaptcha)==null||w.subscribe(e.value,"javascript-error",x),(m=window==null?void 0:window.smartCaptcha)==null||m.subscribe(e.value,"token-expired",E))}},v=a=>{i("callback",a)},y=()=>{i("onChallengeVisible")},C=()=>{i("onChallengeHidden")},k=()=>{i("onNetworkError",{message:"Network error occured"})},x=a=>{i("onJavaScriptError",a)},S=a=>{i("onSuccess",a)},E=()=>{i("onTokenExpired")},B=()=>{window!=null&&window.smartCaptcha&&window.smartCaptcha.execute(e.value)},j=()=>{window!=null&&window.smartCaptcha&&window.smartCaptcha.getResponse(e.value)},T=()=>{window!=null&&window.smartCaptcha&&window.smartCaptcha.reset(e.value)},_=()=>{window!=null&&window.smartCaptcha&&window.smartCaptcha.destroy(e.value)},N=(a,r)=>{window!=null&&window.smartCaptcha&&window.smartCaptcha.subscribe(e.value,a,r)},V=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",d):(t=document.createElement("script"),t.src="https://smartcaptcha.yandexcloud.net/captcha.js?render=onload&onload=onloadFunction",t.type="text/javascript",t.defer=!0,t.crossOrigin="anonymous",document.head.appendChild(t),t.onload=function(){t.loaded=!0,d()}))}),n.onBeforeUnmount(()=>{if(window&&(window!=null&&window.smartCaptcha))try{window.smartCaptcha.destroy(e.value)}catch{}document&&document.getElementsByClassName("smart-captcha").length==1&&t&&t.remove()}),l({widgetId:e,execute:B,getResponse:j,reset:T,destroy:_,subscribe:N});const P=()=>n.h("div",{style:V.value,id:e.value,class:"smart-captcha"});return(a,r)=>(n.openBlock(),n.createBlock(P))}};s.YandexSmartCaptcha=b,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});