2023-07-27 14:31:52 +09:00
|
|
|
<!--
|
2024-02-14 00:59:27 +09:00
|
|
|
SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-04-29 09:15:18 +09:00
|
|
|
<template>
|
|
|
|
<div>
|
2024-01-30 19:53:53 +09:00
|
|
|
<span v-if="!available">Loading<MkEllipsis/></span>
|
2024-01-06 20:14:33 +09:00
|
|
|
<div v-if="props.provider == 'mcaptcha'">
|
|
|
|
<div id="mcaptcha__widget-container" class="m-captcha-style"></div>
|
|
|
|
<div ref="captchaEl"></div>
|
|
|
|
</div>
|
|
|
|
<div v-else ref="captchaEl"></div>
|
2020-04-29 09:15:18 +09:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-11 00:05:18 +09:00
|
|
|
<script lang="ts" setup>
|
2024-01-06 20:14:33 +09:00
|
|
|
import { ref, shallowRef, computed, onMounted, onBeforeUnmount, watch, onUnmounted } from 'vue';
|
2023-09-19 16:37:43 +09:00
|
|
|
import { defaultStore } from '@/store.js';
|
2020-04-29 09:15:18 +09:00
|
|
|
|
2023-03-11 14:32:13 +09:00
|
|
|
// APIs provided by Captcha services
|
|
|
|
export type Captcha = {
|
2020-04-29 09:15:18 +09:00
|
|
|
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-11 00:05:18 +09:00
|
|
|
reset(id?: string): void;
|
2020-04-29 09:15:18 +09:00
|
|
|
getResponse(id: string): string;
|
|
|
|
};
|
|
|
|
|
2024-01-06 20:14:33 +09:00
|
|
|
export type CaptchaProvider = 'hcaptcha' | 'recaptcha' | 'turnstile' | 'mcaptcha';
|
2020-04-29 09:15:18 +09:00
|
|
|
|
|
|
|
type CaptchaContainer = {
|
|
|
|
readonly [_ in CaptchaProvider]?: Captcha;
|
|
|
|
};
|
|
|
|
|
|
|
|
declare global {
|
2022-06-10 14:36:55 +09:00
|
|
|
interface Window extends CaptchaContainer { }
|
2020-04-29 09:15:18 +09:00
|
|
|
}
|
|
|
|
|
2022-01-11 00:05:18 +09:00
|
|
|
const props = defineProps<{
|
|
|
|
provider: CaptchaProvider;
|
2023-03-11 14:32:13 +09:00
|
|
|
sitekey: string | null; // null will show error on request
|
2024-01-06 20:14:33 +09:00
|
|
|
instanceUrl?: string | null;
|
2022-01-11 00:05:18 +09:00
|
|
|
modelValue?: string | null;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
2022-01-26 03:26:34 +09:00
|
|
|
(ev: 'update:modelValue', v: string | null): void;
|
2022-01-11 00:05:18 +09:00
|
|
|
}>();
|
|
|
|
|
|
|
|
const available = ref(false);
|
|
|
|
|
2023-01-03 10:12:37 +09:00
|
|
|
const captchaEl = shallowRef<HTMLDivElement | undefined>();
|
2022-01-11 00:05:18 +09:00
|
|
|
|
|
|
|
const variable = computed(() => {
|
|
|
|
switch (props.provider) {
|
|
|
|
case 'hcaptcha': return 'hcaptcha';
|
|
|
|
case 'recaptcha': return 'grecaptcha';
|
2022-10-13 09:19:57 +09:00
|
|
|
case 'turnstile': return 'turnstile';
|
2024-01-06 20:14:33 +09:00
|
|
|
case 'mcaptcha': return 'mcaptcha';
|
2022-01-11 00:05:18 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-29 00:36:06 +09:00
|
|
|
const loaded = !!window[variable.value];
|
2022-01-11 00:05:18 +09:00
|
|
|
|
|
|
|
const src = computed(() => {
|
2022-01-21 17:43:14 +09: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';
|
2022-10-13 09:19:57 +09:00
|
|
|
case 'turnstile': return 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit';
|
2024-01-06 20:14:33 +09:00
|
|
|
case 'mcaptcha': return null;
|
2022-01-21 17:43:14 +09:00
|
|
|
}
|
2020-04-29 09:15:18 +09:00
|
|
|
});
|
2022-01-11 00:05:18 +09:00
|
|
|
|
2023-01-03 10:12:37 +09:00
|
|
|
const scriptId = computed(() => `script-${props.provider}`);
|
2022-12-20 01:31:21 +09:00
|
|
|
|
2022-01-11 00:05:18 +09:00
|
|
|
const captcha = computed<Captcha>(() => window[variable.value] || {} as unknown as Captcha);
|
|
|
|
|
2024-01-06 20:14:33 +09:00
|
|
|
if (loaded || props.provider === 'mcaptcha') {
|
2022-01-11 00:05:18 +09:00
|
|
|
available.value = true;
|
2024-01-06 20:14:33 +09:00
|
|
|
} else if (src.value !== null) {
|
2023-02-22 15:28:17 +09:00
|
|
|
(document.getElementById(scriptId.value) ?? document.head.appendChild(Object.assign(document.createElement('script'), {
|
2022-01-11 00:05:18 +09:00
|
|
|
async: true,
|
2022-12-20 01:31:21 +09:00
|
|
|
id: scriptId.value,
|
2022-01-11 00:05:18 +09:00
|
|
|
src: src.value,
|
|
|
|
})))
|
|
|
|
.addEventListener('load', () => available.value = true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
2022-06-29 00:36:06 +09:00
|
|
|
if (captcha.value.reset) captcha.value.reset();
|
2022-01-11 00:05:18 +09:00
|
|
|
}
|
|
|
|
|
2024-01-06 20:14:33 +09:00
|
|
|
async function requestRender() {
|
2022-01-11 00:05:18 +09:00
|
|
|
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,
|
|
|
|
});
|
2024-01-06 20:14:33 +09:00
|
|
|
} else if (props.provider === 'mcaptcha' && props.instanceUrl && props.sitekey) {
|
|
|
|
const { default: Widget } = await import('@mcaptcha/vanilla-glue');
|
|
|
|
// @ts-expect-error avoid typecheck error
|
|
|
|
new Widget({
|
|
|
|
siteKey: {
|
|
|
|
instanceUrl: new URL(props.instanceUrl),
|
|
|
|
key: props.sitekey,
|
|
|
|
},
|
|
|
|
});
|
2022-01-11 00:05:18 +09:00
|
|
|
} else {
|
2022-01-16 10:14:14 +09:00
|
|
|
window.setTimeout(requestRender, 1);
|
2022-01-11 00:05:18 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function callback(response?: string) {
|
2022-03-01 21:36:20 +09:00
|
|
|
emit('update:modelValue', typeof response === 'string' ? response : null);
|
2022-01-11 00:05:18 +09:00
|
|
|
}
|
|
|
|
|
2024-01-06 20:14:33 +09:00
|
|
|
function onReceivedMessage(message: MessageEvent) {
|
|
|
|
if (message.data.token) {
|
|
|
|
if (props.instanceUrl && new URL(message.origin).host === new URL(props.instanceUrl).host) {
|
2024-02-14 13:56:13 +09:00
|
|
|
callback(message.data.token);
|
2024-01-06 20:14:33 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:05:18 +09:00
|
|
|
onMounted(() => {
|
|
|
|
if (available.value) {
|
2024-01-06 20:14:33 +09:00
|
|
|
window.addEventListener('message', onReceivedMessage);
|
2022-01-11 00:05:18 +09:00
|
|
|
requestRender();
|
|
|
|
} else {
|
|
|
|
watch(available, requestRender);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-06 20:14:33 +09:00
|
|
|
onUnmounted(() => {
|
|
|
|
window.removeEventListener('message', onReceivedMessage);
|
|
|
|
});
|
|
|
|
|
2022-01-11 00:05:18 +09:00
|
|
|
onBeforeUnmount(() => {
|
|
|
|
reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
reset,
|
|
|
|
});
|
|
|
|
|
2020-04-29 09:15:18 +09:00
|
|
|
</script>
|