@cap.js/widget 0.1.34 → 0.1.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cap.min.js +1 -1
- package/package.json +1 -1
- package/src/cap.css +231 -0
- package/src/cap.js +648 -604
- package/src/worker.js +182 -113
- package/wasm-hashes.min.js +1 -26
package/src/cap.js
CHANGED
|
@@ -1,613 +1,657 @@
|
|
|
1
1
|
(() => {
|
|
2
|
-
|
|
2
|
+
const WASM_VERSION = "0.0.6";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
if (typeof window === "undefined") {
|
|
5
5
|
return;
|
|
6
6
|
}
|
|
7
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
|
-
|
|
8
|
+
const capFetch = (u, _conf = {}) => {
|
|
9
|
+
const conf = {
|
|
10
|
+
..._conf,
|
|
11
|
+
headers: {
|
|
12
|
+
...(_conf.headers || {}),
|
|
13
|
+
"Cap-Stamp": btoa(
|
|
14
|
+
String.fromCharCode(
|
|
15
|
+
...[[Date.now()]].map((n) => [n >> 24, n >> 16, n >> 8, n[0]].map((x) => x & 255))[0],
|
|
16
|
+
),
|
|
17
|
+
).replace(/=/g, ""),
|
|
18
|
+
"Cap-Solver": `0,${WASM_VERSION}`,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
if (window?.CAP_CUSTOM_FETCH) {
|
|
23
|
+
return window.CAP_CUSTOM_FETCH(u, conf);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return fetch(u, conf);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function prng(seed, length) {
|
|
30
|
+
function fnv1a(str) {
|
|
31
|
+
let hash = 2166136261;
|
|
32
|
+
for (let i = 0; i < str.length; i++) {
|
|
33
|
+
hash ^= str.charCodeAt(i);
|
|
34
|
+
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
|
|
35
|
+
}
|
|
36
|
+
return hash >>> 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let state = fnv1a(seed);
|
|
40
|
+
let result = "";
|
|
41
|
+
|
|
42
|
+
function next() {
|
|
43
|
+
state ^= state << 13;
|
|
44
|
+
state ^= state >>> 17;
|
|
45
|
+
state ^= state << 5;
|
|
46
|
+
return state >>> 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
while (result.length < length) {
|
|
50
|
+
const rnd = next();
|
|
51
|
+
result += rnd.toString(16).padStart(8, "0");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return result.substring(0, length);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let wasmModulePromise = null;
|
|
58
|
+
|
|
59
|
+
const getWasmModule = () => {
|
|
60
|
+
if (wasmModulePromise) return wasmModulePromise;
|
|
61
|
+
|
|
62
|
+
const wasmUrl =
|
|
63
|
+
window.CAP_CUSTOM_WASM_URL ||
|
|
64
|
+
`https://cdn.jsdelivr.net/npm/@cap.js/wasm@${WASM_VERSION}/browser/cap_wasm_bg.wasm`;
|
|
65
|
+
|
|
66
|
+
wasmModulePromise = fetch(wasmUrl)
|
|
67
|
+
.then((r) => {
|
|
68
|
+
if (!r.ok) throw new Error(`Failed to fetch wasm: ${r.status}`);
|
|
69
|
+
return r.arrayBuffer();
|
|
70
|
+
})
|
|
71
|
+
.then((buf) => WebAssembly.compile(buf))
|
|
72
|
+
.catch((e) => {
|
|
73
|
+
wasmModulePromise = null;
|
|
74
|
+
throw e;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return wasmModulePromise;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
if (typeof WebAssembly === "object" && typeof WebAssembly.compile === "function") {
|
|
81
|
+
getWasmModule().catch(() => {});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const prefersReducedMotion = () =>
|
|
85
|
+
window.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false;
|
|
86
|
+
|
|
87
|
+
class CapWidget extends HTMLElement {
|
|
88
|
+
#workerUrl = "";
|
|
89
|
+
#resetTimer = null;
|
|
90
|
+
#workersCount = navigator.hardwareConcurrency || 8;
|
|
91
|
+
token = null;
|
|
92
|
+
#shadow;
|
|
93
|
+
#div;
|
|
94
|
+
#host;
|
|
95
|
+
#solving = false;
|
|
96
|
+
#eventHandlers;
|
|
97
|
+
|
|
98
|
+
getI18nText(key, defaultValue) {
|
|
99
|
+
return this.getAttribute(`data-cap-i18n-${key}`) || defaultValue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static get observedAttributes() {
|
|
103
|
+
return [
|
|
104
|
+
"onsolve",
|
|
105
|
+
"onprogress",
|
|
106
|
+
"onreset",
|
|
107
|
+
"onerror",
|
|
108
|
+
"data-cap-worker-count",
|
|
109
|
+
"data-cap-i18n-initial-state",
|
|
110
|
+
];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
constructor() {
|
|
114
|
+
super();
|
|
115
|
+
if (this.#eventHandlers) {
|
|
116
|
+
this.#eventHandlers.forEach((handler, eventName) => {
|
|
117
|
+
this.removeEventListener(eventName.slice(2), handler);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
this.#eventHandlers = new Map();
|
|
122
|
+
this.boundHandleProgress = this.handleProgress.bind(this);
|
|
123
|
+
this.boundHandleSolve = this.handleSolve.bind(this);
|
|
124
|
+
this.boundHandleError = this.handleError.bind(this);
|
|
125
|
+
this.boundHandleReset = this.handleReset.bind(this);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
initialize() {
|
|
129
|
+
this.#workerUrl = URL.createObjectURL(
|
|
130
|
+
// this placeholder will be replaced with the actual worker by the build script
|
|
131
|
+
|
|
132
|
+
new Blob([`%%workerScript%%`], {
|
|
133
|
+
type: "application/javascript",
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
attributeChangedCallback(name, _, value) {
|
|
139
|
+
if (name.startsWith("on")) {
|
|
140
|
+
const eventName = name.slice(2);
|
|
141
|
+
const oldHandler = this.#eventHandlers.get(name);
|
|
142
|
+
if (oldHandler) {
|
|
143
|
+
this.removeEventListener(eventName, oldHandler);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (value) {
|
|
147
|
+
const handler = (event) => {
|
|
148
|
+
const callback = this.getAttribute(name);
|
|
149
|
+
if (typeof window[callback] === "function") {
|
|
150
|
+
window[callback].call(this, event);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
this.#eventHandlers.set(name, handler);
|
|
154
|
+
this.addEventListener(eventName, handler);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (name === "data-cap-worker-count") {
|
|
159
|
+
this.setWorkersCount(parseInt(value, 10));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (
|
|
163
|
+
name === "data-cap-i18n-initial-state" &&
|
|
164
|
+
this.#div &&
|
|
165
|
+
this.#div?.querySelector(".label.active")
|
|
166
|
+
) {
|
|
167
|
+
this.animateLabel(this.getI18nText("initial-state", "Verify you're human"));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async connectedCallback() {
|
|
172
|
+
this.#host = this;
|
|
173
|
+
this.#shadow = this.attachShadow({ mode: "open" });
|
|
174
|
+
this.#div = document.createElement("div");
|
|
175
|
+
this.createUI();
|
|
176
|
+
this.addEventListeners();
|
|
177
|
+
this.initialize();
|
|
178
|
+
this.#div.removeAttribute("disabled");
|
|
179
|
+
|
|
180
|
+
const workers = this.getAttribute("data-cap-worker-count");
|
|
181
|
+
const parsedWorkers = workers ? parseInt(workers, 10) : null;
|
|
182
|
+
this.setWorkersCount(parsedWorkers || navigator.hardwareConcurrency || 8);
|
|
183
|
+
const fieldName = this.getAttribute("data-cap-hidden-field-name") || "cap-token";
|
|
184
|
+
this.#host.innerHTML = `<input type="hidden" name="${fieldName}">`;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async solve() {
|
|
188
|
+
if (this.#solving) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
this.#solving = true;
|
|
194
|
+
this.updateUI("verifying", this.getI18nText("verifying-label", "Verifying..."), true);
|
|
195
|
+
|
|
196
|
+
this.#div.setAttribute(
|
|
197
|
+
"aria-label",
|
|
198
|
+
this.getI18nText("verifying-aria-label", "Verifying you're a human, please wait"),
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
this.dispatchEvent("progress", { progress: 0 });
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
let apiEndpoint = this.getAttribute("data-cap-api-endpoint");
|
|
205
|
+
|
|
206
|
+
if (!apiEndpoint && window?.CAP_CUSTOM_FETCH) {
|
|
207
|
+
apiEndpoint = "/";
|
|
208
|
+
} else if (!apiEndpoint)
|
|
209
|
+
throw new Error(
|
|
210
|
+
"Missing API endpoint. Either custom fetch or an API endpoint must be provided.",
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
if (!apiEndpoint.endsWith("/")) {
|
|
197
214
|
apiEndpoint += "/";
|
|
198
215
|
}
|
|
199
216
|
|
|
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
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
217
|
+
const { challenge, token } = await (
|
|
218
|
+
await capFetch(`${apiEndpoint}challenge`, {
|
|
219
|
+
method: "POST",
|
|
220
|
+
})
|
|
221
|
+
).json();
|
|
222
|
+
|
|
223
|
+
let challenges = challenge;
|
|
224
|
+
|
|
225
|
+
if (!Array.isArray(challenges)) {
|
|
226
|
+
let i = 0;
|
|
227
|
+
|
|
228
|
+
challenges = Array.from({ length: challenge.c }, () => {
|
|
229
|
+
i = i + 1;
|
|
230
|
+
|
|
231
|
+
return [prng(`${token}${i}`, challenge.s), prng(`${token}${i}d`, challenge.d)];
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const solutions = await this.solveChallenges(challenges);
|
|
236
|
+
|
|
237
|
+
const resp = await (
|
|
238
|
+
await capFetch(`${apiEndpoint}redeem`, {
|
|
239
|
+
method: "POST",
|
|
240
|
+
body: JSON.stringify({ token, solutions }),
|
|
241
|
+
headers: { "Content-Type": "application/json" },
|
|
242
|
+
})
|
|
243
|
+
).json();
|
|
244
|
+
|
|
245
|
+
this.dispatchEvent("progress", { progress: 100 });
|
|
246
|
+
|
|
247
|
+
if (!resp.success) throw new Error("Invalid solution");
|
|
248
|
+
const fieldName = this.getAttribute("data-cap-hidden-field-name") || "cap-token";
|
|
249
|
+
if (this.querySelector(`input[name='${fieldName}']`)) {
|
|
250
|
+
this.querySelector(`input[name='${fieldName}']`).value = resp.token;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
this.dispatchEvent("solve", { token: resp.token });
|
|
254
|
+
this.token = resp.token;
|
|
255
|
+
|
|
256
|
+
if (this.#resetTimer) clearTimeout(this.#resetTimer);
|
|
257
|
+
const expiresIn = new Date(resp.expires).getTime() - Date.now();
|
|
258
|
+
if (expiresIn > 0 && expiresIn < 24 * 60 * 60 * 1000) {
|
|
259
|
+
this.#resetTimer = setTimeout(() => this.reset(), expiresIn);
|
|
260
|
+
} else {
|
|
261
|
+
this.error("Invalid expiration time");
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
this.#div.setAttribute(
|
|
265
|
+
"aria-label",
|
|
266
|
+
this.getI18nText(
|
|
267
|
+
"verified-aria-label",
|
|
268
|
+
"We have verified you're a human, you may now continue",
|
|
269
|
+
),
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
return { success: true, token: this.token };
|
|
273
|
+
} catch (err) {
|
|
274
|
+
this.#div.setAttribute(
|
|
275
|
+
"aria-label",
|
|
276
|
+
this.getI18nText("error-aria-label", "An error occurred, please try again"),
|
|
277
|
+
);
|
|
278
|
+
this.error(err.message);
|
|
279
|
+
throw err;
|
|
280
|
+
}
|
|
281
|
+
} finally {
|
|
282
|
+
this.#solving = false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async solveChallenges(challenges) {
|
|
287
|
+
const total = challenges.length;
|
|
288
|
+
let completed = 0;
|
|
289
|
+
|
|
290
|
+
let wasmModule = null;
|
|
291
|
+
const wasmSupported =
|
|
292
|
+
typeof WebAssembly === "object" && typeof WebAssembly.instantiate === "function";
|
|
293
|
+
|
|
294
|
+
if (wasmSupported) {
|
|
295
|
+
try {
|
|
296
|
+
wasmModule = await getWasmModule();
|
|
297
|
+
} catch (e) {
|
|
298
|
+
console.warn("[cap] wasm unavailable, falling back to JS solver:", e);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (!wasmSupported) {
|
|
303
|
+
if (!this.#shadow.querySelector(".warning")) {
|
|
304
|
+
const warningEl = document.createElement("div");
|
|
305
|
+
warningEl.className = "warning";
|
|
306
|
+
warningEl.style.cssText = `width:var(--cap-widget-width,230px);background:rgb(237,56,46);color:white;padding:4px 6px;padding-bottom:calc(var(--cap-border-radius,14px) + 5px);font-size:10px;box-sizing:border-box;font-family:system-ui;border-top-left-radius:8px;border-top-right-radius:8px;text-align:center;user-select:none;margin-bottom:-35.5px;opacity:0;transition:margin-bottom .3s,opacity .3s;`;
|
|
307
|
+
warningEl.innerText = this.getI18nText(
|
|
308
|
+
"wasm-disabled",
|
|
309
|
+
"Enable WASM for significantly faster solving",
|
|
310
|
+
);
|
|
311
|
+
this.#shadow.insertBefore(warningEl, this.#shadow.firstChild);
|
|
312
|
+
setTimeout(() => {
|
|
313
|
+
warningEl.style.marginBottom = `calc(-1 * var(--cap-border-radius, 14px))`;
|
|
314
|
+
warningEl.style.opacity = 1;
|
|
315
|
+
}, 10);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const workers = Array(this.#workersCount)
|
|
320
|
+
.fill(null)
|
|
321
|
+
.map(() => {
|
|
322
|
+
try {
|
|
323
|
+
return new Worker(this.#workerUrl);
|
|
324
|
+
} catch (error) {
|
|
325
|
+
console.error("[cap] Failed to create worker:", error);
|
|
326
|
+
throw new Error("Worker creation failed");
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const solveSingleChallenge = ([salt, target], workerId) =>
|
|
331
|
+
new Promise((resolve, reject) => {
|
|
332
|
+
const worker = workers[workerId];
|
|
333
|
+
if (!worker) {
|
|
334
|
+
reject(new Error("Worker not available"));
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
worker.onmessage = ({ data }) => {
|
|
339
|
+
if (!data.found) return;
|
|
340
|
+
|
|
341
|
+
completed++;
|
|
342
|
+
this.dispatchEvent("progress", {
|
|
343
|
+
progress: Math.round((completed / total) * 100),
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
resolve(data.nonce);
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
worker.onerror = (err) => {
|
|
350
|
+
this.error(`Error in worker: ${err.message || err}`);
|
|
351
|
+
reject(err);
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
if (wasmModule) {
|
|
355
|
+
worker.postMessage({ salt, target, wasmModule }, []);
|
|
356
|
+
} else {
|
|
357
|
+
worker.postMessage({ salt, target });
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
const results = [];
|
|
362
|
+
try {
|
|
363
|
+
for (let i = 0; i < challenges.length; i += this.#workersCount) {
|
|
364
|
+
const chunk = challenges.slice(i, Math.min(i + this.#workersCount, challenges.length));
|
|
365
|
+
const chunkResults = await Promise.all(
|
|
366
|
+
chunk.map((c, idx) => solveSingleChallenge(c, idx)),
|
|
367
|
+
);
|
|
368
|
+
results.push(...chunkResults);
|
|
369
|
+
}
|
|
370
|
+
} finally {
|
|
371
|
+
workers.forEach((w) => {
|
|
372
|
+
if (w) {
|
|
373
|
+
try {
|
|
374
|
+
w.terminate();
|
|
375
|
+
} catch (error) {
|
|
376
|
+
console.error("[cap] error terminating worker:", error);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return results;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
setWorkersCount(workers) {
|
|
386
|
+
const parsedWorkers = parseInt(workers, 10);
|
|
387
|
+
const maxWorkers = Math.min(navigator.hardwareConcurrency || 8, 16);
|
|
388
|
+
this.#workersCount =
|
|
389
|
+
!Number.isNaN(parsedWorkers) && parsedWorkers > 0 && parsedWorkers <= maxWorkers
|
|
390
|
+
? parsedWorkers
|
|
391
|
+
: navigator.hardwareConcurrency || 8;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
createUI() {
|
|
395
|
+
this.#div.classList.add("captcha");
|
|
396
|
+
this.#div.setAttribute("role", "button");
|
|
397
|
+
this.#div.setAttribute("tabindex", "0");
|
|
398
|
+
this.#div.setAttribute(
|
|
399
|
+
"aria-label",
|
|
400
|
+
this.getI18nText("verify-aria-label", "Click to verify you're a human"),
|
|
401
|
+
);
|
|
402
|
+
this.#div.setAttribute("aria-live", "polite");
|
|
403
|
+
this.#div.setAttribute("disabled", "true");
|
|
404
|
+
this.#div.innerHTML = `<div class="checkbox" part="checkbox"><svg class="progress-ring" viewBox="0 0 32 32"><circle class="progress-ring-bg" cx="16" cy="16" r="14"></circle><circle class="progress-ring-circle" cx="16" cy="16" r="14"></circle></svg></div><p part="label" class="label-wrapper"><span class="label active">${this.getI18nText(
|
|
405
|
+
"initial-state",
|
|
406
|
+
"Verify you're human",
|
|
407
|
+
)}</span></p><a part="attribution" aria-label="Secured by Cap" href="https://capjs.js.org/" class="credits" target="_blank" rel="follow noopener" title="Secured by Cap: Self-hosted CAPTCHA for the modern web.">Cap</a>`;
|
|
408
|
+
|
|
409
|
+
const css = `%%capCSS%%`;
|
|
410
|
+
|
|
411
|
+
this.#shadow.innerHTML = `<style${window.CAP_CSS_NONCE ? ` nonce=${window.CAP_CSS_NONCE}` : ""}>${css}</style>`;
|
|
412
|
+
|
|
413
|
+
this.#shadow.appendChild(this.#div);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
addEventListeners() {
|
|
417
|
+
if (!this.#div) return;
|
|
418
|
+
|
|
419
|
+
this.#div.querySelector("a").addEventListener("click", (e) => {
|
|
420
|
+
e.stopPropagation();
|
|
421
|
+
e.preventDefault();
|
|
422
|
+
window.open("https://capjs.js.org", "_blank");
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
this.#div.addEventListener("click", () => {
|
|
426
|
+
if (!this.#div.hasAttribute("disabled")) this.solve();
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
this.#div.addEventListener("keydown", (e) => {
|
|
430
|
+
if ((e.key === "Enter" || e.key === " ") && !this.#div.hasAttribute("disabled")) {
|
|
431
|
+
e.preventDefault();
|
|
432
|
+
e.stopPropagation();
|
|
433
|
+
this.solve();
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
this.addEventListener("progress", this.boundHandleProgress);
|
|
438
|
+
this.addEventListener("solve", this.boundHandleSolve);
|
|
439
|
+
this.addEventListener("error", this.boundHandleError);
|
|
440
|
+
this.addEventListener("reset", this.boundHandleReset);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
animateLabel(text) {
|
|
444
|
+
if (!this.#div) return;
|
|
445
|
+
const wrapper = this.#div.querySelector(".label-wrapper");
|
|
446
|
+
if (!wrapper) return;
|
|
447
|
+
|
|
448
|
+
if (prefersReducedMotion()) {
|
|
449
|
+
const current = wrapper.querySelector(".label.active");
|
|
450
|
+
if (current) {
|
|
451
|
+
current.textContent = text;
|
|
452
|
+
} else {
|
|
453
|
+
const span = document.createElement("span");
|
|
454
|
+
span.className = "label active";
|
|
455
|
+
span.textContent = text;
|
|
456
|
+
wrapper.appendChild(span);
|
|
457
|
+
}
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const current = wrapper.querySelector(".label.active");
|
|
462
|
+
|
|
463
|
+
const next = document.createElement("span");
|
|
464
|
+
next.className = "label";
|
|
465
|
+
next.textContent = text;
|
|
466
|
+
wrapper.appendChild(next);
|
|
467
|
+
|
|
468
|
+
void next.offsetWidth;
|
|
469
|
+
|
|
470
|
+
next.classList.add("active");
|
|
471
|
+
if (current) {
|
|
472
|
+
current.classList.remove("active");
|
|
473
|
+
current.classList.add("exit");
|
|
474
|
+
current.addEventListener("transitionend", () => current.remove(), { once: true });
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
updateUI(state, text, disabled = false) {
|
|
479
|
+
if (!this.#div) return;
|
|
480
|
+
|
|
481
|
+
this.#div.setAttribute("data-state", state);
|
|
482
|
+
|
|
483
|
+
this.animateLabel(text);
|
|
484
|
+
|
|
485
|
+
if (disabled) {
|
|
486
|
+
this.#div.setAttribute("disabled", "true");
|
|
487
|
+
} else {
|
|
488
|
+
this.#div.removeAttribute("disabled");
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
handleProgress(event) {
|
|
493
|
+
if (!this.#div) return;
|
|
494
|
+
|
|
495
|
+
const progressCircle = this.#div.querySelector(".progress-ring-circle");
|
|
496
|
+
|
|
497
|
+
if (progressCircle) {
|
|
498
|
+
const circumference = 2 * Math.PI * 14;
|
|
499
|
+
const offset = circumference - (event.detail.progress / 100) * circumference;
|
|
500
|
+
progressCircle.style.strokeDashoffset = offset;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const wrapper = this.#div.querySelector(".label-wrapper");
|
|
504
|
+
if (wrapper) {
|
|
505
|
+
const activeLabel = wrapper.querySelector(".label.active");
|
|
506
|
+
if (activeLabel) {
|
|
507
|
+
activeLabel.textContent = `${this.getI18nText("verifying-label", "Verifying...")} ${event.detail.progress}%`;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
this.executeAttributeCode("onprogress", event);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
handleSolve(event) {
|
|
515
|
+
this.updateUI("done", this.getI18nText("solved-label", "You're a human"), true);
|
|
516
|
+
this.executeAttributeCode("onsolve", event);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
handleError(event) {
|
|
520
|
+
this.updateUI("error", this.getI18nText("error-label", "Error. Try again."));
|
|
521
|
+
this.executeAttributeCode("onerror", event);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
handleReset(event) {
|
|
525
|
+
this.updateUI("", this.getI18nText("initial-state", "I'm a human"));
|
|
526
|
+
this.executeAttributeCode("onreset", event);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
executeAttributeCode(attributeName, event) {
|
|
530
|
+
const code = this.getAttribute(attributeName);
|
|
531
|
+
if (!code) {
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
new Function("event", code).call(this, event);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
error(message = "Unknown error") {
|
|
539
|
+
console.error("[cap]", message);
|
|
540
|
+
this.dispatchEvent("error", { isCap: true, message });
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
dispatchEvent(eventName, detail = {}) {
|
|
544
|
+
const event = new CustomEvent(eventName, {
|
|
545
|
+
bubbles: true,
|
|
546
|
+
composed: true,
|
|
547
|
+
detail,
|
|
548
|
+
});
|
|
549
|
+
super.dispatchEvent(event);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
reset() {
|
|
553
|
+
if (this.#resetTimer) {
|
|
554
|
+
clearTimeout(this.#resetTimer);
|
|
555
|
+
this.#resetTimer = null;
|
|
556
|
+
}
|
|
557
|
+
this.dispatchEvent("reset");
|
|
558
|
+
this.token = null;
|
|
559
|
+
const fieldName = this.getAttribute("data-cap-hidden-field-name") || "cap-token";
|
|
560
|
+
if (this.querySelector(`input[name='${fieldName}']`)) {
|
|
561
|
+
this.querySelector(`input[name='${fieldName}']`).value = "";
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
get tokenValue() {
|
|
566
|
+
return this.token;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
disconnectedCallback() {
|
|
570
|
+
this.removeEventListener("progress", this.boundHandleProgress);
|
|
571
|
+
this.removeEventListener("solve", this.boundHandleSolve);
|
|
572
|
+
this.removeEventListener("error", this.boundHandleError);
|
|
573
|
+
this.removeEventListener("reset", this.boundHandleReset);
|
|
574
|
+
|
|
575
|
+
this.#eventHandlers.forEach((handler, eventName) => {
|
|
576
|
+
this.removeEventListener(eventName.slice(2), handler);
|
|
577
|
+
});
|
|
578
|
+
this.#eventHandlers.clear();
|
|
579
|
+
|
|
580
|
+
if (this.#shadow) {
|
|
581
|
+
this.#shadow.innerHTML = "";
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
this.reset();
|
|
585
|
+
this.cleanup();
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
cleanup() {
|
|
589
|
+
if (this.#resetTimer) {
|
|
590
|
+
clearTimeout(this.#resetTimer);
|
|
591
|
+
this.#resetTimer = null;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
if (this.#workerUrl) {
|
|
595
|
+
URL.revokeObjectURL(this.#workerUrl);
|
|
596
|
+
this.#workerUrl = "";
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
class Cap {
|
|
602
|
+
constructor(config = {}, el) {
|
|
603
|
+
const widget = el || document.createElement("cap-widget");
|
|
604
|
+
|
|
605
|
+
Object.entries(config).forEach(([a, b]) => {
|
|
606
|
+
widget.setAttribute(a, b);
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
if (!config.apiEndpoint && !window?.CAP_CUSTOM_FETCH) {
|
|
610
|
+
widget.remove();
|
|
611
|
+
throw new Error(
|
|
612
|
+
"Missing API endpoint. Either custom fetch or an API endpoint must be provided.",
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
if (config.apiEndpoint) {
|
|
617
|
+
widget.setAttribute("data-cap-api-endpoint", config.apiEndpoint);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
this.widget = widget;
|
|
621
|
+
this.solve = this.widget.solve.bind(this.widget);
|
|
622
|
+
this.reset = this.widget.reset.bind(this.widget);
|
|
623
|
+
this.addEventListener = this.widget.addEventListener.bind(this.widget);
|
|
624
|
+
|
|
625
|
+
Object.defineProperty(this, "token", {
|
|
626
|
+
get: () => widget.token,
|
|
627
|
+
configurable: true,
|
|
628
|
+
enumerable: true,
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
if (!el) {
|
|
632
|
+
widget.style.display = "none";
|
|
633
|
+
document.documentElement.appendChild(widget);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
window.Cap = Cap;
|
|
639
|
+
|
|
640
|
+
if (!customElements.get("cap-widget") && !window?.CAP_DONT_SKIP_REDEFINE) {
|
|
641
|
+
customElements.define("cap-widget", CapWidget);
|
|
642
|
+
} else if (customElements.get("cap-widget")) {
|
|
643
|
+
console.warn(
|
|
644
|
+
"[cap] the cap-widget element has already been defined, skipping re-defining it.\nto prevent this, set window.CAP_DONT_SKIP_REDEFINE to true",
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
if (typeof exports === "object" && typeof module !== "undefined") {
|
|
649
|
+
module.exports = Cap;
|
|
650
|
+
} else if (typeof define === "function" && define.amd) {
|
|
651
|
+
define([], () => Cap);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
if (typeof exports !== "undefined") {
|
|
655
|
+
exports.default = Cap;
|
|
656
|
+
}
|
|
613
657
|
})();
|