-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathreCaptcha.js
228 lines (193 loc) · 7.91 KB
/
reCaptcha.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/* global grecaptcha */
define(
[
'uiComponent',
'jquery',
'ko',
'underscore',
'Magento_ReCaptchaFrontendUi/js/registry',
'Magento_ReCaptchaFrontendUi/js/reCaptchaScriptLoader',
'Magento_ReCaptchaFrontendUi/js/nonInlineReCaptchaRenderer'
], function (Component, $, ko, _, registry, reCaptchaLoader, nonInlineReCaptchaRenderer) {
'use strict';
return Component.extend({
defaults: {
template: 'Magento_ReCaptchaFrontendUi/reCaptcha',
reCaptchaId: 'recaptcha'
},
/**
* @inheritdoc
*/
initialize: function () {
this._super();
this._loadApi();
},
/**
* Loads recaptchaapi API and triggers event, when loaded
* @private
*/
_loadApi: function () {
if (this._isApiRegistered !== undefined) {
if (this._isApiRegistered === true) {
$(window).trigger('recaptchaapiready');
}
return;
}
this._isApiRegistered = false;
// global function
window.globalOnRecaptchaOnLoadCallback = function () {
this._isApiRegistered = true;
$(window).trigger('recaptchaapiready');
}.bind(this);
reCaptchaLoader.addReCaptchaScriptTag();
},
/**
* Checking that reCAPTCHA is invisible type
* @returns {Boolean}
*/
getIsInvisibleRecaptcha: function () {
if (this.settings ===
void 0) {
return false;
}
return this.settings.invisible;
},
/**
* reCAPTCHA callback
* @param {String} token
*/
reCaptchaCallback: function (token) {
var submitButton;
if (this.getIsInvisibleRecaptcha()) {
this.tokenField.value = token;
submitButton = this.$parentForm.find('button:not([type]), [type=submit]');
if (submitButton.length) { //eslint-disable-line max-depth
submitButton.attr('disabled', false);
}
this.$parentForm.submit();
}
},
/**
* Initialize reCAPTCHA after first rendering
*/
initCaptcha: function () {
var $parentForm,
$wrapper,
$reCaptcha,
widgetId,
parameters;
if (this.captchaInitialized || this.settings ===
void 0) {
return;
}
this.captchaInitialized = true;
/*
* Workaround for data-bind issue:
* We cannot use data-bind to link a dynamic id to our component
* See:
* https://stackoverflow.com/questions/46657573/recaptcha-the-bind-parameter-must-be-an-element-or-id
*
* We create a wrapper element with a wrapping id and we inject the real ID with jQuery.
* In this way we have no data-bind attribute at all in our reCAPTCHA div
*/
$wrapper = $('#' + this.getReCaptchaId() + '-wrapper');
$reCaptcha = $wrapper.find('.g-recaptcha');
$reCaptcha.attr('id', this.getReCaptchaId());
$parentForm = $wrapper.parents('form');
if (this.settings === undefined) {
return;
}
parameters = _.extend(
{
'callback': function (token) { // jscs:ignore jsDoc
this.reCaptchaCallback(token);
this.validateReCaptcha(true);
}.bind(this),
'expired-callback': function () {
this.validateReCaptcha(false);
}.bind(this)
},
this.settings.rendering
);
if (parameters.size === 'invisible' && parameters.badge !== 'inline') {
nonInlineReCaptchaRenderer.add($reCaptcha, parameters);
}
// eslint-disable-next-line no-undef
widgetId = grecaptcha.render(this.getReCaptchaId(), parameters);
this.initParentForm($parentForm, widgetId);
registry.ids.push(this.getReCaptchaId());
registry.captchaList.push(widgetId);
registry.tokenFields.push(this.tokenField);
},
/**
* Initialize parent form.
*
* @param {Object} parentForm
* @param {String} widgetId
*/
initParentForm: function (parentForm, widgetId) {
var listeners;
if (this.getIsInvisibleRecaptcha() && parentForm.length > 0) {
parentForm.on('submit', function (event) {
var submitButton;
if (!this.tokenField.value) {
submitButton = this.$parentForm.find('button:not([type]), [type=submit]');
if (submitButton.length) { //eslint-disable-line max-depth
submitButton.attr('disabled', true);
}
// eslint-disable-next-line no-undef
grecaptcha.execute(widgetId);
event.preventDefault(event);
event.stopImmediatePropagation();
}
}.bind(this));
// Move our (last) handler topmost. We need this to avoid submit bindings with ko.
listeners = $._data(parentForm[0], 'events').submit;
listeners.unshift(listeners.pop());
// Create a virtual token field
this.tokenField = $('<input type="text" name="token" style="display: none" />')[0];
this.$parentForm = parentForm;
parentForm.append(this.tokenField);
} else {
this.tokenField = null;
}
let submitButton = parentForm.find('button:not([type]), [type=submit]');
if (submitButton.length) {
submitButton.prop('disabled', false);
}
},
/**
* Validates reCAPTCHA
* @param {*} state
* @returns {jQuery}
*/
validateReCaptcha: function (state) {
if (!this.getIsInvisibleRecaptcha()) {
return $(document).find('input[type=checkbox].required-captcha').prop('checked', state);
}
},
/**
* Render reCAPTCHA
*/
renderReCaptcha: function () {
if (window.grecaptcha && window.grecaptcha.render) { // Check if reCAPTCHA is already loaded
this.initCaptcha();
} else { // Wait for reCAPTCHA to be loaded
$(window).on('recaptchaapiready', function () {
this.initCaptcha();
}.bind(this));
}
},
/**
* Get reCAPTCHA ID
* @returns {String}
*/
getReCaptchaId: function () {
return this.reCaptchaId;
}
});
});