Files
cap/docs/guide/widget.md
T

96 lines
3.3 KiB
Markdown
Raw Normal View History

2025-01-18 13:46:25 +00:00
# @cap.js/widget
2025-04-12 13:39:56 +01:00
> [!NOTE]
2025-03-04 11:30:35 +00:00
> **Requirements:** All modern browsers should be supported, but the build script specifically targets the last 10 versions of Chrome, Firefox, Safari and Edge.
2025-04-22 14:41:19 +01:00
`@cap.js/widget` is Cap's client-side library. It includes the `cap-widget` web component, the invisible mode and the Captcha solver. First, add it to your client-side code:
2025-01-18 13:46:25 +00:00
2025-04-22 14:41:19 +01:00
::: code-group
```html [jsdelivr]
2025-01-18 13:46:25 +00:00
<script src="https://cdn.jsdelivr.net/npm/@cap.js/widget"></script>
```
2025-04-22 14:41:19 +01:00
```html [unpkg]
<script src="https://unpkg.com/@cap.js/widget"></script>
```
2025-05-07 19:30:12 +01:00
```html [standalone server]
<script src="https://<server url>/assets/widget.js"></script>
```
2025-04-22 14:41:19 +01:00
:::
2025-05-21 16:36:02 +01:00
::: warning
We're using the latest version of the library here for keeping stuff simple, but you should optimally pin a specific version to avoid breaking changes in the future.
:::
2025-01-18 13:46:25 +00:00
You can now use the `<cap-widget>` component in your HTML.
```html
2025-05-21 16:36:02 +01:00
<cap-widget
id="cap"
data-cap-api-endpoint="<your cap api endpoint>"
></cap-widget>
2025-01-18 13:46:25 +00:00
```
2025-05-09 14:45:13 +01:00
> [!NOTE] You'll need to start a server with the Cap API running at the same URL as specified in the `data-cap-api-endpoint` attribute. In the server-side example we provided, it's set to `/api`, but you can change this by replacing every `app.post('/api/...', ...)` to `app.post('/your endpoint/...', ...)`.
2025-01-18 13:46:25 +00:00
2025-05-07 19:30:12 +01:00
> [!TIP] The following attributes are supported:
2025-04-12 13:39:56 +01:00
>
> - `data-cap-api-endpoint`: API endpoint (required)
> - `data-cap-worker-count`: Number of workers to use (defaults to `navigator.hardwareConcurrency || 8`)
2025-01-18 13:46:25 +00:00
Then, in your JavaScript, listen for the `solve` event to capture the token when generated:
```js{3}
const widget = document.querySelector("#cap");
2025-04-12 13:39:56 +01:00
widget.addEventListener("solve", function (e) {
2025-01-18 13:46:25 +00:00
const token = e.detail.token;
2025-04-12 13:39:56 +01:00
2025-01-18 13:46:25 +00:00
// Handle the token as needed
});
```
2025-04-23 14:52:50 +01:00
Alternatively, you can use `onsolve=""` directly within the widget or wrap the widget in a `<form></form>` (where Cap will automatically submit the token alongside other form data. for this, it'll create a hidden field with name set to its `data-cap-hidden-field-name` attribute or `cap-token`).
2025-01-18 13:46:25 +00:00
## Supported events
2025-04-12 13:39:56 +01:00
The following custom events are supported:
2025-01-18 13:46:25 +00:00
- `solve`: Triggered when the token is generated.
- `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.
2025-04-22 13:42:58 +01:00
2025-05-07 19:30:12 +01:00
## i18n
You can change the text on each label of the widget by setting the `data-cap-i18n-*` attribute, like this:
```html
<cap-widget
id="cap"
data-cap-api-endpoint="<your cap api endpoint>"
data-cap-i18n-verifying-label="Verifying..."
data-cap-i18n-initial-state="I'm a human"
data-cap-i18n-solved-label="I'm a human"
data-cap-i18n-error-label="Error"
></cap-widget>
```
2025-04-22 13:42:58 +01:00
## Custom fetch
2025-05-07 19:30:12 +01:00
2025-04-22 13:42:58 +01:00
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);
};
2025-05-07 19:30:12 +01:00
```
2025-05-09 14:45:13 +01:00
## Custom WASM URL
2025-05-21 16:36:02 +01:00
You can override the default WASM URL by setting `window.CAP_CUSTOM_WASM_URL` to a custom URL. This URL will be used to load the WASM module. This defaults to `https://cdn.jsdelivr.net/npm/@cap.js/wasm@0.0.3/browser/cap_wasm.min.js`