@gladesinger/vue3-yandex-smartcaptcha
Version:
Yandex Smart Captcha Vue component
104 lines (103 loc) • 4.65 kB
JavaScript
import { defineComponent as H, ref as I, computed as K, onMounted as L, onBeforeUnmount as O, createBlock as P, openBlock as T, h as V } from "vue";
const J = /* @__PURE__ */ H({
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(w, { expose: f, emit: v }) {
const n = w, s = v, e = I(null), r = `smart-captcha-${Math.random().toString(36).substring(2, 15)}`;
let t;
const i = () => {
var a, o, l, d, u, p, h;
if (window && document && (window != null && window.smartCaptcha)) {
const m = document == null ? void 0 : document.getElementById(r);
m && (e.value = (a = window == null ? void 0 : window.smartCaptcha) == null ? void 0 : a.render(m, {
sitekey: n.siteKey,
hl: n.language,
test: n.test,
webview: n.webview,
invisible: n.invisible,
shieldPosition: n.shieldPosition,
hideShield: n.hideShield,
callback: b
}), (o = window == null ? void 0 : window.smartCaptcha) == null || o.subscribe(e.value, "success", k), (l = window == null ? void 0 : window.smartCaptcha) == null || l.subscribe(e.value, "challenge-visible", C), (d = window == null ? void 0 : window.smartCaptcha) == null || d.subscribe(e.value, "challenge-hidden", g), (u = window == null ? void 0 : window.smartCaptcha) == null || u.subscribe(e.value, "network-error", y), (p = window == null ? void 0 : window.smartCaptcha) == null || p.subscribe(e.value, "javascript-error", x), (h = window == null ? void 0 : window.smartCaptcha) == null || h.subscribe(e.value, "token-expired", E));
}
}, c = () => {
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();
}, b = (a) => {
s("callback", a);
}, C = () => {
s("onChallengeVisible");
}, g = () => {
s("onChallengeHidden");
}, y = () => {
s("onNetworkError", { message: "Network error occured" });
}, x = (a) => {
s("onJavaScriptError", a);
}, k = (a) => {
s("onSuccess", a);
}, E = () => {
s("onTokenExpired");
}, B = () => {
window != null && window.smartCaptcha && e.value !== null && window.smartCaptcha.execute(e.value);
}, S = () => {
if (window != null && window.smartCaptcha && e.value !== null)
return window.smartCaptcha.getResponse(e.value);
}, _ = () => {
window != null && window.smartCaptcha && e.value !== null && window.smartCaptcha.reset(e.value);
}, j = () => {
c();
}, N = (a, o) => {
window != null && window.smartCaptcha && e.value !== null && window.smartCaptcha.subscribe(e.value, a, o);
}, U = K(() => n.invisible ? { height: 0, width: 0 } : { width: "100px" });
L(() => {
if (!n.loadWidget) return;
const a = n.useOldUrl ? "smartcaptcha.yandexcloud.net" : "smartcaptcha.cloud.yandex.ru";
t = Array.from(document.getElementsByTagName("script")).find(
(o) => o.src.includes(`${a}/captcha.js`)
), t ? t.loaded ? i() : t.addEventListener("load", function o() {
t && (t.removeEventListener("load", o), t.loaded = !0, i());
}) : (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, i();
});
}), O(() => {
c();
}), f({
widgetId: e,
containerId: r,
subscribe: N,
execute: B,
getResponse: S,
reset: _,
destroy: j
});
const Y = () => V("div", {
style: U.value,
id: r,
class: "smart-captcha",
"data-widget-id": e.value,
"data-sitekey": n.siteKey
});
return (a, o) => (T(), P(Y));
}
});
export {
J as YandexSmartCaptcha
};