-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
332 lines (278 loc) · 8.08 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>reCAPTCHA Tester</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<style>
body {
background-color: #CEE;
padding: 1rem 0.5rem 0 0.5rem;
}
main {
background-color: #FFF;
border-radius: 3px;
max-width: 600px;
margin: auto;
padding: 0.5rem;
box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.5);
}
.thin-border {
border: 1px solid #CCC;
border-radius: 3px;
padding: 0.5rem;
margin-bottom: 1rem;
}
.form-check-label {
user-select: none;
}
.form-buttons button {
flex-grow: 1;
flex-basis: 0;
}
.form-buttons button:not(:last-child) {
margin-right: 0.5rem;
}
@keyframes fadetext {
0% { color: #000; }
80% { color: #000; }
100% { color: #777; }
}
#result {
font-family: monospace;
word-break: break-all;
}
.fade-text {
color: #777;
animation-name: fadetext;
animation-duration: 1.5s;
animation-timing-function: ease-in;
}
@media only screen and (max-width: 600px) {
#v2-checkbox-container {
transform: scale(0.95);
transform-origin: center center;
}
}
</style>
</head>
<body>
<main>
<h2 class="text-center">reCAPTCHA Tester</h2>
<form id="create-widget" class="thin-border">
<div class="mb-3">
<div class="form-label mb-1">
reCAPTCHA Type
</div>
<div class="form-check-inline">
<input
class="form-check-input"
required
type="radio"
name="type"
value="v2-checkbox"
id="v2-checkbox"
>
<label class="form-check-label" for="v2-checkbox">
V2 Checkbox
</label>
</div>
<div class="form-check-inline">
<input
class="form-check-input"
required
type="radio"
name="type"
value="v3"
id="v3"
>
<label class="form-check-label" for="v3">
V3
</label>
</div>
</div>
<div class="mb-3">
<label class="form-label" for="sitekey">
Site Key
</label>
<input class="form-control" required type="text" name="sitekey">
</div>
<div class="form-buttons d-flex justify-content-between">
<button
class="btn btn-primary"
type="submit"
>
Create
</button>
<button
class="btn btn-outline-primary"
type="button"
onclick="window.location.search = ''"
>
Reset
</button>
</div>
<div id="create-widget-error" class="mt-3 d-none text-danger">
Failed to create widget - perhaps the type or site key are wrong?
</div>
</form>
<form id="v3-container" class="thin-border d-none">
<div class="mb-3">
<label class="form-label" for="action">
Action (can be anything)
</label>
<input
class="form-control"
required type="text" value="submit"
name="action"
>
</div>
<div class="form-buttons d-flex justify-content-center">
<button
class="btn btn-primary"
type="submit"
>
Generate Token
</button>
</div>
<div id="generate-token-error" class="mt-3 d-none text-danger">
</div>
</form>
<div
class="mb-3 d-flex justify-content-center"
id="v2-checkbox-container"
></div>
<div class="thin-border d-none" id="result-container">
<p>Result copied to clipboard!</p>
<div id="result"></div>
</div>
</main>
<script>
const $ = (s, e = document) => e.querySelector(s);
const $$ = (s, e = document) => [...e.querySelectorAll(s)];
function parseQuery(text) {
const search = new URLSearchParams(text);
const entries = search.entries();
return Object.fromEntries(entries);
}
function formatQuery(data) {
const search = new URLSearchParams();
for (const [key, value] of Object.entries(data)) {
search.set(key, value);
}
return search.toString();
}
function updateForm(form, data) {
for (const [key, value] of Object.entries(data)) {
const elem = form.elements[key];
if (elem) {
elem.value = value;
}
}
}
function getFormData(form) {
const result = {};
for (const element of form.elements) {
if (element.name) {
result[element.name] = element.value;
}
}
return result;
}
function sleep(ms) {
return new Promise(ok => setTimeout(ok, ms));
}
function loadScript(source) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = source;
script.onload = () => resolve();
script.onerror = error => reject(error);
document.head.append(script);
});
}
function copyToClipboard(text) {
const textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
function recaptchaReady() {
return new Promise(ok => grecaptcha.ready(ok));
}
async function createCaptcha(type, sitekey) {
const baseUrl = 'https://www.google.com/recaptcha/api.js';
const query = (type === 'v3') ? { render: sitekey } : {};
const url = baseUrl + '?' + formatQuery(query);
const errorBox = $('#create-widget-error');
errorBox.classList.add('d-none');
try {
await loadScript(url);
await recaptchaReady();
} catch (error) {
errorBox.classList.remove('d-none');
}
if (type === 'v3') {
const formV3 = $('#v3-container');
formV3.classList.remove('d-none');
formV3.addEventListener('submit', event => {
event.preventDefault();
const data = getFormData(event.target);
v3GenerateToken(sitekey, data.action);
});
} else if (type === 'v2-checkbox') {
const id = grecaptcha.render($('#v2-checkbox-container'), {
sitekey: sitekey,
async callback(challenge) {
challengeSolved(challenge);
await sleep(1000);
grecaptcha.reset(id);
},
});
}
}
async function v3GenerateToken(sitekey, action) {
const errorBox = $('#generate-token-error');
errorBox.textContent = '';
errorBox.classList.add('d-none');
try {
const token = await grecaptcha.execute(sitekey, { action });
challengeSolved(token);
} catch (error) {
errorBox.textContent = error.message;
errorBox.classList.remove('d-none');
}
}
function challengeSolved(challenge) {
copyToClipboard(challenge);
$('#result-container').classList.remove('d-none');
const resultElem = $('#result');
resultElem.textContent = challenge;
resultElem.classList.remove('fade-text');
setTimeout(() => {
resultElem.classList.add('fade-text');
});
}
if (window.location.hostname === '127.0.0.1') {
window.location.hostname = 'localhost';
}
if (window.location.search) {
const query = parseQuery(window.location.search);
const allowedTypes = $$('input[name=type]').map(it => it.value);
if (
!allowedTypes.includes(query.type) ||
!query.sitekey
) {
window.location.search = '';
} else {
updateForm($('#create-widget'), query);
createCaptcha(query.type, query.sitekey).catch(console.error);
}
}
</script>
</body>
</html>