misskey/packages/frontend/src/components/MkCaptcha.vue

119 lines
2.9 KiB
Vue
Raw Normal View History

2020-04-29 09:15:18 +09:00
<template>
<div>
<span v-if="!available">{{ i18n.ts.waiting }}<MkEllipsis/></span>
<div ref="captchaEl"></div>
2020-04-29 09:15:18 +09:00
</div>
</template>
<script lang="ts" setup>
import { ref, shallowRef, computed, onMounted, onBeforeUnmount, watch } from 'vue';
import { defaultStore } from '@/store';
import { i18n } from '@/i18n';
2020-04-29 09:15:18 +09: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;
reset(id?: string): void;
2020-04-29 09:15:18 +09:00
getResponse(id: string): string;
};
type CaptchaProvider = 'hcaptcha' | 'recaptcha' | 'turnstile';
2020-04-29 09:15:18 +09:00
type CaptchaContainer = {
readonly [_ in CaptchaProvider]?: Captcha;
};
declare global {
interface Window extends CaptchaContainer { }
2020-04-29 09:15:18 +09:00
}
const props = defineProps<{
provider: CaptchaProvider;
sitekey: string;
modelValue?: string | null;
}>();
const emit = defineEmits<{
2022-01-26 03:26:34 +09:00
(ev: 'update:modelValue', v: string | null): void;
}>();
const available = ref(false);
const captchaEl = shallowRef<HTMLDivElement | undefined>();
const variable = computed(() => {
switch (props.provider) {
case 'hcaptcha': return 'hcaptcha';
case 'recaptcha': return 'grecaptcha';
case 'turnstile': return 'turnstile';
}
});
const loaded = !!window[variable.value];
const src = computed(() => {
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';
case 'turnstile': return 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit';
}
2020-04-29 09:15:18 +09:00
});
const scriptId = computed(() => `script-${props.provider}`);
2022-12-20 01:31:21 +09:00
const captcha = computed<Captcha>(() => window[variable.value] || {} as unknown as Captcha);
if (loaded) {
available.value = true;
} else {
2023-02-22 15:28:17 +09:00
(document.getElementById(scriptId.value) ?? document.head.appendChild(Object.assign(document.createElement('script'), {
async: true,
2022-12-20 01:31:21 +09:00
id: scriptId.value,
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-16 10:14:14 +09:00
window.setTimeout(requestRender, 1);
}
}
function callback(response?: string) {
2022-03-01 21:36:20 +09:00
emit('update:modelValue', typeof response === 'string' ? response : null);
}
onMounted(() => {
if (available.value) {
requestRender();
} else {
watch(available, requestRender);
}
});
onBeforeUnmount(() => {
reset();
});
defineExpose({
reset,
});
2020-04-29 09:15:18 +09:00
</script>