-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathplugin.js
163 lines (129 loc) · 4.19 KB
/
plugin.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { EventEmitter } from 'events'
import Vue from 'vue'
const API_URL = 'https://www.google.com/recaptcha/api.js'
class ReCaptcha {
constructor ({ hideBadge, language, siteKey, version, size }) {
if (!siteKey) {
throw new Error('ReCaptcha error: No key provided')
}
if (!version) {
throw new Error('ReCaptcha error: No version provided')
}
this._elements = {}
this._eventBus = null
this._ready = false
this.hideBadge = hideBadge
this.language = language || 'en'
this.siteKey = siteKey
this.version = version
this.size = size
}
destroy () {
if (this._ready) {
this._ready = false
const { head } = document
const { style } = this._elements
const scripts = [...document.head.querySelectorAll('script')]
.filter(script => script.src.includes('recaptcha'))
if (scripts.length) {
scripts.forEach(script => head.removeChild(script))
}
if (head.contains(style)) {
head.removeChild(style)
}
}
}
async execute (action) {
try {
await this.init()
if ('grecaptcha' in window) {
return window.grecaptcha.execute(
this.siteKey,
{ action }
)
}
} catch (error) {
throw new Error('ReCaptcha error: Failed to execute')
}
}
getResponse () {
return new Promise((resolve, reject) => {
if ('grecaptcha' in window) {
if(this.size == 'invisible'){
window.grecaptcha.execute()
window.recaptchaSuccessCallback = token => {
this._eventBus.emit('recaptcha-success', token)
resolve(token)
}
window.recaptchaErrorCallback = error => {
this._eventBus.emit('recaptcha-error', error)
reject(error)
}
} else {
const response = window.grecaptcha.getResponse()
if (response) {
this._eventBus.emit('recaptcha-success', response)
resolve(response)
} else {
const errorMessage = 'Failed to execute'
this._eventBus.emit('recaptcha-error', errorMessage)
reject(errorMessage)
}
}
}
})
}
init () {
if (this._ready) {
return Promise.resolve()
}
this._eventBus = new EventEmitter()
this._elements = {
script: document.createElement('script'),
style: document.createElement('style')
}
const { script, style } = this._elements
script.addEventListener('error', () => {
document.head.removeChild(script)
throw new Error('ReCaptcha error: Failed to load script')
})
script.setAttribute('async', '')
script.setAttribute('defer', '')
if (this.version === 2) {
script.setAttribute('src', API_URL + '?hl=' + this.language)
} else {
script.setAttribute('src', API_URL + '?render=' + this.siteKey)
}
document.head.appendChild(script)
window.recaptchaSuccessCallback = (token) => this._eventBus.emit('recaptcha-success', token)
window.recaptchaExpiredCallback = () => this._eventBus.emit('recaptcha-expired')
window.recaptchaErrorCallback = () => this._eventBus.emit('recaptcha-error', 'Failed to execute')
this._ready = new Promise(resolve => {
script.addEventListener('load', () => {
if (this.version === 3 && this.hideBadge) {
style.innerHTML = '.grecaptcha-badge { display: none }'
document.head.appendChild(style)
} else if(this.version === 2 && this.hideBadge) {
// display: none DISABLES the spam checking!
// ref: https://stackoverflow.com/questions/44543157/how-to-hide-the-google-invisible-recaptcha-badge
style.innerHTML = '.grecaptcha-badge { visibility: hidden; }'
document.head.appendChild(style)
}
window.grecaptcha.ready(resolve)
})
})
return this._ready
}
on (event, callback) {
return this._eventBus.on(event, callback)
}
reset () {
if (this.version === 2) {
window.grecaptcha.reset()
}
}
}
export default function (_, inject) {
Vue.component('Recaptcha', () => import('./recaptcha.vue'))
inject('recaptcha', new ReCaptcha(<%= serialize(options) %>))
}