--- outline: [2, 3] description: "Cap's client widget requests, solves and displays proof-of-work challenges with a native web component and WASM. Install it, use it in any framework, and customize it." --- # Widget The widget is a `` web component. It fetches a challenge from your server, solves it in Web Workers with WebAssembly, and gives you a token to verify. ## Install ::: code-group ```sh [npm] npm i cap-widget ``` ```sh [pnpm] pnpm add cap-widget ``` ```sh [bun] bun add cap-widget ``` ```html [cdn] ``` ::: Pin a version from the [releases page](https://github.com/tiagozip/cap/releases) when you use the CDN. jsDelivr is blocked in parts of China, so if your visitors are there, install from npm or [self-host the widget](#self-hosting-the-widget). ## Usage Point `data-cap-api-endpoint` at your server. For capd, that's `https:////`. ::: code-group ```html [HTML] ``` ```jsx [React] import "cap-widget"; export default function Form() { return ( console.log(e.detail.token)} /> ); } ``` ```vue [Vue] ``` ```svelte [Svelte] console.log(e.detail.token)} > ``` ```jsx [Solid] import "cap-widget"; export default function Form() { return ( console.log(e.detail.token)} /> ); } ``` ```tsx [Qwik] import { component$ } from "@builder.io/qwik"; import "cap-widget"; export default component$(() => ( console.log(e.detail.token)} /> )); ``` ```astro [Astro] ``` ::: A few framework notes: - React handles custom element events properly from version 19. Preact uses the same syntax. - Vue may warn about an unknown component. Tell its compiler that `cap-` tags are custom elements: ```js vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.startsWith("cap-") } } }) ``` - The Svelte example is Svelte 5. On Svelte 4, use `on:solve`. ### In a form Inside a `
`, the widget adds a hidden `cap-token` input when it's solved, so the token is submitted with the other fields. Add `required` to stop the form from submitting before then. ```html
``` To solve without showing a widget, use [invisible mode](./invisible.md). To show it only when a button is pressed, use [floating mode](./floating.md). ## When it fetches and solves The widget doesn't wait for a click to start working. Once it's visible on the page, it fetches a challenge and starts the [instrumentation](./instrumentation.md) frame, which watches the page until the solve finishes. It fetches again a while before the challenge expires, using the challenge's lifetime from its token. For a 10 minute challenge, that's 90 seconds before expiry for SHA-256 and 3 minutes before for HashWX. The first mouse move, touch or key press on the page starts a speculative solve. For a [format 2](./capjs-core.md#format-2-hashwx) challenge, which HashWX uses, the widget only downloads the HashWX module ahead of time. For a format 1 SHA-256 challenge, it waits 2.5 seconds and then solves in a single worker, and a click on the widget moves the rest of the solve onto all its workers. When the key doesn't need interaction data, the widget also redeems the solution in the background and keeps the token, so the click on the widget returns at once. When the key checks interactions or scores behavior, it waits for the click to redeem, because the click is part of what the server checks. If the server rejected a background redeem, the click shows that error. ## Events Each event is a `CustomEvent`. | Event | Fires when | `detail` | | --- | --- | --- | | `solve` | The challenge is solved | `{ token }` | | `progress` | Solving progresses | `{ progress }`, from 0 to 100 | | `error` | Something fails | `{ isCap, code, message }`. The codes are listed in [troubleshooting](./error-codes.md). | | `reset` | The widget resets | `{}` | ## Attributes | Attribute | What it does | | --- | --- | | `data-cap-api-endpoint` | Your server's endpoint. Required. | | `data-cap-worker-count` | Number of solver workers. Defaults to the number of CPU cores, or 8. | | `data-cap-hidden-field-name` | Name of the hidden form input. Defaults to `cap-token`. | | `data-cap-lang` | Forces a [language](#languages). | | `data-cap-troubleshooting-url` | Where the troubleshooting link points when a visitor is blocked. | | `data-cap-disable-haptics` | Turns off vibration on phones. | | `required` | Blocks form submission until the widget is solved. | ## Languages The widget follows the browser's language for Arabic, Chinese (Simplified), French, German, Hindi, Japanese, Portuguese, Russian and Spanish, and uses English otherwise. Set `data-cap-lang="de"` on a widget, or `window.CAP_LANG = "de"` for the whole page, to choose one yourself. To change any label, set its `data-cap-i18n-*` attribute: ```html ``` The keys are `initial-state`, `verifying-label`, `solved-label`, `error-label`, `csp-error-label`, `required-label`, `troubleshooting-label`, `wasm-disabled`, and the screen reader labels `group-aria-label`, `verify-aria-label`, `verifying-aria-label`, `verified-aria-label` and `error-aria-label`. ## Styling Override these CSS variables on `cap-widget`. The values shown are the defaults. ```css cap-widget { --cap-background: #fdfdfd; --cap-border-color: #dddddd8f; --cap-border-radius: 14px; --cap-widget-height: 30px; --cap-widget-width: 230px; --cap-widget-padding: 14px; --cap-gap: 15px; --cap-color: #212121; --cap-checkbox-size: 25px; --cap-checkbox-border: 1px solid #aaaaaad1; --cap-checkbox-border-radius: 6px; --cap-checkbox-background: #fafafa91; --cap-checkbox-margin: 2px; --cap-font: system-ui, -apple-system, sans-serif; --cap-spinner-color: #000; --cap-spinner-background-color: #eee; --cap-spinner-thickness: 5px; } ``` ## Page-wide options {#options} Set these on `window` before the widget loads. | Option | What it does | | --- | --- | | `CAP_CUSTOM_FETCH` | Replaces `fetch` for the widget's requests. | | `CAP_CSS_NONCE` | Nonce for the injected `