UNPKG

@paparootru/vue3-smart-captcha

Version:
75 lines (74 loc) 2.75 kB
import { defineComponent as w, ref as C, onMounted as f, onUnmounted as v, computed as r, openBlock as k, createElementBlock as S, normalizeClass as g } from "vue"; const c = "https://smartcaptcha.yandexcloud.net/captcha.js", l = 10, y = /* @__PURE__ */ w({ __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 }, onChallengeVisible: {}, onChallengeHidden: {}, onNetworkError: {}, onSuccess: {}, onTokenExpired: {} }, emits: ["widgetInitialized"], setup(i, { emit: d }) { const o = C(), p = d, e = i; f(() => { a.value && m(), u(); }), v(() => { var t; a.value && ((t = document.querySelector(`script[src="${c}?render=onload"]`)) == null || t.remove()); }); const a = r(() => e.loadWidget), h = r(() => e.invisible === !0), m = () => { const t = document.createElement("script"); t.setAttribute("src", `${c}?render=onload`), t.setAttribute("defer", "true"), document.head.appendChild(t); }, u = () => { let t = 0; const n = setInterval(() => { if (++t === l) { console.warn( `Captcha cannot be initialized for ${e.timeout}ms. Make sure widget script is loaded` ), clearInterval(n); return; } if (window.smartCaptcha !== void 0) { clearInterval(n); const s = window.smartCaptcha.render(o.value, { sitekey: e.sitekey, callback: e.callback, hl: e.hl, test: e.test, webview: e.webview, invisible: e.invisible, shieldPosition: e.shieldPosition, hideShield: e.hideShield }); p("widgetInitialized", s), b(s); } }, e.timeout / l); }, b = (t) => { e.onChallengeHidden && window.smartCaptcha.subscribe(t, "challenge-hidden", e.onChallengeHidden), e.onChallengeVisible && window.smartCaptcha.subscribe(t, "challenge-visible", e.onChallengeVisible), e.onNetworkError && window.smartCaptcha.subscribe(t, "network-error", e.onNetworkError), e.onSuccess && window.smartCaptcha.subscribe(t, "success", e.onSuccess), e.onTokenExpired && window.smartCaptcha.subscribe(t, "token-expired", e.onTokenExpired); }; return (t, n) => (k(), S("div", { ref_key: "container", ref: o, class: g({ "smart-captcha": !h.value }) }, null, 2)); } }), E = { install: (i) => { i.component("SmartCaptcha", y); } }; export { y as SmartCaptcha, E as SmartCaptchaPlugin };