add custom fetch and docs improvements

This commit is contained in:
Tiago
2025-04-22 13:42:58 +01:00
parent 4dd0ef3a13
commit 1ccad71f43
8 changed files with 120 additions and 6 deletions
+2
View File
@@ -108,6 +108,7 @@ export default defineConfig({
{ text: "Quickstart", link: "/guide/index.md" },
{ text: "Effectiveness", link: "/guide/effectiveness.md" },
{ text: "Alternatives", link: "/guide/alternatives.md" },
{ text: "Philosophy", link: "/guide/philosophy.md" },
{
text: "Modes",
items: [
@@ -124,6 +125,7 @@ export default defineConfig({
{ text: "@cap.js/solver", link: "/guide/solver.md" },
],
},
{ text: "Benchmark", link: "/guide/benchmark.md" },
{ text: "Demo", link: "https://cap-starter.glitch.me/" },
],
+80
View File
@@ -0,0 +1,80 @@
# Benchmark
Coming soon!
<!--
Press the button below to run a simple benchmark.
<style>
.benchmark-form {
display: flex;
flex-direction: column;
gap: 1rem;
max-width: 300px;
padding: 16px;
border-radius: 12px;
border: 1px solid var(--vp-c-divider);
}
.benchmark-field {
display: flex;
flex-direction: column;
}
.benchmark-field label {
margin: 0;
font-size: 0.9rem;
margin-bottom: 0.2rem;
opacity: .9;
}
.benchmark-field input {
padding: 0.5rem .8rem;
border-radius: 6px;
background-color: var(--vp-sidebar-bg-color);
border: 1px solid var(--vp-c-divider);
font-size: 16px;
font-family: inherit;
}
.benchmark-field input:focus {
border-color: var(--vp-c-brand-1);
outline: none;
}
.benchmark-form button {
background-color: var(--vp-c-brand-1);
font-size: 14px;
font-weight: 500;
color: white;
padding: 0.5rem 1rem;
border-radius: 6px;
transition: background-color 0.2s ease;
}
.benchmark-form button:hover {
background-color: var(--vp-c-brand-2);
}
</style>
<div class="benchmark-form">
<div class="benchmark-field">
<label for="difficulty">Challenge difficulty</label>
<input type="number" id="difficulty" name="difficulty" min="1" max="10" value="4">
</div>
<div class="benchmark-field">
<label for="challenges">Number of challenges</label>
<input type="number" id="challenges" name="challenges" min="1" max="100" value="5">
</div>
<div class="benchmark-field">
<label for="workers">Number of workers</label>
<input type="number" id="workers" name="workers" min="1" max="28" value="8">
</div>
<button>Run</button>
</div>
<script>
window.window.CAP_CUSTOM_FETCH = function (a, b) {
console.log({ a, b});
return fetch(a, b);
}
console.log("hi");
</script>
-->
+15
View File
@@ -0,0 +1,15 @@
# Philosophy
Most CAPTCHA systems today are built around surveillance — tracking users, fingerprinting browsers, and collecting data in exchange for access. Cap rejects that model.
Cap is designed around a few core ideas:
- **Privacy by default** — no trackers, no third-party requests, no hidden profiling.
- **Simplicity over complexity** — no image labeling, no click-the-fire-hydrant puzzles.
- **Speed matters** — fast to render, fast to solve, fast to validate.
- **User-first** — designed to be unintrusive and accessible, with invisible and floating modes.
- **Self-hostable, not centralized** — you control the infra, tokens, and behavior.
By using proof-of-work instead of user behavior analysis, Cap shifts the burden from identity to computation. That means anyone can prove theyre human — no cookies, no accounts, no friction.
Cap is what a CAPTCHA shouldve been all along: **simple, honest, and yours.**
+10
View File
@@ -59,3 +59,13 @@ The following custom events are supported:
- `error`: Triggered when an error occurs.
- `reset`: Triggered when the widget is reset.
- `progress`: Triggered when the there's a progress update while in verification.
## Custom fetch
You can override the default fetch implementation by setting `window.CAP_CUSTOM_FETCH` to a custom function. This function will receive the URL and options as arguments and should return a promise that resolves to the response.
```js
window.CAP_CUSTOM_FETCH = function (url, options) {
// Custom fetch implementation
return fetch(url, options);
};
```
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@cap.js/widget",
"version": "0.0.21",
"version": "0.0.22",
"description": "Client-side widget for Cap. Cap is a lightweight, modern open-source CAPTCHA alternative designed using SHA-256 PoW.",
"keywords": [
"account security",
+10 -3
View File
@@ -1,6 +1,13 @@
(function () {
let workerScript;
const capFetch = function () {
if (window?.CAP_CUSTOM_FETCH) {
return window.CAP_CUSTOM_FETCH(...arguments);
}
return fetch(...arguments);
};
const until = (predFn, timeout = 10000) => {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
@@ -124,14 +131,14 @@
if (!apiEndpoint) throw new Error("Missing API endpoint");
const { challenge, token } = await (
await fetch(`${apiEndpoint}challenge`, {
await capFetch(`${apiEndpoint}challenge`, {
method: "POST",
})
).json();
const solutions = await this.solveChallenges(challenge);
const resp = await (
await fetch(`${apiEndpoint}redeem`, {
await capFetch(`${apiEndpoint}redeem`, {
method: "POST",
body: JSON.stringify({ token, solutions }),
headers: { "Content-Type": "application/json" },
@@ -527,7 +534,7 @@
setTimeout(async function () {
workerScript =
(await (
await fetch(
await capFetch(
"https://cdn.jsdelivr.net/npm/@cap.js/widget/wasm-hashes.min.js"
)
).text()) +