Files

62 lines
1.4 KiB
Markdown
Raw Permalink Normal View History

2026-03-02 20:06:39 +00:00
# Programmatic mode
2025-01-18 13:46:25 +00:00
2025-12-21 13:28:54 +00:00
You can use `new Cap({ ... })` in your client-side JavaScript to create a new Cap instance and use the `solve()` method to solve the challenge.
2025-01-18 13:46:25 +00:00
```js
const cap = new Cap({
apiEndpoint: "/api/",
2025-01-18 13:46:25 +00:00
});
2025-04-12 13:39:56 +01:00
const solution = await cap.solve();
console.log(solution.token);
2025-01-18 13:46:25 +00:00
```
2025-04-12 13:39:56 +01:00
You can also set up [event listeners](widget.md#supported-events):
```js
const cap = new Cap({
apiEndpoint: "/api/",
2025-04-12 13:39:56 +01:00
});
cap.addEventListener("progress", (event) => {
console.log(`Solving... ${event.detail.progress}% done`);
});
2025-04-12 13:39:56 +01:00
```
Behind the scenes, Cap creates a hidden `cap-widget` element and uses it to solve the challenge.
2025-01-18 13:46:25 +00:00
## Supported methods and arguments
The following methods are supported:
#### `new Cap({ ... })`
2025-01-18 14:36:36 +00:00
Creates a new Cap instance. If a 2nd argument is provided, it will use that element instead of creating a new one in memory.
2025-01-18 13:46:25 +00:00
**Arguments**
2025-01-18 13:46:25 +00:00
```json
{
2025-05-30 17:52:07 +01:00
apiEndpoint: ..., // api endpoint, similar to the widget `data-cap-api-endpoint` attribute
2025-01-18 13:46:25 +00:00
workers: navigator.hardwareConcurrency || 8 // number of worker threads to use
}
```
#### `cap.solve()`
2025-01-18 13:46:25 +00:00
Requests and solves a challenge.
**Output:** `{ token }`
#### `cap.token`
2025-01-18 13:46:25 +00:00
Returns the token from the latest solve
#### `cap.reset()`
2025-01-18 13:46:25 +00:00
Resets `cap.token`
#### `cap.addEventListener(..., function () { ... })`
Listens for an event for the cap widget. See [supported events](widget.md#supported-events)