forked from bbonch/vue3-recaptcha2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (79 loc) · 2.11 KB
/
index.js
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
import { h } from 'vue'
export default {
name: "vueRecaptcha",
data() {
return {
recaptcha: null
}
},
props: {
siteKey: {
type: String,
required: true
},
size: {
type: String,
required: false,
default: "normal"
},
theme: {
type: String,
required: false,
default: "light"
}
},
emits: {
verify: (response) => {
if (response)
return true;
else
return false;
},
expire: null,
fail: null
},
methods: {
renderRecaptcha() {
this.recaptcha = grecaptcha.render(this.$refs.recaptcha, {
'sitekey': this.siteKey,
'theme': this.theme,
'size': this.size,
'tabindex': this.tabindex,
'callback': (response) => this.$emit("verify", response),
'expired-callback': () => this.$emit("expire"),
'error-callback': () => this.$emit("fail")
});
},
execute() {
grecaptcha.execute(this.recaptcha);
},
reset() {
grecaptcha.reset(this.recaptcha);
}
},
mounted() {
if (window.grecaptcha == null) {
new Promise((resolve) => {
window.recaptchaReady = function () {
resolve();
};
const doc = window.document;
const scriptId = "recaptcha-script";
const scriptTag = doc.createElement("script");
scriptTag.id = scriptId;
scriptTag.setAttribute("src", "https://www.google.com/recaptcha/api.js?onload=recaptchaReady&render=explicit");
doc.head.appendChild(scriptTag);
}).then(() => {
this.renderRecaptcha();
});
}
else {
this.renderRecaptcha();
}
},
render() {
return h('div', {
ref: 'recaptcha'
}, {})
}
}