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