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
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Examples of API requests for different captcha types are available on the [JavaS
- [TSPD](#tspd)
- [Basilisk](#basilisk)
- [Hunt](#hunt)
- [Drag and Drop](#drag-and-drop)
- [Other methods](#other-methods)
- [goodReport](#goodreport)
- [badReport](#badreport)
Expand Down Expand Up @@ -955,6 +956,30 @@ console.log(err);
})
```

### Drag and Drop

<sup>[API method description.](https://2captcha.com/2captcha-api#drag-and-drop-captcha)</sup>

A method for solving captcha where one or more images need to be dragged onto specific positions on a background image.

Required parameters: `body`, `images`.

The result (`data`) is a pipe-separated string of coordinates, for example `"120,340|null|210,90"`. The order matches the order of the `images` array you sent. `null` means the corresponding image wasn't moved — it does **not** mean an error or coordinates `(0,0)`.

```js
solver.dragAndDrop({
body: "BASE64_BACKGROUND_IMAGE",
images: ["BASE64_IMAGE_1", "BASE64_IMAGE_2"],
textinstructions: "Drag the images to proper position"
})
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
```

## Other methods

### goodReport
Expand Down
23 changes: 23 additions & 0 deletions examples/dragAndDrop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('fs');
const TwoCaptcha = require("../dist/index.js");
require('dotenv').config();
const APIKEY = process.env.APIKEY;
const solver = new TwoCaptcha.Solver(APIKEY);

const backgroundBase64 = fs.readFileSync("./media/drag_drop_main.jpeg", "base64")
const imagesBase64 = [
fs.readFileSync("./media/drag_drop_image1.jpeg", "base64"),
fs.readFileSync("./media/drag_drop_image2.jpeg", "base64")
]

solver.dragAndDrop({
body: backgroundBase64,
images: imagesBase64,
textinstructions: "Drag the images to proper position"
})
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
Binary file added examples/media/drag_drop_image1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/media/drag_drop_image2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/media/drag_drop_main.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"Alibaba Captcha",
"TSPD",
"Basilisk",
"Hunt"
"Hunt",
"Drag and Drop"
],
"scripts": {
"build": "tsc && node ./dist/index.js",
Expand Down
75 changes: 75 additions & 0 deletions src/structs/2captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ export interface paramsHunt {
proxytype?: string,
}

export interface paramsDragAndDrop {
body: string,
images: string[],
textinstructions?: string,
language?: 0 | 1 | 2,
lang?: string,
pingback?: string,
}

/**
* An object containing properties of the captcha solution.
* @typedef {Object} CaptchaAnswer
Expand Down Expand Up @@ -2739,6 +2748,72 @@ public async hunt(params: paramsHunt): Promise<CaptchaAnswer> {
}
}

/**
* ### Solves Drag & Drop captcha
*
* A method for solving captcha where one or more images need to be dragged onto specific positions on a background image.
* [Read more about Drag & Drop captcha](https://2captcha.com/2captcha-api#drag-and-drop-captcha).
*
* @param {{ body, images, textinstructions, language, lang, pingback }} params Parameters Drag & Drop Captcha as an object.
* @param {string} params.body Background image encoded into Base64 format, without the `data:image/...;base64,` prefix.
* @param {string[]} params.images Array of Base64-encoded images to drag. Order matters — the same order is used in the response.
* @param {string} params.textinstructions Optional. Text with instruction for solving the captcha, up to 140 characters. For example: "Drag the images to proper position".
* @param {number} params.language Optional. `0` - not specified. `1` - Cyrillic captcha. `2` - Latin captcha.
* @param {string} params.lang Optional. Language code. [See the list of supported languages](https://2captcha.com/2captcha-api#language).
* @param {string} params.pingback Optional. URL for pingback (callback) response that will be sent when captcha is solved. [More info here](https://2captcha.com/2captcha-api#pingback).
*
* @returns {Promise<CaptchaAnswer>} The result from the solve. `data` is a pipe-separated string of coordinates, for example `"120,340|null|210,90"`. `null` means the corresponding image wasn't moved.
* @throws APIError
*
* @example
* const backgroundBase64 = fs.readFileSync("./media/drag_drop_main.jpeg", "base64")
* const imagesBase64 = [
* fs.readFileSync("./media/drag_drop_image1.jpeg", "base64"),
* fs.readFileSync("./media/drag_drop_image2.jpeg", "base64")
* ]
*
* solver.dragAndDrop({
* body: backgroundBase64,
* images: imagesBase64,
* textinstructions: "Drag the images to proper position"
* })
* .then((res) => {
* console.log(res);
* })
* .catch((err) => {
* console.log(err);
* })
*/
public async dragAndDrop(params: paramsDragAndDrop): Promise<CaptchaAnswer> {
checkCaptchaParams(params, "drag_drop")

const payload = {
...this.defaultPayload,
...params,
method: "drag_drop",
};

const response = await fetch(this.in, {
body: JSON.stringify(payload),
method: "post",
headers: { "Content-Type": "application/json" }
})
const result = await response.text()

let data;
try {
data = JSON.parse(result)
} catch {
throw new APIError(result)
}

if (data.status == 1) {
return this.pollResponse(data.request)
} else {
throw new APIError(data.request)
}
}

/**
* Reports a captcha as correctly solved.
*
Expand Down
6 changes: 5 additions & 1 deletion src/utils/checkCaptchaParams.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Captcha methods for which parameter checking is available
const supportedMethods = ["userrecaptcha", "hcaptcha", "geetest", "geetest_v4","yandex","funcaptcha","lemin","amazon_waf",
"turnstile", "base64", "capy","datadome", "cybersiara", "mt_captcha", "bounding_box", 'friendly_captcha', 'grid',
'textcaptcha', 'canvas', 'rotatecaptcha', 'keycaptcha', 'cutcaptcha', 'tencent', 'atb_captcha', 'prosopo', 'captchafox', 'vkimage', 'vkcaptcha', 'temu', 'altcha', 'binance', 'audio', 'yidun', 'alibaba', 'tspd', 'basilisk', 'hunt']
'textcaptcha', 'canvas', 'rotatecaptcha', 'keycaptcha', 'cutcaptcha', 'tencent', 'atb_captcha', 'prosopo', 'captchafox', 'vkimage', 'vkcaptcha', 'temu', 'altcha', 'binance', 'audio', 'yidun', 'alibaba', 'tspd', 'basilisk', 'hunt', 'drag_drop']

// Names of required fields that must be contained in the parameters captcha
const recaptchaRequiredFields = ['pageurl','googlekey']
Expand Down Expand Up @@ -42,6 +42,7 @@ const alibabaRequiredFields = ['pageurl', 'scene_id', 'prefix']
const tspdRequiredFields = ['pageurl', 'tspd_cookie', 'html_page_base64', 'proxy', 'proxytype']
const basiliskRequiredFields = ['pageurl', 'sitekey']
const huntRequiredFields = ['pageurl', 'api_get_lib']
const dragAndDropRequiredFields = ['body', 'images']

/**
* Getting required arguments for a captcha.
Expand Down Expand Up @@ -164,6 +165,9 @@ const getRequiredFildsArr = (method: string):Array<string> => {
case "hunt":
requiredFieldsArr = huntRequiredFields
break;
case "drag_drop":
requiredFieldsArr = dragAndDropRequiredFields
break;
}
return requiredFieldsArr
}
Expand Down