summaryrefslogtreecommitdiff
path: root/src/client/components/captcha.vue
blob: 1a894d9350b286f10ec966583a65a013b13b7940 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<template>
<div>
	<span v-if="!available">{{ $t('waiting') }}<mk-ellipsis/></span>
	<div ref="captcha"></div>
</div>
</template>

<script lang="ts">
import Vue from 'vue';

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;
};

type CaptchaProvider = 'hcaptcha' | 'grecaptcha';

type CaptchaContainer = {
	readonly [_ in CaptchaProvider]?: Captcha;
};

declare global {
	interface Window extends CaptchaContainer {
	}
}

export default Vue.extend({
	props: {
		provider: {
			type: String,
			required: true,
		},
		sitekey: {
			type: String,
			required: true,
		},
		value: {
			type: String,
		},
	},

	data() {
		return {
			available: false,
		};
	},

	computed: {
		loaded() {
			return !!window[this.provider as CaptchaProvider];
		},
		src() {
			const endpoint = ({
				hcaptcha: 'https://hcaptcha.com/1',
				grecaptcha: 'https://www.google.com/recaptcha',
			} as Record<PropertyKey, unknown>)[this.provider];

			return `${typeof endpoint == 'string' ? endpoint : 'about:invalid'}/api.js?render=explicit`;
		},
		captcha() {
			return window[this.provider as CaptchaProvider] || {} as unknown as Captcha;
		},
	},

	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);
		}
	},

	beforeDestroy() {
		this.reset();
	},

	methods: {
		reset() {
			this.captcha?.reset();
		},
		requestRender() {
			if (this.captcha.render && this.$refs.captcha instanceof Element) {
				this.captcha.render(this.$refs.captcha, {
					sitekey: this.sitekey,
					theme: this.$store.state.device.darkMode ? 'dark' : 'light',
					callback: this.callback,
					'expired-callback': this.callback,
					'error-callback': this.callback,
				});
			} else {
				setTimeout(this.requestRender.bind(this), 1);
			}
		},
		callback(response?: string) {
			this.$emit('input', typeof response == 'string' ? response : null);
		},
	},
});
</script>