-
Notifications
You must be signed in to change notification settings - Fork 0
/
invisible-recaptcha.html
176 lines (161 loc) · 5.33 KB
/
invisible-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
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<link rel="import" href="../polymer/polymer.html">
<!--
A Polymer [Invisible Recaptcha](https://www.google.com/recaptcha) web-component for 🤖 detection (Polymer 1.x)
`<invisible-recaptcha>` is an web-component implementation of the Invisible Recaptcha by Google.
<invisible-recaptcha recaptcha-key=""></invisible-recaptcha>
### How to detect 🤖 :
1. You need to get an API Key [here](http://www.google.com/recaptcha/admin) (select invisible Recaptcha)
2. Place the element where you want
3. When you want to make the verification, just call the verify function.
4. Wait the end of the verification. `verification-finished` will fire.
5. Verify the token with your backend. [Full documentation](https://developers.google.com/recaptcha/docs/verify)
### How to use inside shadow-dom :
1. Set the `shadow-dom` attribute.
2. When you open the form call `render()`
@demo demo/index.html
-->
<dom-module id="invisible-recaptcha">
<template>
<style>
:host {
display: block;
}
</style>
<div
hidden$={{hideBadge}}
class="g-recaptcha"
data-sitekey$="{{recaptchaKey}}"
data-callback="handleResponse"
data-size="invisible"
data-badge$="{{badgePosition}}"
data-type$="{{recaptchaType}}"
data-tabindex$="{{tabindex}}">
</div>
</template>
</dom-module>
<script>
function handleResponse(token) {
if (document.querySelector('invisible-recaptcha')) {
document.querySelector('invisible-recaptcha')._handleResponse(token);
} else {
this.dispatchEvent(new Event('verificationFinished'));
}
this.dispatchEvent(new Event('verificationFinished'));
}
Polymer({
is: 'invisible-recaptcha',
/**
* Fired when the recaptcha verification is done.
*
* @event verification-finished
*/
properties: {
/**
* Recaptcha key. To obtain an API key, see http://www.google.com/recaptcha/admin.
*/
recaptchaKey: {
type: String,
notify: true
},
/**
* Last Response from the Recaptcha API. To handle the response, see https://developers.google.com/recaptcha/docs/verify
*/
lastResponse: {
type: String,
notify: true
},
/**
* The position of the Recaptcha Badge. Can be `bottomright`, `bottomleft` or `inline`
*/
badgePosition: {
type: String,
value: 'bottomright'
},
/**
* Set to true to hide the Recaptcha Badge
*/
hideBadge: {
type: Boolean,
value: false
},
/**
* Set to true if this element will be encapsulated inside the Shadow Dom
*/
shadowDom: {
type: Boolean,
value: false
},
/**
* The default type of captcha showed to the user. Can be `image` or `audio`
*/
recaptchaType: {
type: String,
value: 'image'
},
/**
* The tabindex of the challenge. If other elements in your page use tabindex, it should be set to make user navigation easier.
*/
tabindex: {
type: Number,
value: 0
},
},
ready: function() {
if (this.shadowDom) {
var self = this;
window.addEventListener('verificationFinished', function() {
self._handleResponse()
});
}
},
/**
* Verify if the user is a robot!
*/
verify: function() {
grecaptcha.execute();
},
/**
* Reset the recaptcha module
*/
reset: function() {
grecaptcha.reset();
},
/**
* Get the token from the last verification.
*
* @return Token
*/
getResponse: function() {
return lastResponse
},
/**
* Render the recaptcha inside the body to prevent Shadow-Dom problem.
*/
render: function(container) {
this.shadowHelper = document.createElement('div');
document.querySelector('body').appendChild(this.shadowHelper);
if (this.hideBadge) {
this.shadowHelper.hidden = true;
}
var renderOption = {
"sitekey": this.recaptchaKey,
"theme": "light",
'size': 'invisible',
'callback': 'handleResponse',
'badge': this.badgePosition,
'tabindex': this.tabindex,
'type': this.recaptchaType
};
grecaptcha.render(this.shadowHelper, renderOption);
},
_handleResponse: function() {
var token = grecaptcha.getResponse();
this.lastResponse = token;
this.fire('verification-finished', {
'token': token
});
grecaptcha.reset();
},
});
</script>