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