1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2019. Volodymyr Hryvinskyi. All rights reserved. |
3
|
|
|
* @author: <mailto:[email protected]> |
4
|
|
|
* @github: <https://github.com/hryvinskyi> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
define([ |
8
|
|
|
'jquery', |
9
|
|
|
'ko', |
10
|
|
|
'uiComponent', |
11
|
|
|
'./model/invisible-captcha' |
12
|
|
|
], function ($, ko, Component, invisibleCaptcha) { |
13
|
|
|
'use strict'; |
14
|
|
|
|
15
|
|
|
return Component.extend({ |
16
|
|
|
defaults: { |
17
|
|
|
template: 'Hryvinskyi_InvisibleCaptcha/invisible-captcha', |
18
|
|
|
action: '', |
19
|
|
|
captchaId: '' |
20
|
|
|
}, |
21
|
|
|
_initializedForms: [], |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Initialization |
25
|
|
|
*/ |
26
|
|
|
initialize: function () { |
27
|
|
|
this._super(); |
28
|
|
|
this._loadGoogleApi(); |
29
|
|
|
}, |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initialize Google ReCaptca Script |
33
|
|
|
* |
34
|
|
|
* @private |
35
|
|
|
*/ |
36
|
|
|
_loadGoogleApi: function () { |
37
|
|
|
var self = this; |
38
|
|
|
|
39
|
|
|
if (invisibleCaptcha.isApiLoad() === true) { |
40
|
|
|
$(window).trigger('recaptcha_api_ready_' + self.captchaId); |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
window.onloadCallbackGoogleRecapcha = function () { |
45
|
|
|
invisibleCaptcha.isApiLoaded(true); |
46
|
|
|
invisibleCaptcha.initializeForms.each(function (item) { |
47
|
|
|
self._initializeTokenField(item.element, item.self); |
48
|
|
|
}); |
49
|
|
|
$(window).trigger('recaptcha_api_ready_' + self.captchaId); |
50
|
|
|
}; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
if(invisibleCaptcha.isApiLoaded() === true) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
require([ |
58
|
|
|
'//www.google.com/recaptcha/api.js?onload=onloadCallbackGoogleRecapcha&render=' + self.siteKey |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
invisibleCaptcha.isApiLoad(true); |
62
|
|
|
}, |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Loads google API and triggers event, when loaded |
66
|
|
|
* |
67
|
|
|
* @private |
68
|
|
|
*/ |
69
|
|
|
_initializeTokenField: function (element, self) { |
70
|
|
|
if (invisibleCaptcha.initializedForms.indexOf(self.captchaId) === -1) { |
71
|
|
|
invisibleCaptcha.initializedForms.push(self.captchaId); |
72
|
|
|
|
73
|
|
|
var tokenField = $('<input type="hidden" name="hryvinskyi_invisible_token" />'), |
74
|
|
|
siteKey = self.siteKey, |
75
|
|
|
action = self.action; |
76
|
|
|
|
77
|
|
|
grecaptcha.ready(function () { |
|
|
|
|
78
|
|
|
grecaptcha |
|
|
|
|
79
|
|
|
.execute(siteKey, {action: action}) |
80
|
|
|
.then(function (token) { |
81
|
|
|
tokenField.val(token); |
82
|
|
|
}); |
83
|
|
|
}); |
84
|
|
|
tokenField.attr('data-action', action); |
85
|
|
|
$(element).append(tokenField); |
86
|
|
|
} |
87
|
|
|
}, |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Initialize recaptcha |
91
|
|
|
* |
92
|
|
|
* @param {Dom} element |
93
|
|
|
* @param {Object} self |
94
|
|
|
*/ |
95
|
|
|
initializeCaptcha: function (element, self) { |
96
|
|
|
if (invisibleCaptcha.isApiLoad() === true && invisibleCaptcha.isApiLoaded() !== true) { |
97
|
|
|
invisibleCaptcha.initializeForms.push({'element': element, self: self}); |
98
|
|
|
} else if(invisibleCaptcha.isApiLoaded() === true) { |
99
|
|
|
self._initializeTokenField(element, self); |
100
|
|
|
} else { |
101
|
|
|
$(window).on('recaptcha_api_ready_' + self.captchaId, function () { |
102
|
|
|
self._initializeTokenField(element, self); |
103
|
|
|
}); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
}); |
107
|
|
|
}); |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.