This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
forked from cbalit/re-captcha
-
Notifications
You must be signed in to change notification settings - Fork 19
/
google-recaptcha.html
240 lines (225 loc) · 6.5 KB
/
google-recaptcha.html
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
<!--
Copyright 2015 The Polymer Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<link rel="import" href="../polymer/polymer.html">
<!--
Polymer element for Google reCAPTCHA
See https://developers.google.com/recaptcha
##### Example
```html
<google-recaptcha sitekey="yoursitekey"></google-recaptcha>
```
@element google-recaptcha
@blurb Element for Google reCAPTCHA
@status alpha
@homepage https://github.com/googlewebcomponents/re-captcha
@demo demo/index.html
-->
<dom-module id="google-recaptcha">
<style>
:host {
display: block;
}
</style>
<template><content></content></template>
</dom-module>
<script>
Recaptcha = (function () {
'use strict';
var SCRIPT_LOADED=false;
return Polymer({
is: 'google-recaptcha',
_API_URL:'https://www.google.com/recaptcha/api.js',
_RETRY_DELAY:200,
_delay:0,
_containerId:'',
_captchaId:'',
properties: {
/**
* The total time (in milliseconds) to wait for API loading
*/
timeout: {
type: Number,
value: 3000
},
/**
* Your sitekey
*
* (Provided on registration -- see https://developers.google.com/recaptcha/docs/start)
*/
sitekey: {
type: String,
value: ''
},
/**
* The color theme of the widget (`dark` or `light`)
*/
theme: {
type: String,
value: 'light'
},
/**
* The type of reCaptcha to serve (`image` or `audio`)
*/
type: {
type: String,
value: 'image'
},
/**
* The tabindex of the widget and challenge
*
* If other elements in your page use tabindex, this should be set to make user navigation easier.
*/
tabindex: {
type: Number,
value: 0
},
/**
* The response from the reCaptcha API
*/
response: {
type: String,
value: '',
notify: true
}
},
observers: [
'_validate(theme, type, timeout)'
],
_validate:function () {
if(this.theme!='dark' && this.theme!='light'){
this.theme='light';
}
if(this.type!='audio' && this.type!='image'){
this.type='image';
}
if(isNaN(this.timeout)){
this.timeout=3000;
}
},
_resetScriptLoaded: function () {
SCRIPT_LOADED=false;
},
_generateScriptApiTag:function () {
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.setAttribute('async','');
script.setAttribute('defer','');
script.setAttribute('type','text/javascript');
script.setAttribute('src',this._API_URL);
head.appendChild(script);
SCRIPT_LOADED=true;
},
_generateContainer:function () {
var div = document.createElement('div');
this._containerId=this._guidGenerator();
div.setAttribute('id',this._containerId);
this.appendChild(div);
},
_guidGenerator:function () {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
},
ready: function () {
},
attached: function () {
if(this.sitekey===''){
throw new Error("sitekey attribute is mandatory for re-captcha element");
}
if(!SCRIPT_LOADED){
this._generateScriptApiTag();
}
this._generateContainer();
this._renderWhenApiReady();
},
_renderWhenApiReady: function () {
if(!window.grecaptcha){
// Check if we can try again according to timeout
if(this._delay < this.timeout){
setTimeout(function() {
this._renderWhenApiReady()
}.bind(this), this._RETRY_DELAY);
// increment delay to check timeout
this._delay+=this._RETRY_DELAY;
}
else{
// We reach the timeout value
}
}
else{
this._render();
}
},
/**
* The `captcha-rendered` event is fired whenever the captcha widget is rendered.
*
* @event captcha-rendered
*/
/**
* The `_render` method renders the container as a reCaptcha widget and stores the ID of the newly created widget.
*/
_render: function () {
var _this=this;
//Render call
this._captchaId=window.grecaptcha.render(this._containerId, {
sitekey: this.sitekey,
theme: this.theme,
type: this.type,
tabindex: this.tabindex,
callback: _this._responseHandler.bind(this),
'expired-callback': _this._expiredHandler.bind(this)
});
this.fire('captcha-rendered',null);
},
/**
* The `reset` method resets the reCaptcha widget.
*/
reset: function () {
window.grecaptcha.reset(this._captchaId);
},
/**
* The `getResponse` method gets the response for the reCaptcha widget.
*
* @return {String} Returns the response.
*/
getResponse:function(){
return window.grecaptcha.getResponse(this._captchaId);
},
/**
* The `captcha-response` event is fired whenever we
* get the response from the reCaptcha API.
*
* @event captcha-response
* @param {Object} detail
* @param {string} detail.response The response
*/
/**
* The `responseHandler` method will store the response and fire the captcha-response. At least
* it will dispatch a captcha-response event with the response
*
* @method responseHandler
* @param {String} response response to store
*/
_responseHandler: function (response) {
this.response=response;
this.fire('captcha-response',{response:response});
},
/**
* The `captcha-expired` event is fired when the recaptcha response expires and the user
* needs to solve a new CAPTCHA.
*
* @event captcha-expired
*/
/**
* The `expiredHandler` method fires the captcha-expired event.
*/
_expiredHandler: function () {
this.fire('captcha-expired');
}
});
})();
</script>