2020-04-28 17:15:18 -07:00
|
|
|
<template>
|
|
|
|
<div>
|
2022-01-27 18:39:49 -08:00
|
|
|
<span v-if="!available">{{ i18n.ts.waiting }}<MkEllipsis/></span>
|
2022-01-10 07:05:18 -08:00
|
|
|
<div ref="captchaEl"></div>
|
2020-04-28 17:15:18 -07:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-10 07:05:18 -08:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { ref, computed, onMounted, onBeforeUnmount, watch } from 'vue';
|
|
|
|
import { defaultStore } from '@/store';
|
|
|
|
import { i18n } from '@/i18n';
|
2020-04-28 17:15:18 -07:00
|
|
|
|
|
|
|
type Captcha = {
|
|
|
|
render(container: string | Node, options: {
|
|
|
|
readonly [_ in 'sitekey' | 'theme' | 'type' | 'size' | 'tabindex' | 'callback' | 'expired' | 'expired-callback' | 'error-callback' | 'endpoint']?: unknown;
|
|
|
|
}): string;
|
|
|
|
remove(id: string): void;
|
|
|
|
execute(id: string): void;
|
2022-01-10 07:05:18 -08:00
|
|
|
reset(id?: string): void;
|
2020-04-28 17:15:18 -07:00
|
|
|
getResponse(id: string): string;
|
|
|
|
};
|
|
|
|
|
2021-04-22 06:29:33 -07:00
|
|
|
type CaptchaProvider = 'hcaptcha' | 'recaptcha';
|
2020-04-28 17:15:18 -07:00
|
|
|
|
|
|
|
type CaptchaContainer = {
|
|
|
|
readonly [_ in CaptchaProvider]?: Captcha;
|
|
|
|
};
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface Window extends CaptchaContainer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 07:05:18 -08:00
|
|
|
const props = defineProps<{
|
|
|
|
provider: CaptchaProvider;
|
|
|
|
sitekey: string;
|
|
|
|
modelValue?: string | null;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
2022-01-25 10:26:34 -08:00
|
|
|
(ev: 'update:modelValue', v: string | null): void;
|
2022-01-10 07:05:18 -08:00
|
|
|
}>();
|
|
|
|
|
|
|
|
const available = ref(false);
|
|
|
|
|
|
|
|
const captchaEl = ref<HTMLDivElement | undefined>();
|
|
|
|
|
|
|
|
const variable = computed(() => {
|
|
|
|
switch (props.provider) {
|
|
|
|
case 'hcaptcha': return 'hcaptcha';
|
|
|
|
case 'recaptcha': return 'grecaptcha';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const loaded = computed(() => !!window[variable.value]);
|
|
|
|
|
|
|
|
const src = computed(() => {
|
2022-01-21 00:43:14 -08:00
|
|
|
switch (props.provider) {
|
|
|
|
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';
|
|
|
|
}
|
2020-04-28 17:15:18 -07:00
|
|
|
});
|
2022-01-10 07:05:18 -08:00
|
|
|
|
|
|
|
const captcha = computed<Captcha>(() => window[variable.value] || {} as unknown as Captcha);
|
|
|
|
|
|
|
|
if (loaded.value) {
|
|
|
|
available.value = true;
|
|
|
|
} else {
|
|
|
|
(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
if (captcha.value?.reset) captcha.value.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestRender() {
|
|
|
|
if (captcha.value.render && captchaEl.value instanceof Element) {
|
|
|
|
captcha.value.render(captchaEl.value, {
|
|
|
|
sitekey: props.sitekey,
|
|
|
|
theme: defaultStore.state.darkMode ? 'dark' : 'light',
|
|
|
|
callback: callback,
|
|
|
|
'expired-callback': callback,
|
|
|
|
'error-callback': callback,
|
|
|
|
});
|
|
|
|
} else {
|
2022-01-15 17:14:14 -08:00
|
|
|
window.setTimeout(requestRender, 1);
|
2022-01-10 07:05:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function callback(response?: string) {
|
2022-03-01 04:36:20 -08:00
|
|
|
emit('update:modelValue', typeof response === 'string' ? response : null);
|
2022-01-10 07:05:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
if (available.value) {
|
|
|
|
requestRender();
|
|
|
|
} else {
|
|
|
|
watch(available, requestRender);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
reset,
|
|
|
|
});
|
|
|
|
|
2020-04-28 17:15:18 -07:00
|
|
|
</script>
|