This repository was archived by the owner on Aug 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathangular-recaptcha.js
More file actions
412 lines (346 loc) · 14.6 KB
/
angular-recaptcha.js
File metadata and controls
412 lines (346 loc) · 14.6 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
* @license angular-recaptcha build:2018-01-03
* https://github.com/vividcortex/angular-recaptcha
* Copyright (c) 2018 VividCortex
**/
/*global angular, Recaptcha */
(function (ng) {
'use strict';
ng.module('vcRecaptcha', []);
}(angular));
/*global angular */
(function (ng) {
'use strict';
function throwNoKeyException() {
throw new Error('You need to set the "key" attribute to your public reCaptcha key. If you don\'t have a key, please get one from https://www.google.com/recaptcha/admin/create');
}
var app = ng.module('vcRecaptcha');
/**
* An angular service to wrap the reCaptcha API
*/
app.provider('vcRecaptchaService', function(){
var provider = this;
var config = {};
provider.onLoadFunctionName = 'vcRecaptchaApiLoaded';
/**
* Sets the reCaptcha configuration values which will be used by default is not specified in a specific directive instance.
*
* @since 2.5.0
* @param defaults object which overrides the current defaults object.
*/
provider.setDefaults = function(defaults){
ng.copy(defaults, config);
};
/**
* Sets the reCaptcha key which will be used by default is not specified in a specific directive instance.
*
* @since 2.5.0
* @param siteKey the reCaptcha public key (refer to the README file if you don't know what this is).
*/
provider.setSiteKey = function(siteKey){
config.key = siteKey;
};
/**
* Sets the reCaptcha theme which will be used by default is not specified in a specific directive instance.
*
* @since 2.5.0
* @param theme The reCaptcha theme.
*/
provider.setTheme = function(theme){
config.theme = theme;
};
/**
* Sets the reCaptcha stoken which will be used by default is not specified in a specific directive instance.
*
* @since 2.5.0
* @param stoken The reCaptcha stoken.
*/
provider.setStoken = function(stoken){
config.stoken = stoken;
};
/**
* Sets the reCaptcha size which will be used by default is not specified in a specific directive instance.
*
* @since 2.5.0
* @param size The reCaptcha size.
*/
provider.setSize = function(size){
config.size = size;
};
/**
* Sets the reCaptcha type which will be used by default is not specified in a specific directive instance.
*
* @since 2.5.0
* @param type The reCaptcha type.
*/
provider.setType = function(type){
config.type = type;
};
/**
* Sets the reCaptcha language which will be used by default is not specified in a specific directive instance.
*
* @param lang The reCaptcha language.
*/
provider.setLang = function(lang){
config.lang = lang;
};
/**
* Sets the reCaptcha badge position which will be used by default if not specified in a specific directive instance.
*
* @param badge The reCaptcha badge position.
*/
provider.setBadge = function(badge){
config.badge = badge;
};
/**
* Sets the reCaptcha configuration values which will be used by default is not specified in a specific directive instance.
*
* @since 2.5.0
* @param onLoadFunctionName string name which overrides the name of the onload function. Should match what is in the recaptcha script querystring onload value.
*/
provider.setOnLoadFunctionName = function(onLoadFunctionName){
provider.onLoadFunctionName = onLoadFunctionName;
};
provider.$get = ['$rootScope','$window', '$q', '$document', '$interval', function ($rootScope, $window, $q, $document, $interval) {
var deferred = $q.defer(), promise = deferred.promise, instances = {}, recaptcha;
$window.vcRecaptchaApiLoadedCallback = $window.vcRecaptchaApiLoadedCallback || [];
var callback = function () {
recaptcha = $window.grecaptcha;
deferred.resolve(recaptcha);
};
$window.vcRecaptchaApiLoadedCallback.push(callback);
$window[provider.onLoadFunctionName] = function () {
$window.vcRecaptchaApiLoadedCallback.forEach(function(callback) {
callback();
});
};
function getRecaptcha() {
if (!!recaptcha) {
return $q.when(recaptcha);
}
return promise;
}
function validateRecaptchaInstance() {
if (!recaptcha) {
throw new Error('reCaptcha has not been loaded yet.');
}
}
// Check if grecaptcha is not defined already.
if (ng.isDefined($window.grecaptcha)) {
callback();
} else if ($window.document.querySelector('script[src^="https://www.google.com/recaptcha/api.js"]')) {
// wait for script to be loaded.
var intervalWait = $interval(function() {
if (ng.isDefined($window.grecaptcha)) {
$interval.cancel(intervalWait);
callback();
}
}, 25);
} else {
// Generate link on demand
var script = $window.document.createElement('script');
script.async = true;
script.defer = true;
script.src = 'https://www.google.com/recaptcha/api.js?onload='+provider.onLoadFunctionName+'&render=explicit';
$document.find('body')[0].appendChild(script);
}
return {
/**
* Creates a new reCaptcha object
*
* @param elm the DOM element where to put the captcha
* @param conf the captcha object configuration
* @throws NoKeyException if no key is provided in the provider config or the directive instance (via attribute)
*/
create: function (elm, conf) {
conf.sitekey = conf.key || config.key;
conf.theme = conf.theme || config.theme;
conf.stoken = conf.stoken || config.stoken;
conf.size = conf.size || config.size;
conf.type = conf.type || config.type;
conf.hl = conf.lang || config.lang;
conf.badge = conf.badge || config.badge;
if (!conf.sitekey || conf.sitekey.length !== 40) {
throwNoKeyException();
}
return getRecaptcha().then(function (recaptcha) {
var widgetId = recaptcha.render(elm, conf);
instances[widgetId] = elm;
return widgetId;
});
},
/**
* Reloads the reCaptcha
*/
reload: function (widgetId) {
validateRecaptchaInstance();
recaptcha.reset(widgetId);
// Let everyone know this widget has been reset.
$rootScope.$broadcast('reCaptchaReset', widgetId);
},
/**
* Executes the reCaptcha
*/
execute: function (widgetId) {
validateRecaptchaInstance();
recaptcha.execute(widgetId);
},
/**
* Get/Set reCaptcha language
*/
useLang: function (widgetId, lang) {
var instance = instances[widgetId];
if (instance) {
var iframe = instance.querySelector('iframe');
if (lang) {
// Setter
if (iframe && iframe.src) {
var s = iframe.src;
if (/[?&]hl=/.test(s)) {
s = s.replace(/([?&]hl=)\w+/, '$1' + lang);
} else {
s += ((s.indexOf('?') === -1) ? '?' : '&') + 'hl=' + lang;
}
iframe.src = s;
}
} else {
// Getter
if (iframe && iframe.src && /[?&]hl=\w+/.test(iframe.src)) {
return iframe.src.replace(/.+[?&]hl=(\w+)([^\w].+)?/, '$1');
} else {
return null;
}
}
} else {
throw new Error('reCaptcha Widget ID not exists', widgetId);
}
},
/**
* Gets the response from the reCaptcha widget.
*
* @see https://developers.google.com/recaptcha/docs/display#js_api
*
* @returns {String}
*/
getResponse: function (widgetId) {
validateRecaptchaInstance();
return recaptcha.getResponse(widgetId);
},
/**
* Gets reCaptcha instance and configuration
*/
getInstance: function (widgetId) {
return instances[widgetId];
},
/**
* Destroy reCaptcha instance.
*/
destroy: function (widgetId) {
delete instances[widgetId];
}
};
}];
});
}(angular));
/*global angular, Recaptcha */
(function (ng) {
'use strict';
var app = ng.module('vcRecaptcha');
app.directive('vcRecaptcha', ['$document', '$timeout', 'vcRecaptchaService', '$q', function ($document, $timeout, vcRecaptcha, $q) {
return {
restrict: 'A',
require: '?^^form',
scope: {
response: '=?ngModel',
key: '=?',
stoken: '=?',
theme: '=?',
size: '=?',
type: '=?',
lang: '=?',
badge: '=?',
tabindex: '=?',
required: '=?',
onCreate: '&',
onSuccess: '&',
onExpire: '&'
},
link: function (scope, elm, attrs, ctrl) {
scope.widgetId = null;
if (ctrl && ng.isDefined(attrs.required)) {
scope.$watch('required', validate);
}
var removeCreationListener = scope.$watch('key', function (key) {
// Abort if undefined or null
if (ng.isUndefined(key) || key === null) {
return;
}
var callback = function (gRecaptchaResponse) {
// Safe $apply
$timeout(function () {
scope.response = gRecaptchaResponse;
validate();
// Notify about the response availability
scope.onSuccess({response: gRecaptchaResponse, widgetId: scope.widgetId});
});
};
// Accept a promise, or resolve immediately if the value is a bare string
$q.resolve(key).then(function (resolved) {
return vcRecaptcha.create(elm[0], {
callback: callback,
key: resolved,
stoken: scope.stoken || attrs.stoken || null,
theme: scope.theme || attrs.theme || null,
type: scope.type || attrs.type || null,
lang: scope.lang || attrs.lang || null,
tabindex: scope.tabindex || attrs.tabindex || null,
size: scope.size || attrs.size || null,
badge: scope.badge || attrs.badge || null,
'expired-callback': expired
});
}).then(function (widgetId) {
// The widget has been created
validate();
scope.widgetId = widgetId;
scope.onCreate({widgetId: widgetId});
scope.$on('$destroy', destroy);
scope.$on('reCaptchaReset', function (event, resetWidgetId) {
if (ng.isUndefined(resetWidgetId) || widgetId === resetWidgetId) {
scope.response = '';
validate();
}
});
});
// Remove this listener to avoid creating the widget more than once.
removeCreationListener();
});
function destroy() {
if (ctrl) {
// reset the validity of the form if we were removed
ctrl.$setValidity('recaptcha', null);
}
cleanup();
}
function expired() {
// Safe $apply
$timeout(function () {
scope.response = '';
validate();
// Notify about the response availability
scope.onExpire({widgetId: scope.widgetId});
});
}
function validate() {
if (ctrl) {
ctrl.$setValidity('recaptcha', scope.required === false ? null : Boolean(scope.response));
}
}
function cleanup() {
vcRecaptcha.destroy(scope.widgetId);
// removes elements reCaptcha added.
ng.element($document[0].querySelectorAll('.pls-container')).parent().remove();
}
}
};
}]);
}(angular));