-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathplugin.js
211 lines (169 loc) · 5.78 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { EventEmitter } from 'events'
import Vue from 'vue'
const API_URL = 'https://www.recaptcha.net/recaptcha/api.js'
class ReCaptcha {
constructor ({ hideBadge, language, onloadCallback, mode, siteKey, version, size }) {
if (onloadCallback && typeof onloadCallback !== 'function') {
throw new Error('ReCaptcha error: onloadCallback must be a function')
}
if (!siteKey) {
throw new Error('ReCaptcha error: No key provided')
}
if (!version) {
throw new Error('ReCaptcha error: No version provided')
}
this._elements = {}
this._grecaptcha = null
this._eventBus = null
this._ready = false
this.hideBadge = hideBadge
this.language = language
this.onloadCallback = onloadCallback
this.siteKey = siteKey
this.version = version
this.size = size
this.mode = mode
}
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)
}
const badge = document.querySelector('.grecaptcha-badge')
if (badge) {
badge.remove()
}
}
}
async execute (action) {
try {
await this.init()
if ('grecaptcha' in window) {
return this._grecaptcha.execute(
this.siteKey,
{ action }
)
}
} catch (error) {
throw new Error(`ReCaptcha error: Failed to execute ${error}`)
}
}
getResponse (widgetId) {
return new Promise((resolve, reject) => {
if ('grecaptcha' in window) {
if(this.size == 'invisible'){
this._grecaptcha.execute(widgetId)
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 = this._grecaptcha.getResponse(widgetId)
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) {
// make sure caller waits until recaptcha get ready
return this._ready
}
this._eventBus = new EventEmitter()
this._elements = {
script: document.createElement('script'),
style: document.createElement('style')
}
const { script, style } = this._elements
script.setAttribute('async', '')
script.setAttribute('defer', '')
const params = []
if (this.version === 3) { params.push('render=' + this.siteKey) }
if (this.language) { params.push('hl=' + this.language) }
if (this.onloadCallback && typeof this.onloadCallback === 'function') {
if (this.version === 2) {
params.push('onload=recaptchaOnloadCallbackFunction')
}
}
let scriptUrl = API_URL
if (this.mode === 'enterprise') {
scriptUrl = scriptUrl.replace('api.js', 'enterprise.js')
params.push('render=' + this.siteKey)
}
script.setAttribute('src', scriptUrl + '?' + params.join('&'))
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')
window.recaptchaOnloadCallback = () => this._eventBus.emit('recaptcha-onload')
window.recaptchaOnloadCallbackFunction = () => {
this._eventBus.emit('recaptcha-onload')
if (typeof this.onloadCallback === 'function') {
this.onloadCallback();
}
}
this._ready = new Promise((resolve, reject) => {
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)
}
this._grecaptcha = window.grecaptcha.enterprise || window.grecaptcha
this._grecaptcha.ready(() => {
windows.recaptchaOnloadCallbackFunction()
resolve()
});
})
script.addEventListener('error', () => {
document.head.removeChild(script)
reject('ReCaptcha error: Failed to load script')
this._ready = null;
})
document.head.appendChild(script)
})
return this._ready
}
on (event, callback) {
return this._eventBus.on(event, callback)
}
reset (widgetId) {
if (this.version === 2 || typeof widgetId !== 'undefined') {
this._grecaptcha.reset(widgetId)
}
}
render (reference, { sitekey, theme }) {
return this._grecaptcha.render(reference.$el || reference, { sitekey, theme })
}
}
export default function (_, inject) {
const { recaptcha = {} } = _.$config || {}
const options = {
...<%= serialize(options) %>,
...recaptcha,
}
Vue.component('Recaptcha', () => import('./recaptcha.vue'))
inject('recaptcha', new ReCaptcha(options))
}