2020-04-28 19:15:18 -05:00
|
|
|
<template>
|
|
|
|
<div>
|
2020-12-25 19:47:36 -06:00
|
|
|
<span v-if="!available">{{ $ts.waiting }}<MkEllipsis/></span>
|
2020-04-28 19:15:18 -05:00
|
|
|
<div ref="captcha"></div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2020-10-17 06:12:00 -05:00
|
|
|
import { defineComponent } from 'vue';
|
2020-04-28 19:15:18 -05: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;
|
|
|
|
};
|
|
|
|
|
|
|
|
type CaptchaProvider = 'hcaptcha' | 'grecaptcha';
|
|
|
|
|
|
|
|
type CaptchaContainer = {
|
|
|
|
readonly [_ in CaptchaProvider]?: Captcha;
|
|
|
|
};
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface Window extends CaptchaContainer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
export default defineComponent({
|
2020-04-28 19:15:18 -05:00
|
|
|
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',
|
2021-01-29 19:57:11 -06:00
|
|
|
grecaptcha: 'https://www.recaptcha.net/recaptcha',
|
2020-04-28 19:15:18 -05:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
beforeUnmount() {
|
2020-04-28 19:15:18 -05:00
|
|
|
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,
|
2020-12-18 19:55:52 -06:00
|
|
|
theme: this.$store.state.darkMode ? 'dark' : 'light',
|
2020-04-28 19:15:18 -05:00
|
|
|
callback: this.callback,
|
|
|
|
'expired-callback': this.callback,
|
|
|
|
'error-callback': this.callback,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setTimeout(this.requestRender.bind(this), 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
callback(response?: string) {
|
2020-10-17 06:12:00 -05:00
|
|
|
this.$emit('update:value', typeof response == 'string' ? response : null);
|
2020-04-28 19:15:18 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|