2020-04-29 02:15:18 +02:00
|
|
|
<template>
|
2023-04-08 02:01:42 +02:00
|
|
|
<div>
|
|
|
|
<span v-if="!available">{{ i18n.ts.waiting }}<MkEllipsis /></span>
|
|
|
|
<div ref="captchaEl"></div>
|
|
|
|
</div>
|
2020-04-29 02:15:18 +02:00
|
|
|
</template>
|
|
|
|
|
2022-01-10 16:05:18 +01:00
|
|
|
<script lang="ts" setup>
|
2023-04-08 02:01:42 +02:00
|
|
|
import { ref, computed, onMounted, onBeforeUnmount, watch } from "vue";
|
|
|
|
import { defaultStore } from "@/store";
|
|
|
|
import { i18n } from "@/i18n";
|
2020-04-29 02:15:18 +02:00
|
|
|
|
|
|
|
type Captcha = {
|
2023-04-08 02:01:42 +02:00
|
|
|
render(
|
|
|
|
container: string | Node,
|
|
|
|
options: {
|
|
|
|
readonly [_ in
|
|
|
|
| "sitekey"
|
|
|
|
| "theme"
|
|
|
|
| "type"
|
|
|
|
| "size"
|
|
|
|
| "tabindex"
|
|
|
|
| "callback"
|
|
|
|
| "expired"
|
|
|
|
| "expired-callback"
|
|
|
|
| "error-callback"
|
|
|
|
| "endpoint"]?: unknown;
|
|
|
|
}
|
|
|
|
): string;
|
2020-04-29 02:15:18 +02:00
|
|
|
remove(id: string): void;
|
|
|
|
execute(id: string): void;
|
2022-01-10 16:05:18 +01:00
|
|
|
reset(id?: string): void;
|
2020-04-29 02:15:18 +02:00
|
|
|
getResponse(id: string): string;
|
|
|
|
};
|
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
type CaptchaProvider = "hcaptcha" | "recaptcha";
|
2020-04-29 02:15:18 +02:00
|
|
|
|
|
|
|
type CaptchaContainer = {
|
|
|
|
readonly [_ in CaptchaProvider]?: Captcha;
|
|
|
|
};
|
|
|
|
|
|
|
|
declare global {
|
2023-04-08 02:01:42 +02:00
|
|
|
interface Window extends CaptchaContainer {}
|
2020-04-29 02:15:18 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:05:18 +01:00
|
|
|
const props = defineProps<{
|
|
|
|
provider: CaptchaProvider;
|
|
|
|
sitekey: string;
|
|
|
|
modelValue?: string | null;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
2023-04-08 02:01:42 +02:00
|
|
|
(ev: "update:modelValue", v: string | null): void;
|
2022-01-10 16:05:18 +01:00
|
|
|
}>();
|
|
|
|
|
|
|
|
const available = ref(false);
|
|
|
|
|
|
|
|
const captchaEl = ref<HTMLDivElement | undefined>();
|
|
|
|
|
|
|
|
const variable = computed(() => {
|
|
|
|
switch (props.provider) {
|
2023-04-08 02:01:42 +02:00
|
|
|
case "hcaptcha":
|
|
|
|
return "hcaptcha";
|
|
|
|
case "recaptcha":
|
|
|
|
return "grecaptcha";
|
2022-01-10 16:05:18 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-28 17:36:06 +02:00
|
|
|
const loaded = !!window[variable.value];
|
2022-01-10 16:05:18 +01:00
|
|
|
|
|
|
|
const src = computed(() => {
|
2022-01-21 09:43:14 +01:00
|
|
|
switch (props.provider) {
|
2023-04-08 02:01:42 +02:00
|
|
|
case "hcaptcha":
|
|
|
|
return "https://js.hcaptcha.com/1/api.js?render=explicit&recaptchacompat=off";
|
|
|
|
case "recaptcha":
|
|
|
|
return "https://www.recaptcha.net/recaptcha/api.js?render=explicit";
|
2022-01-21 09:43:14 +01:00
|
|
|
}
|
2020-04-29 02:15:18 +02:00
|
|
|
});
|
2022-01-10 16:05:18 +01:00
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
const captcha = computed<Captcha>(
|
|
|
|
() => window[variable.value] || ({} as unknown as Captcha)
|
|
|
|
);
|
2022-01-10 16:05:18 +01:00
|
|
|
|
2022-06-28 17:36:06 +02:00
|
|
|
if (loaded) {
|
2022-01-10 16:05:18 +01:00
|
|
|
available.value = true;
|
|
|
|
} else {
|
2023-04-08 02:01:42 +02:00
|
|
|
(
|
|
|
|
document.getElementById(props.provider) ||
|
|
|
|
document.head.appendChild(
|
|
|
|
Object.assign(document.createElement("script"), {
|
|
|
|
async: true,
|
|
|
|
id: props.provider,
|
|
|
|
src: src.value,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).addEventListener("load", () => (available.value = true));
|
2022-01-10 16:05:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
2022-06-28 17:36:06 +02:00
|
|
|
if (captcha.value.reset) captcha.value.reset();
|
2022-01-10 16:05:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function requestRender() {
|
|
|
|
if (captcha.value.render && captchaEl.value instanceof Element) {
|
|
|
|
captcha.value.render(captchaEl.value, {
|
|
|
|
sitekey: props.sitekey,
|
2023-04-08 02:01:42 +02:00
|
|
|
theme: defaultStore.state.darkMode ? "dark" : "light",
|
2022-01-10 16:05:18 +01:00
|
|
|
callback: callback,
|
2023-04-08 02:01:42 +02:00
|
|
|
"expired-callback": callback,
|
|
|
|
"error-callback": callback,
|
2022-01-10 16:05:18 +01:00
|
|
|
});
|
|
|
|
} else {
|
2022-01-16 02:14:14 +01:00
|
|
|
window.setTimeout(requestRender, 1);
|
2022-01-10 16:05:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function callback(response?: string) {
|
2023-04-08 02:01:42 +02:00
|
|
|
emit("update:modelValue", typeof response === "string" ? response : null);
|
2022-01-10 16:05:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
if (available.value) {
|
|
|
|
requestRender();
|
|
|
|
} else {
|
|
|
|
watch(available, requestRender);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
reset,
|
|
|
|
});
|
2020-04-29 02:15:18 +02:00
|
|
|
</script>
|