UNPKG

@getjoystick/vue-turnstile

Version:

Vue library for Cloudflare Turnstile CAPTCHA alternative

2 lines (1 loc) 2.23 kB
(function(t,n){typeof exports=="object"&&typeof module<"u"?module.exports=n(require("vue")):typeof define=="function"&&define.amd?define(["vue"],n):(t=typeof globalThis<"u"?globalThis:t||self,t.VueTurnstile=n(t.Vue))})(this,function(t){"use strict";const n="https://challenges.cloudflare.com/turnstile/v0/api.js",d="cfTurnstileOnLoad";let s=window.turnstile!==void 0?"ready":"unloaded",o;const a=t.defineComponent({name:"VueTurnstile",emits:["update:modelValue"],props:{siteKey:{type:String,required:!0},modelValue:{type:String,required:!0},resetInterval:{type:Number,required:!1,default:295*1e3},size:{type:String,required:!1,default:"normal"},theme:{type:String,required:!1,default:"auto"},action:{type:String,required:!1,default:""},renderOnMount:{type:Boolean,required:!1,default:!0},removeBeforeUnMount:{type:Boolean,required:!1,default:!0}},data(){return{timeoutId:void 0,turnstileWidgetId:void 0}},computed:{turnstileOptions(){return{sitekey:this.siteKey,theme:this.theme,size:this.size,callback:this.callback,action:this.action}}},methods:{callback(r){this.$emit("update:modelValue",r),this.startResetTimeout()},reset(){window.turnstile&&(this.$emit("update:modelValue",""),window.turnstile.reset())},render(){this.turnstileWidgetId=window.turnstile.render(this.$refs.turnstile,this.turnstileOptions)},remove(){window.turnstile&&this.turnstileWidgetId&&window.turnstile.remove(this.turnstileWidgetId),this.timeoutId&&clearTimeout(this.timeoutId)},startResetTimeout(){this.timeoutId=setTimeout(()=>{this.reset()},this.resetInterval)}},async mounted(){const r=new Promise((i,e)=>{o={resolve:i,reject:e},s==="ready"&&i(void 0)});window[d]=()=>{o.resolve(),s="ready"},await(()=>{if(s==="unloaded"){s="loading";const i=`${n}?onload=${d}&render=explicit`,e=document.createElement("script");e.src=i,e.async=!0,e.addEventListener("error",()=>{o.reject("Failed to load Turnstile.")}),document.head.appendChild(e)}return r})(),this.renderOnMount&&this.render()},async beforeUnmount(){this.removeBeforeUnMount&&this.remove()}}),c=(r,u)=>{const i=r.__vccOpts||r;for(const[e,l]of u)i[e]=l;return i},f={ref:"turnstile"};function m(r,u,i,e,l,h){return t.openBlock(),t.createElementBlock("div",f,null,512)}return c(a,[["render",m]])});