vue3-smart-captcha
Version:
Yandex Smart Captcha for Vue3 projects
161 lines (160 loc) • 4.85 kB
JavaScript
var R = Object.defineProperty;
var x = (a, e, s) => e in a ? R(a, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : a[e] = s;
var m = (a, e, s) => x(a, typeof e != "symbol" ? e + "" : e, s);
import { ref as u, onMounted as T, onUnmounted as b, defineComponent as I, watch as j, openBlock as A, createElementBlock as B } from "vue";
class O {
constructor() {
m(this, "SCRIPT_RENDER_ONLOAD_SRC", "https://smartcaptcha.cloud.yandex.ru/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 D = (a, e, s = !0, o = 2e3) => {
const i = new O(), c = u(), d = u(), n = u(), r = {
success: [],
"javascript-error": [],
"network-error": [],
"token-expired": [],
"challenge-hidden": [],
"challenge-visible": []
}, p = () => {
typeof a == "string" ? n.value = document.querySelector(a) : a instanceof HTMLElement ? n.value = a : a.value && (n.value = a.value);
}, w = () => {
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);
}, _ = () => {
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
};
}, f = (t) => {
d.value = t;
}, y = (t) => {
r[t].length > 0 && r[t].forEach((l) => {
i.subscribe(c.value, t, l);
});
}, C = () => {
c.value !== void 0 && (i.subscribe(c.value, "success", f), Array.from(Object.keys(r)).forEach((t) => {
y(t);
}));
}, g = () => {
n.value && window.smartCaptcha !== void 0 && (c.value = window.smartCaptcha.render(n.value, _()), C());
};
T(() => {
p(), window.smartCaptcha === void 0 && s && w();
const t = 10, l = setInterval(() => {
let S = 0;
if (++S === 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), g());
}, o / t);
}), b(() => {
var t;
s && ((t = document.querySelector("script[data-captcha]")) == null || t.remove()), v();
});
const k = () => {
i.execute(c.value);
}, h = () => {
c.value = void 0, d.value = void 0;
}, E = () => {
i.reset(c.value), h();
}, v = () => {
i.destroy(c.value), h();
};
return {
widgetId: c,
token: d,
execute: k,
reset: E,
destroy: v,
getResponse: () => i.getResponse(c.value),
subscribeTo: (t, l) => {
r[t].push(l);
}
};
}, H = /* @__PURE__ */ I({
__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, destroy: d } = D(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), n = e;
return j(c, (r) => {
r !== void 0 && n("initialized", r);
}), i("challenge-hidden", () => {
n("challengeHidden");
}), i("challenge-visible", () => {
n("challengeVisible");
}), i("javascript-error", (r) => {
n("javascriptError", r);
}), i("network-error", () => {
n("networkError");
}), i("token-expired", () => {
n("tokenExpired");
}), i("success", (r) => {
n("success", r);
}), b(() => {
d();
}), (r, p) => (A(), B("div", {
ref_key: "container",
ref: s
}, null, 512));
}
}), W = {
install: (a) => {
a.component("SmartCaptcha", H);
}
};
export {
H as SmartCaptcha,
W as SmartCaptchaPlugin,
O as SmartCaptchaUtils,
H as default,
D as useSmartCaptcha
};