Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ on the target page.
| Image to Text | ✅ | — |
| Yandex SmartCaptcha | ✅ | ✅ |
| Yandex SmartCaptcha Image | ✅ | — |
| Yandex SmartCaptcha Kaleidoscope | ✅ | — |
| Coordinates (click captcha) | ✅ | — |
| Tencent | ✅ | ✅ |

Expand Down Expand Up @@ -551,6 +552,37 @@ captchaSolver.solve(task)
See the runnable example in
[examples/yandex_smartcaptcha_image.js](examples/yandex_smartcaptcha_image.js).

#### Kaleidoscope puzzle

Use `Tasks.YandexSmartCaptchaKaleidoscopeTask` for the image-based Kaleidoscope
puzzle. Encode the 320×180 captcha image as base64 and pass the puzzle steps as
a JSON array string. The SDK sends API v2 type `PazlCaptchaTask`.

| Field | Required | Description |
|---|---|---|
| `image` | yes | Main captcha image, base64-encoded. |
| `task` | yes | Puzzle steps serialized as a JSON array string, for example `"[15,24,4,13,10]"`. |

The solution is a string containing the number of slider-arrow clicks required
to solve the captcha.

```javascript
import fs from 'node:fs';
import { CaptchaClient, Tasks } from '@captcha-solver-api/javascript-sdk';

const captchaSolver = new CaptchaClient({ clientKey: 'your_api_key' });
const task = new Tasks.YandexSmartCaptchaKaleidoscopeTask({
image: fs.readFileSync('examples/assets/yandex-smartcaptcha-kaleidoscope.jpg').toString('base64'),
task: '[15,24,4,13,10]'
});
captchaSolver.solve(task)
.then((clicks) => console.log(clicks))
.catch(console.error);
```

See the runnable example in
[examples/yandex_smartcaptcha_kaleidoscope.js](examples/yandex_smartcaptcha_kaleidoscope.js).

### Coordinates (click captcha)

<sup>[API method description.](https://captcha-solver.com/en/docs/captcha-types#coordinates)</sup>
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ proxy credentials before running token CAPTCHA examples.
| GeeTest v4 | [`geetest_v4.js`](geetest_v4.js) | [`geetest_v4_proxy.js`](geetest_v4_proxy.js) |
| Yandex SmartCaptcha | [`yandex_smartcaptcha.js`](yandex_smartcaptcha.js) | [`yandex_smartcaptcha_proxy.js`](yandex_smartcaptcha_proxy.js) |
| Yandex SmartCaptcha Image | [`yandex_smartcaptcha_image.js`](yandex_smartcaptcha_image.js) | No separate proxy task |
| Yandex SmartCaptcha Kaleidoscope | [`yandex_smartcaptcha_kaleidoscope.js`](yandex_smartcaptcha_kaleidoscope.js) | No separate proxy task |
| Tencent | [`tencent.js`](tencent.js) | [`tencent_proxy.js`](tencent_proxy.js) |
| Image to Text | [`image_to_text.js`](image_to_text.js) | No separate proxy task |
| Coordinates | [`coordinates.js`](coordinates.js) | No separate proxy task |
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions examples/yandex_smartcaptcha_kaleidoscope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dotenv/config';
import fs from 'node:fs';
import { CaptchaClient, Tasks } from '@captcha-solver-api/javascript-sdk';

if (!process.env.CAPTCHA_API_KEY) {
throw new Error('Set CAPTCHA_API_KEY in .env or in the current shell before running this example.');
}

const captchaSolver = new CaptchaClient({
clientKey: process.env.CAPTCHA_API_KEY
});
const image = fs.readFileSync(
new URL('./assets/yandex-smartcaptcha-kaleidoscope.jpg', import.meta.url)
).toString('base64');

const task = new Tasks.YandexSmartCaptchaKaleidoscopeTask({
image,
task: '[7,15,14,0,11,6,6,0,11,15,6,10,1,14,3,12,9,0,15,1,5,1,8,11,12,11,0,12,1,10,1,4,14,6,10,6,10,8,3,0,5,9,7,8,0,3,8,0,9,12,12,11,3,0,8,10]'
});

captchaSolver.solve(task)
.then((clicks) => {
console.log('Captcha solved. Slider arrow clicks:', clicks);
})
.catch(console.error);
6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export interface TaskResult {
errorId: number;
/** Task status: `'processing'` while a worker is on it, `'ready'` once solved. */
status?: 'processing' | 'ready';
/** The solution object. Present only when `status` is `'ready'`; its shape depends on the task type. */
solution?: Record<string, unknown>;
/** The solution. Present only when `status` is `'ready'`; its shape depends on the task type. */
solution?: unknown;
/** Any other fields the API returns for this endpoint (e.g. `taskId`, `balance`, `cost`). */
[key: string]: unknown;
}
Expand Down Expand Up @@ -195,7 +195,7 @@ export class CaptchaClient {
const remaining = this.timeout - (Date.now() - startedAt); if (remaining <= 0) throw new TimeoutError(`Task ${taskId} timed out after ${this.timeout}ms`);
await new Promise(resolve => setTimeout(resolve, Math.min(this.pollingInterval, remaining)));
if (Date.now() - startedAt >= this.timeout) throw new TimeoutError(`Task ${taskId} timed out after ${this.timeout}ms`);
const result = await this.getTaskResult(taskId); if (result.status === 'ready') return (result.solution || {}) as SolutionOf<T>;
const result = await this.getTaskResult(taskId); if (result.status === 'ready') return (result.solution ?? {}) as SolutionOf<T>;
}
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type {
YandexSmartCaptchaTaskParams,
CoordinatesTaskParams,
YandexSmartCaptchaImageTaskParams,
YandexSmartCaptchaKaleidoscopeTaskParams,
TencentTaskProxylessParams,
TencentTaskParams
} from './tasks.js';
Expand Down
30 changes: 29 additions & 1 deletion src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const REQUIRED_TASK_PARAMS: Record<string, readonly string[]> = {
YandexSmartCaptchaTask: ['websiteURL', 'websiteKey', 'proxyType', 'proxyAddress', 'proxyPort'],
CoordinatesTask: ['body'],
SmartCaptchaTask: ['image', 'imgInstructions'],
PazlCaptchaTask: ['image', 'task'],
TencentTaskProxyless: ['websiteURL', 'appId'],
TencentTask: ['websiteURL', 'appId', 'proxyType', 'proxyAddress', 'proxyPort']
};
Expand Down Expand Up @@ -167,7 +168,7 @@ export interface TencentSolution {
* @typeParam TSolution - Shape of the `solution` object returned for this
* task by `CaptchaClient.solve()`.
*/
export class BaseTask<TSolution extends Record<string, unknown> = Record<string, unknown>> {
export class BaseTask<TSolution = Record<string, unknown>> {
/**
* Type-only marker that carries the solution shape to `CaptchaClient.solve()`.
* Never set at runtime.
Expand Down Expand Up @@ -654,6 +655,33 @@ export class YandexSmartCaptchaImageTask extends BaseTask<CoordinatesSolution> {
}
}

/* ------------------------------------------------------------------ */
/* Yandex SmartCaptcha kaleidoscope */
/* ------------------------------------------------------------------ */

/** Parameters of {@link YandexSmartCaptchaKaleidoscopeTask}. */
export interface YandexSmartCaptchaKaleidoscopeTaskParams {
/** The main Kaleidoscope challenge image, base64-encoded. */
image: string;
/** Puzzle steps serialized as a JSON array string. */
task: string;
}

/**
* Image-based Yandex SmartCaptcha Kaleidoscope puzzle.
*
* Serialized as the API v2 `PazlCaptchaTask`. The solution is a string
* containing the number of slider-arrow clicks required to solve the puzzle.
* Solution: `string`.
*
* @see https://captcha-solver.com/en/docs/captcha-types#yandex-smartcaptcha
*/
export class YandexSmartCaptchaKaleidoscopeTask extends BaseTask<string> {
constructor({ image, task }: YandexSmartCaptchaKaleidoscopeTaskParams = {} as YandexSmartCaptchaKaleidoscopeTaskParams) {
super({ type: 'PazlCaptchaTask', image, task });
}
}

/* ------------------------------------------------------------------ */
/* Tencent */
/* ------------------------------------------------------------------ */
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/async/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ describe('CaptchaClient (async)', () => {
expect(callCount).toBe(2);
});

test('solve returns a string solution for a Kaleidoscope task', async () => {
global.fetch = jest.fn()
.mockImplementationOnce(() => Promise.resolve({
ok: true,
json: () => Promise.resolve({ errorId: 0, taskId: 12345 })
}))
.mockImplementationOnce(() => Promise.resolve({
ok: true,
json: () => Promise.resolve({ errorId: 0, status: 'ready', solution: '8' })
}));

const task = new Tasks.YandexSmartCaptchaKaleidoscopeTask({
image: 'captcha-image',
task: '[7,15,14,0]'
});

await expect(client.solve(task)).resolves.toBe('8');
});

test('throws TimeoutError when timeout exceeded', async () => {
client.timeout = 100;
client.pollingInterval = 200;
Expand Down
1 change: 1 addition & 0 deletions tests/unit/public-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const TASK_CLASSES = [
'YandexSmartCaptchaTask',
'CoordinatesTask',
'YandexSmartCaptchaImageTask',
'YandexSmartCaptchaKaleidoscopeTask',
'TencentTaskProxyless',
'TencentTask'
];
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/task-validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ describe('built-in task validation', () => {
expect(() => new Tasks.YandexSmartCaptchaImageTask()).toThrow(
'SmartCaptchaTask requires: image, imgInstructions'
);
expect(() => new Tasks.YandexSmartCaptchaKaleidoscopeTask()).toThrow(
'PazlCaptchaTask requires: image, task'
);
expect(() => new Tasks.RecaptchaV2Proxyless()).toThrow(
'RecaptchaV2TaskProxyless requires: websiteURL, websiteKey'
);
Expand All @@ -22,6 +25,7 @@ describe('built-in task validation', () => {
['CoordinatesTask', { body: ' ' }, 'body'],
['YandexSmartCaptchaImageTask', {}, 'image, imgInstructions'],
['YandexSmartCaptchaImageTask', { image: 'image', imgInstructions: '' }, 'imgInstructions'],
['YandexSmartCaptchaKaleidoscopeTask', { image: 'image', task: '' }, 'task'],
['TencentTaskProxyless', { websiteURL: 'https://example.com' }, 'appId'],
['RecaptchaV2Enterprise', { websiteURL: 'https://example.com', websiteKey: 'key' }, 'proxyType, proxyAddress, proxyPort'],
['Turnstile', { websiteURL: 'https://example.com', websiteKey: 'key', proxyType: 'http' }, 'proxyAddress, proxyPort'],
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/yandex_smartcaptcha_kaleidoscope.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Tasks } from '@captcha-solver-api/javascript-sdk';

describe('YandexSmartCaptchaKaleidoscopeTask', () => {
test('serializes as an API v2 PazlCaptchaTask', () => {
const task = new Tasks.YandexSmartCaptchaKaleidoscopeTask({
image: 'captcha-image',
task: '[7,15,14,0]'
});

expect(task.toDict()).toEqual({
type: 'PazlCaptchaTask',
image: 'captcha-image',
task: '[7,15,14,0]'
});
});
});