@cap.js/widget 0.1.6 → 0.1.7

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/cap.js +30 -58
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap.js/widget",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Client-side widget for Cap. Cap is a lightweight, modern open-source CAPTCHA alternative designed using SHA-256 PoW.",
5
5
  "keywords": [
6
6
  "account security",
package/src/cap.js CHANGED
@@ -25,14 +25,7 @@
25
25
  }
26
26
 
27
27
  static get observedAttributes() {
28
- return [
29
- "onsolve",
30
- "onprogress",
31
- "onreset",
32
- "onerror",
33
- "workers",
34
- "[cap]",
35
- ];
28
+ return ["onsolve", "onprogress", "onreset", "onerror", "workers", "[cap]"];
36
29
  }
37
30
 
38
31
  constructor() {
@@ -90,12 +83,9 @@
90
83
 
91
84
  const workers = this.getAttribute("data-cap-worker-count");
92
85
  this.setWorkersCount(
93
- parseInt(workers)
94
- ? parseInt(workers, 10)
95
- : navigator.hardwareConcurrency || 8
86
+ parseInt(workers) ? parseInt(workers, 10) : navigator.hardwareConcurrency || 8
96
87
  );
97
- const fieldName =
98
- this.getAttribute("data-cap-hidden-field-name") || "cap-token";
88
+ const fieldName = this.getAttribute("data-cap-hidden-field-name") || "cap-token";
99
89
  this.#host.innerHTML = `<input type="hidden" name="${fieldName}">`;
100
90
  }
101
91
 
@@ -132,8 +122,7 @@
132
122
  this.dispatchEvent("progress", { progress: 100 });
133
123
 
134
124
  if (!resp.success) throw new Error("Invalid solution");
135
- const fieldName =
136
- this.getAttribute("data-cap-hidden-field-name") || "cap-token";
125
+ const fieldName = this.getAttribute("data-cap-hidden-field-name") || "cap-token";
137
126
  if (this.querySelector(`input[name='${fieldName}']`)) {
138
127
  this.querySelector(`input[name='${fieldName}']`).value = resp.token;
139
128
  }
@@ -192,16 +181,19 @@
192
181
  reject(err);
193
182
  };
194
183
 
195
- worker.postMessage({ salt, target });
184
+ worker.postMessage({
185
+ salt,
186
+ target,
187
+ wasmUrl:
188
+ window.CAP_CUSTOM_WASM_URL ||
189
+ "https://cdn.jsdelivr.net/npm/@cap.js/wasm@0.0.3/browser/cap_wasm.min.js",
190
+ });
196
191
  });
197
192
 
198
193
  const results = [];
199
194
  try {
200
195
  for (let i = 0; i < challenge.length; i += this.#workersCount) {
201
- const chunk = challenge.slice(
202
- i,
203
- Math.min(i + this.#workersCount, challenge.length)
204
- );
196
+ const chunk = challenge.slice(i, Math.min(i + this.#workersCount, challenge.length));
205
197
  const chunkResults = await Promise.all(
206
198
  chunk.map((c, idx) => solveSingleChallenge(c, idx))
207
199
  );
@@ -218,9 +210,7 @@
218
210
  const parsedWorkers = parseInt(workers, 10);
219
211
  const maxWorkers = Math.min(navigator.hardwareConcurrency || 8, 16);
220
212
  this.#workersCount =
221
- !isNaN(parsedWorkers) &&
222
- parsedWorkers > 0 &&
223
- parsedWorkers <= maxWorkers
213
+ !isNaN(parsedWorkers) && parsedWorkers > 0 && parsedWorkers <= maxWorkers
224
214
  ? parsedWorkers
225
215
  : navigator.hardwareConcurrency || 8;
226
216
  }
@@ -230,7 +220,10 @@
230
220
  this.#div.setAttribute("role", "button");
231
221
  this.#div.setAttribute("tabindex", "0");
232
222
  this.#div.setAttribute("disabled", "true");
233
- this.#div.innerHTML = `<div class="checkbox"></div><p>${this.getI18nText("initial-state", "I'm a human")}</p><a href="https://capjs.js.org/" class="credits" target="_blank" rel="follow noopener"><span>Secured by&nbsp;</span>Cap</a>`;
223
+ this.#div.innerHTML = `<div class="checkbox"></div><p>${this.getI18nText(
224
+ "initial-state",
225
+ "I'm a human"
226
+ )}</p><a href="https://capjs.js.org/" class="credits" target="_blank" rel="follow noopener"><span>Secured by&nbsp;</span>Cap</a>`;
234
227
  this.#shadow.innerHTML = `<style>.captcha{background-color:var(--cap-background);border:1px solid var(--cap-border-color);border-radius:var(--cap-border-radius);width:var(--cap-widget-width);display:flex;align-items:center;padding:var(--cap-widget-padding);gap:var(--cap-gap);cursor:pointer;transition:filter var(--cap-transition-duration),transform var(--cap-transition-duration);position:relative;-webkit-tap-highlight-color:rgba(255,255,255,0);overflow:hidden;color:var(--cap-color)}.captcha:hover{filter:var(--cap-hover-filter)}.captcha:not([disabled]):active{transform:scale(var(--cap-active-scale))}.checkbox{width:var(--cap-checkbox-size);height:var(--cap-checkbox-size);border:var(--cap-checkbox-border);border-radius:var(--cap-checkbox-border-radius);background-color:var(--cap-checkbox-background);transition:opacity var(--cap-transition-duration);margin-top:var(--cap-checkbox-margin);margin-bottom:var(--cap-checkbox-margin)}.captcha *{font-family:var(--cap-font)}.captcha p{margin:0;font-weight:500;font-size:15px;user-select:none;transition:opacity var(--cap-transition-duration)}.captcha[data-state=verifying] .checkbox{background: none;display:flex;align-items:center;justify-content:center;transform: scale(1.1);border: none;border-radius: 50%;background: conic-gradient(var(--cap-spinner-color) 0%, var(--cap-spinner-color) var(--progress, 0%), var(--cap-spinner-background-color) var(--progress, 0%), var(--cap-spinner-background-color) 100%);position: relative;}.captcha[data-state=verifying] .checkbox::after {content: "";background-color: var(--cap-background);width: calc(100% - var(--cap-spinner-thickness));height: calc(100% - var(--cap-spinner-thickness));border-radius: 50%;margin:calc(var(--cap-spinner-thickness) / 2)}.captcha[data-state=done] .checkbox{border:1px solid transparent;background-image:var(--cap-checkmark);background-size:cover}.captcha[data-state=error] .checkbox{border:1px solid transparent;background-image:var(--cap-error-cross);background-size:cover}.captcha[disabled]{
235
228
  cursor:not-allowed}.captcha[disabled][data-state=verifying]{cursor:progress}.captcha[disabled][data-state=done]{cursor:default}.captcha .credits{position:absolute;bottom:10px;right:10px;font-size:var(--cap-credits-font-size);color:var(--cap-color);opacity:var(--cap-opacity-hover)}.captcha .credits span{display:none;text-decoration:underline}.captcha .credits:hover span{display:inline-block}</style>`;
236
229
 
@@ -249,10 +242,7 @@
249
242
  });
250
243
 
251
244
  this.#div.addEventListener("keydown", (e) => {
252
- if (
253
- (e.key === "Enter" || e.key === " ") &&
254
- !this.#div.hasAttribute("disabled")
255
- ) {
245
+ if ((e.key === "Enter" || e.key === " ") && !this.#div.hasAttribute("disabled")) {
256
246
  e.preventDefault();
257
247
  this.solve();
258
248
  }
@@ -280,10 +270,9 @@
280
270
  this.#div
281
271
  .querySelector(".checkbox")
282
272
  .style.setProperty("--progress", `${event.detail.progress}%`);
283
- progressElement.innerText = `${this.getI18nText(
284
- "verifying-label",
285
- "Verifying..."
286
- )} ${event.detail.progress}%`;
273
+ progressElement.innerText = `${this.getI18nText("verifying-label", "Verifying...")} ${
274
+ event.detail.progress
275
+ }%`;
287
276
  }
288
277
  this.executeAttributeCode("onprogress", event);
289
278
  }
@@ -333,8 +322,7 @@
333
322
  }
334
323
  this.dispatchEvent("reset");
335
324
  this.token = null;
336
- const fieldName =
337
- this.getAttribute("data-cap-hidden-field-name") || "cap-token";
325
+ const fieldName = this.getAttribute("data-cap-hidden-field-name") || "cap-token";
338
326
  if (this.querySelector(`input[name='${fieldName}']`)) {
339
327
  this.querySelector(`input[name='${fieldName}']`).value = "";
340
328
  }
@@ -417,10 +405,7 @@
417
405
  document.adoptedStyleSheets.push(sheet);
418
406
 
419
407
  const workerFunct = function () {
420
- if (
421
- typeof WebAssembly !== "object" ||
422
- typeof WebAssembly?.instantiate !== "function"
423
- ) {
408
+ if (typeof WebAssembly !== "object" || typeof WebAssembly?.instantiate !== "function") {
424
409
  self.onmessage = async ({ data: { salt, target } }) => {
425
410
  // Fallback solver in case WASM is not available
426
411
 
@@ -441,16 +426,9 @@
441
426
  const inputString = salt + nonce;
442
427
  const inputBytes = encoder.encode(inputString);
443
428
 
444
- const hashBuffer = await crypto.subtle.digest(
445
- "SHA-256",
446
- inputBytes
447
- );
429
+ const hashBuffer = await crypto.subtle.digest("SHA-256", inputBytes);
448
430
 
449
- const hashBytes = new Uint8Array(
450
- hashBuffer,
451
- 0,
452
- targetBytesLength
453
- );
431
+ const hashBytes = new Uint8Array(hashBuffer, 0, targetBytesLength);
454
432
 
455
433
  let matches = true;
456
434
  for (let k = 0; k < targetBytesLength; k++) {
@@ -487,14 +465,11 @@
487
465
 
488
466
  let initPromise, solve_pow_function;
489
467
 
490
- initPromise = import(
491
- window.CAP_CUSTOM_WASM_URL || "https://cdn.jsdelivr.net/npm/@cap.js/wasm@0.0.3/browser/cap_wasm.min.js"
492
- )
468
+ initPromise = import("https://cdn.jsdelivr.net/npm/@cap.js/wasm@0.0.3/browser/cap_wasm.min.js")
493
469
  .then((wasmModule) => {
494
470
  return wasmModule.default().then((instance) => {
495
- solve_pow_function = (
496
- instance && instance.exports ? instance.exports : wasmModule
497
- ).solve_pow;
471
+ solve_pow_function = (instance && instance.exports ? instance.exports : wasmModule)
472
+ .solve_pow;
498
473
  });
499
474
  })
500
475
  .catch((e) => {
@@ -504,8 +479,7 @@
504
479
 
505
480
  self.onmessage = async ({ data: { salt, target } }) => {
506
481
  const until = (predFn) => {
507
- const poll = (done) =>
508
- predFn() ? done() : setTimeout(() => poll(done), 500);
482
+ const poll = (done) => (predFn() ? done() : setTimeout(() => poll(done), 500));
509
483
  return new Promise(poll);
510
484
  };
511
485
 
@@ -549,9 +523,7 @@
549
523
  if (!customElements.get("cap-widget")) {
550
524
  customElements.define("cap-widget", CapWidget);
551
525
  } else {
552
- console.warn(
553
- "The cap-widget element has already been defined. Skipping re-defining it."
554
- );
526
+ console.warn("The cap-widget element has already been defined. Skipping re-defining it.");
555
527
  }
556
528
 
557
529
  if (typeof exports === "object" && typeof module !== "undefined") {