diff --git a/README.md b/README.md index 6e1bff9..cbeed12 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ on the target page. | Image to Text | ✅ | — | | Yandex SmartCaptcha | ✅ | ✅ | | Yandex SmartCaptcha Image | ✅ | — | +| Yandex SmartCaptcha Kaleidoscope | ✅ | — | | Coordinates (click captcha) | ✅ | — | | Tencent | ✅ | ✅ | @@ -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) [API method description.](https://captcha-solver.com/en/docs/captcha-types#coordinates) diff --git a/examples/README.md b/examples/README.md index fa2f97f..21d5b14 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 | diff --git a/examples/assets/yandex-smartcaptcha-kaleidoscope.jpg b/examples/assets/yandex-smartcaptcha-kaleidoscope.jpg new file mode 100644 index 0000000..b266962 Binary files /dev/null and b/examples/assets/yandex-smartcaptcha-kaleidoscope.jpg differ diff --git a/examples/yandex_smartcaptcha_kaleidoscope.js b/examples/yandex_smartcaptcha_kaleidoscope.js new file mode 100644 index 0000000..41ef64d --- /dev/null +++ b/examples/yandex_smartcaptcha_kaleidoscope.js @@ -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); diff --git a/src/client.ts b/src/client.ts index 537af5e..3039fe1 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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; + /** 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; } @@ -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; + const result = await this.getTaskResult(taskId); if (result.status === 'ready') return (result.solution ?? {}) as SolutionOf; } } } diff --git a/src/index.ts b/src/index.ts index 320b475..6cea1fb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,6 +31,7 @@ export type { YandexSmartCaptchaTaskParams, CoordinatesTaskParams, YandexSmartCaptchaImageTaskParams, + YandexSmartCaptchaKaleidoscopeTaskParams, TencentTaskProxylessParams, TencentTaskParams } from './tasks.js'; diff --git a/src/tasks.ts b/src/tasks.ts index 0740c7b..12225cc 100644 --- a/src/tasks.ts +++ b/src/tasks.ts @@ -15,6 +15,7 @@ const REQUIRED_TASK_PARAMS: Record = { YandexSmartCaptchaTask: ['websiteURL', 'websiteKey', 'proxyType', 'proxyAddress', 'proxyPort'], CoordinatesTask: ['body'], SmartCaptchaTask: ['image', 'imgInstructions'], + PazlCaptchaTask: ['image', 'task'], TencentTaskProxyless: ['websiteURL', 'appId'], TencentTask: ['websiteURL', 'appId', 'proxyType', 'proxyAddress', 'proxyPort'] }; @@ -167,7 +168,7 @@ export interface TencentSolution { * @typeParam TSolution - Shape of the `solution` object returned for this * task by `CaptchaClient.solve()`. */ -export class BaseTask = Record> { +export class BaseTask> { /** * Type-only marker that carries the solution shape to `CaptchaClient.solve()`. * Never set at runtime. @@ -654,6 +655,33 @@ export class YandexSmartCaptchaImageTask extends BaseTask { } } +/* ------------------------------------------------------------------ */ +/* 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 { + constructor({ image, task }: YandexSmartCaptchaKaleidoscopeTaskParams = {} as YandexSmartCaptchaKaleidoscopeTaskParams) { + super({ type: 'PazlCaptchaTask', image, task }); + } +} + /* ------------------------------------------------------------------ */ /* Tencent */ /* ------------------------------------------------------------------ */ diff --git a/tests/unit/async/client.test.js b/tests/unit/async/client.test.js index ade52a9..a612f53 100644 --- a/tests/unit/async/client.test.js +++ b/tests/unit/async/client.test.js @@ -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; diff --git a/tests/unit/public-api.test.js b/tests/unit/public-api.test.js index 49deee5..48bb22e 100644 --- a/tests/unit/public-api.test.js +++ b/tests/unit/public-api.test.js @@ -32,6 +32,7 @@ const TASK_CLASSES = [ 'YandexSmartCaptchaTask', 'CoordinatesTask', 'YandexSmartCaptchaImageTask', + 'YandexSmartCaptchaKaleidoscopeTask', 'TencentTaskProxyless', 'TencentTask' ]; diff --git a/tests/unit/task-validation.test.js b/tests/unit/task-validation.test.js index 0422617..d857b28 100644 --- a/tests/unit/task-validation.test.js +++ b/tests/unit/task-validation.test.js @@ -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' ); @@ -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'], diff --git a/tests/unit/yandex_smartcaptcha_kaleidoscope.test.js b/tests/unit/yandex_smartcaptcha_kaleidoscope.test.js new file mode 100644 index 0000000..0e0eb4a --- /dev/null +++ b/tests/unit/yandex_smartcaptcha_kaleidoscope.test.js @@ -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]' + }); + }); +});