Skip to content

Commit 85d42e3

Browse files
Add Grid method
1 parent 6ea78f5 commit 85d42e3

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

src/structs/2captcha.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as utils from "../utils/generic"
44
import getProviderData from "./providers/providers"
55
import { softId } from "./constants/constants"
66
import checkCaptchaParams from "../utils/checkCaptchaParams"
7+
import renameParams from "../utils/renameParams"
78

89

910
const provider = getProviderData ()
@@ -221,6 +222,21 @@ export interface paramsBoundingBox {
221222
imginstructions?: string,
222223
}
223224

225+
export interface paramsGrid {
226+
body: string,
227+
recaptcha: 1,
228+
rows?: number
229+
cols?: number
230+
minСlicks?: number,
231+
maxСlicks?: number,
232+
previousId?: string,
233+
textinstructions?: string,
234+
imginstructions?: string,
235+
canSkip?: number,
236+
lang?: string,
237+
pingback?: string,
238+
}
239+
224240
/**
225241
* An object containing properties of the captcha solution.
226242
* @typedef {Object} CaptchaAnswer
@@ -1342,6 +1358,72 @@ public async boundingBox(params: paramsBoundingBox): Promise<CaptchaAnswer> {
13421358
}
13431359
}
13441360

1361+
1362+
/**
1363+
* ### Grid method
1364+
*
1365+
* The method can be used to bypass tasks where a grid is applied to an image and you need to click on grid tiles, like reCAPTCHA or hCaptcha images.
1366+
*
1367+
* @param {{ body, textinstructions, imginstructions, rows, cols, minСlicks, maxСlicks, previousId, canSkip, lang, pingback}} params Parameters Grid Method as an object.
1368+
* @param {string} params.body `Base64`- encoded captcha image.
1369+
* @param {string} params.textinstructions Text will be shown to worker to help him to select object on the image correctly. For example: "*Select cars in the image*". **Optional parameter**, if the instruction already exists in the form of the `imginstructions`.
1370+
* @param {string} params.imginstructions Image with instruction for worker to help him to select object on the image correctly. The image must be encoded in `Base64` format. **Optional parameter**, if the instruction already exists in the form of the `textinstructions`.
1371+
* @param {number} params.rows Number of rows in grid captcha.
1372+
* @param {number} params.cols Number of columns in grid captcdha.
1373+
* @param {number} params.minСlicks The minimum number of tiles that must be selected. Can't be more than `rows` * `cols`.
1374+
* @param {number} params.maxСlicks The maximum number of tiles that can be selected on the image.
1375+
* @param {string} params.previousId Id of your previous request with the same captcha challenge.
1376+
* @param {number} params.canSkip Set the value to `1` only if it's possible that there's no images matching to the instruction. We'll provide a button "No matching images" to worker and you will receive `No_matching_images` as answer.
1377+
* @param {string} params.lang Language code. [See the list of supported languages](https://2captcha.com/2captcha-api#language).
1378+
* @param {string} params.pingback params.pingback URL for pingback (callback) response that will be sent when captcha is solved. URL should be registered on the server. [More info here](https://2captcha.com/2captcha-api#pingback).
1379+
*
1380+
* @example
1381+
* solver.grid({
1382+
* body: 'iVBORw0KGgoAAAANSUhEUgAAAcIA...',
1383+
* textinstructions: "Select cars in the image",
1384+
* imginstructions: '/9j/4AAQSkZJRgABAQEA...'
1385+
* })
1386+
* .then((res) => {
1387+
* console.log(res);
1388+
* })
1389+
* .catch((err) => {
1390+
* console.log(err);
1391+
* })
1392+
*/
1393+
public async grid(params: paramsGrid): Promise<CaptchaAnswer> {
1394+
checkCaptchaParams(params, "grid")
1395+
1396+
params = await renameParams(params)
1397+
1398+
const payload = {
1399+
...params,
1400+
method: "base64",
1401+
recaptcha: 1,
1402+
...this.defaultPayload,
1403+
}
1404+
1405+
const URL = this.in
1406+
const response = await fetch(URL, {
1407+
body: JSON.stringify( payload ),
1408+
method: "post",
1409+
headers: {'Content-Type': 'application/json'}
1410+
})
1411+
const result = await response.text()
1412+
1413+
let data;
1414+
try {
1415+
data = JSON.parse(result)
1416+
} catch {
1417+
throw new APIError(result)
1418+
}
1419+
1420+
if (data.status == 1) {
1421+
return this.pollResponse(data.request)
1422+
} else {
1423+
throw new APIError(data.request)
1424+
}
1425+
}
1426+
13451427
/**
13461428
* Reports a captcha as correctly solved.
13471429
*

0 commit comments

Comments
 (0)