UNPKG

vue3-smart-captcha

Version:

Yandex Smart Captcha for Vue3 projects

159 lines (158 loc) 4.8 kB
var k = Object.defineProperty; var E = (a, e, s) => e in a ? k(a, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : a[e] = s; var h = (a, e, s) => E(a, typeof e != "symbol" ? e + "" : e, s); import { ref as u, onMounted as S, onUnmounted as R, defineComponent as x, watch as T, openBlock as I, createElementBlock as j } from "vue"; class A { constructor() { h(this, "SCRIPT_RENDER_ONLOAD_SRC", "https://smartcaptcha.yandexcloud.net/captcha.js?render=onload"); } execute(e) { var s; (s = window.smartCaptcha) == null || s.execute(e); } reset(e) { var s; (s = window.smartCaptcha) == null || s.reset(e); } destroy(e) { var s; (s = window.smartCaptcha) == null || s.destroy(e); } getResponse(e) { var s; return (s = window.smartCaptcha) == null ? void 0 : s.getResponse(e); } subscribe(e, s, o) { var i; (i = window.smartCaptcha) == null || i.subscribe(e, s, o); } } const B = (a, e, s = !0, o = 2e3) => { const i = new A(), c = u(), r = u(), n = u(), d = { success: [], "javascript-error": [], "network-error": [], "token-expired": [], "challenge-hidden": [], "challenge-visible": [] }, v = () => { typeof a == "string" ? n.value = document.querySelector(a) : a instanceof HTMLElement ? n.value = a : a.value && (n.value = a.value); }, m = () => { const t = document.createElement("script"); t.src = i.SCRIPT_RENDER_ONLOAD_SRC, t.defer = !0, t.type = "text/javascript", t.crossOrigin = "anonymous", t.dataset.captcha = "true", document.head.appendChild(t); }, b = () => { if (typeof e == "string") return { sitekey: e }; const t = (e == null ? void 0 : e.sitekey) || void 0; if (!t) throw new Error("Captcha key is not defined. Please provide value or specify `VITE_SMARTCAPTCHA_KEY` environment variable"); return { ...e, sitekey: t }; }, w = (t) => { r.value = t; }, _ = (t) => { d[t].length > 0 && d[t].forEach((l) => { i.subscribe(c.value, t, l); }); }, f = () => { c.value !== void 0 && (i.subscribe(c.value, "success", w), Array.from(Object.keys(d)).forEach((t) => { _(t); })); }, y = () => { n.value && window.smartCaptcha !== void 0 && (c.value = window.smartCaptcha.render(n.value, b()), f()); }; S(() => { v(), window.smartCaptcha === void 0 && s && m(); const t = 10, l = setInterval(() => { let g = 0; if (++g === t) { console.warn( `The captcha could not be initialized in ${o}ms. Make sure the widget script has been loaded` ), clearInterval(l); return; } window.smartCaptcha !== void 0 && (clearInterval(l), y()); }, o / t); }), R(() => { var t; s && ((t = document.querySelector("script[data-captcha]")) == null || t.remove()); }); const C = () => { i.execute(c.value); }, p = () => { c.value = void 0, r.value = void 0; }; return { widgetId: c, token: r, execute: C, reset: () => { i.reset(c.value), p(); }, destroy: () => { i.destroy(c.value), p(); }, getResponse: () => i.getResponse(c.value), subscribeTo: (t, l) => { d[t].push(l); } }; }, O = /* @__PURE__ */ x({ __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 } }, emits: ["challengeHidden", "challengeVisible", "javascriptError", "networkError", "tokenExpired", "success", "initialized"], setup(a, { emit: e }) { const s = u(), o = a, { subscribeTo: i, widgetId: c } = B(s, { sitekey: o.sitekey, callback: o.callback, hl: o.hl, test: o.test, webview: o.webview, invisible: o.invisible, shieldPosition: o.shieldPosition, hideShield: o.hideShield }, o.loadWidget, o.timeout), r = e; return T(c, (n) => { n !== void 0 && r("initialized", n); }), i("challenge-hidden", () => { r("challengeHidden"); }), i("challenge-visible", () => { r("challengeVisible"); }), i("javascript-error", (n) => { r("javascriptError", n); }), i("network-error", () => { r("networkError"); }), i("token-expired", () => { r("tokenExpired"); }), i("success", (n) => { r("success", n); }), (n, d) => (I(), j("div", { ref_key: "container", ref: s }, null, 512)); } }), W = { install: (a) => { a.component("SmartCaptcha", O); } }; export { O as SmartCaptcha, W as SmartCaptchaPlugin, A as SmartCaptchaUtils, O as default, B as useSmartCaptcha };