-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathreCaptcha.js
194 lines (163 loc) · 6.58 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
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/* eslint-disable no-undef */
// jscs:disable jsDoc
define(
[
'uiComponent',
'jquery',
'ko',
'Magento_ReCaptchaFrontendUi/js/registry'
],
function (Component, $, ko, registry, undefined) {
'use strict';
return Component.extend({
defaults: {
template: 'Magento_ReCaptchaFrontendUi/reCaptcha',
reCaptchaId: 'recaptcha'
},
_isApiRegistered: undefined,
initialize: function () {
this._super();
this._loadApi();
},
/**
* Loads recaptchaapi API and triggers event, when loaded
* @private
*/
_loadApi: function () {
var element, scriptTag;
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);
if (window.addedScriptTag === undefined) {
element = document.createElement('script');
scriptTag = document.getElementsByTagName('script')[0];
element.async = true;
element.src = 'https://www.google.com/recaptcha/api.js' +
'?onload=globalOnRecaptchaOnLoadCallback&render=explicit';
scriptTag.parentNode.insertBefore(element, scriptTag);
window.addedScriptTag = true;
}
},
/**
* Checking that reCAPTCHA is invisible type
* @returns {Boolean}
*/
getIsInvisibleRecaptcha: function () {
return this.settings.invisible;
},
/**
* reCAPTCHA callback
* @param {String} token
*/
reCaptchaCallback: function (token) {
if (this.getIsInvisibleRecaptcha()) {
this.tokenField.value = token;
this.$parentForm.submit();
}
},
/**
* Initialize reCAPTCHA after first rendering
*/
initCaptcha: function () {
var me = this,
$parentForm,
$wrapper,
$reCaptcha,
widgetId,
listeners;
if (this.captchaInitialized) {
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');
me = this;
let parameters = _.extend(
{
'callback': function (token) { // jscs:ignore jsDoc
me.reCaptchaCallback(token);
me.validateReCaptcha(true);
},
'expired-callback': function () {
me.validateReCaptcha(false);
}
},
this.settings.rendering
);
// eslint-disable-next-line no-undef
widgetId = grecaptcha.render(this.getReCaptchaId(), parameters);
if (this.getIsInvisibleRecaptcha() && $parentForm.length > 0) {
$parentForm.submit(function (event) {
if (!me.tokenField.value) {
// eslint-disable-next-line no-undef
grecaptcha.execute(widgetId);
event.preventDefault(event);
event.stopImmediatePropagation();
}
});
// 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;
}
registry.ids.push(this.getReCaptchaId());
registry.captchaList.push(widgetId);
registry.tokenFields.push(this.tokenField);
},
validateReCaptcha: function (state) {
if (!this.getIsInvisibleRecaptcha()) {
return $(document).find('input[type=checkbox].required-captcha').prop('checked', state);
}
},
/**
* Render reCAPTCHA
*/
renderReCaptcha: function () {
var me = this;
if (window.grecaptcha && window.grecaptcha.render) { // Check if reCAPTCHA is already loaded
me.initCaptcha();
} else { // Wait for reCAPTCHA to be loaded
$(window).on('recaptchaapiready', function () {
me.initCaptcha();
});
}
},
/**
* Get reCAPTCHA ID
* @returns {String}
*/
getReCaptchaId: function () {
return this.reCaptchaId;
}
});
}
);