2020-04-29 02:15:18 +02:00
|
|
|
<template>
|
|
|
|
<div>
|
2020-12-26 02:47:36 +01:00
|
|
|
<span v-if="!available">{{ $ts.waiting }}<MkEllipsis/></span>
|
2020-04-29 02:15:18 +02:00
|
|
|
<div ref="captcha"></div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2021-04-26 05:47:54 +02:00
|
|
|
import { defineComponent, PropType } from 'vue';
|
2020-04-29 02:15:18 +02: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;
|
|
|
|
getResponse(id: string): string;
|
|
|
|
};
|
|
|
|
|
2021-04-22 15:29:33 +02:00
|
|
|
type CaptchaProvider = 'hcaptcha' | 'recaptcha';
|
2020-04-29 02:15:18 +02:00
|
|
|
|
|
|
|
type CaptchaContainer = {
|
|
|
|
readonly [_ in CaptchaProvider]?: Captcha;
|
|
|
|
};
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface Window extends CaptchaContainer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
export default defineComponent({
|
2020-04-29 02:15:18 +02:00
|
|
|
props: {
|
|
|
|
provider: {
|
2021-04-26 05:47:54 +02:00
|
|
|
type: String as PropType<CaptchaProvider>,
|
2020-04-29 02:15:18 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
sitekey: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
},
|
2021-10-01 12:32:16 +02:00
|
|
|
modelValue: {
|
2020-04-29 02:15:18 +02:00
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
available: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
2021-04-26 05:47:54 +02:00
|
|
|
variable(): string {
|
|
|
|
switch (this.provider) {
|
|
|
|
case 'hcaptcha': return 'hcaptcha';
|
|
|
|
case 'recaptcha': return 'grecaptcha';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
loaded(): boolean {
|
|
|
|
return !!window[this.variable];
|
2020-04-29 02:15:18 +02:00
|
|
|
},
|
2021-04-26 05:47:54 +02:00
|
|
|
src(): string {
|
2020-04-29 02:15:18 +02:00
|
|
|
const endpoint = ({
|
|
|
|
hcaptcha: 'https://hcaptcha.com/1',
|
2021-04-22 15:29:33 +02:00
|
|
|
recaptcha: 'https://www.recaptcha.net/recaptcha',
|
2021-04-26 05:47:54 +02:00
|
|
|
} as Record<CaptchaProvider, string>)[this.provider];
|
2020-04-29 02:15:18 +02:00
|
|
|
|
2021-04-26 05:47:54 +02:00
|
|
|
return `${typeof endpoint === 'string' ? endpoint : 'about:invalid'}/api.js?render=explicit`;
|
2020-04-29 02:15:18 +02:00
|
|
|
},
|
2021-04-26 05:47:54 +02:00
|
|
|
captcha(): Captcha {
|
|
|
|
return window[this.variable] || {} as unknown as Captcha;
|
2020-04-29 02:15:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
|
|
|
if (this.loaded) {
|
|
|
|
this.available = true;
|
|
|
|
} else {
|
|
|
|
(document.getElementById(this.provider) || document.head.appendChild(Object.assign(document.createElement('script'), {
|
|
|
|
async: true,
|
|
|
|
id: this.provider,
|
|
|
|
src: this.src,
|
|
|
|
})))
|
|
|
|
.addEventListener('load', () => this.available = true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted() {
|
|
|
|
if (this.available) {
|
|
|
|
this.requestRender();
|
|
|
|
} else {
|
|
|
|
this.$watch('available', this.requestRender);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-10-17 13:12:00 +02:00
|
|
|
beforeUnmount() {
|
2020-04-29 02:15:18 +02:00
|
|
|
this.reset();
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
reset() {
|
2021-04-26 05:47:54 +02:00
|
|
|
if (this.captcha?.reset) this.captcha.reset();
|
2020-04-29 02:15:18 +02:00
|
|
|
},
|
|
|
|
requestRender() {
|
|
|
|
if (this.captcha.render && this.$refs.captcha instanceof Element) {
|
|
|
|
this.captcha.render(this.$refs.captcha, {
|
|
|
|
sitekey: this.sitekey,
|
2020-12-19 02:55:52 +01:00
|
|
|
theme: this.$store.state.darkMode ? 'dark' : 'light',
|
2020-04-29 02:15:18 +02:00
|
|
|
callback: this.callback,
|
|
|
|
'expired-callback': this.callback,
|
|
|
|
'error-callback': this.callback,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setTimeout(this.requestRender.bind(this), 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
callback(response?: string) {
|
2021-10-01 12:32:16 +02:00
|
|
|
this.$emit('update:modelValue', typeof response == 'string' ? response : null);
|
2020-04-29 02:15:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|