Skip to content

Commit c2a7b92

Browse files
Add Canvas Method
1 parent 67696a3 commit c2a7b92

File tree

6 files changed

+109
-2
lines changed

6 files changed

+109
-2
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Examples of API requests for different captcha types are available on the [JavaS
3737
- [Bounding Box Method](#bounding-box-method)
3838
- [Grid](#grid)
3939
- [Text Captcha](#text-captcha)
40+
- [Canvas](#canvas)
4041
- [Other methods](#other-methods)
4142
- [goodReport](#goodreport)
4243
- [badReport](#badreport)
@@ -520,6 +521,26 @@ solver.textCaptcha({
520521
})
521522
```
522523

524+
### Canvas
525+
526+
<sup>[API method description.](https://2captcha.com/2captcha-api#canvas)</sup>
527+
528+
The canvas method can be used when you need to draw a line around an object on an image. Returns a set of points' coordinates to draw a polygon.
529+
530+
```js
531+
solver.canvas({
532+
body: 'iVBORw0KGgoAAAANSgAAAcIA...',
533+
imginstructions: '/9j/4AAQSkZJRgABAQEA...',
534+
textinstructions: 'Highlight the red CIRCLE'
535+
})
536+
.then((res) => {
537+
console.log(res);
538+
})
539+
.catch((err) => {
540+
console.log(err);
541+
})
542+
```
543+
523544
## Other methods
524545

525546
### goodReport

examples/canvas.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const TwoCaptcha = require("../dist/index.js");
2+
require('dotenv').config();
3+
const APIKEY = process.env.APIKEY
4+
const solver = new TwoCaptcha.Solver(APIKEY);
5+
const fs = require('fs')
6+
const imageBase64 = fs.readFileSync("./media/canvas.png", "base64")
7+
const imginstructionsBase64 = fs.readFileSync("./media/canvasImgInstructions.jpg", "base64")
8+
9+
solver.canvas({
10+
body: imageBase64,
11+
textinstructions: 'Highlight the red CIRCLE',
12+
imginstructions: imginstructionsBase64,
13+
})
14+
.then((res) => {
15+
console.log(res);
16+
})
17+
.catch((err) => {
18+
console.log(err);
19+
})

examples/media/canvas.png

2.73 KB
Loading
14.4 KB
Loading

src/structs/2captcha.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ export interface paramsBoundingBox {
224224

225225
export interface paramsGrid {
226226
body: string,
227-
recaptcha: 1,
227+
recaptcha: number,
228+
canvas?: number,
228229
rows?: number,
229230
cols?: number,
230231
minСlicks?: number,
@@ -1425,6 +1426,7 @@ public async grid(params: paramsGrid): Promise<CaptchaAnswer> {
14251426
throw new APIError(data.request)
14261427
}
14271428
}
1429+
14281430
/**
14291431
* ### Text Captcha method
14301432
*
@@ -1479,6 +1481,67 @@ public async text(params: paramsTextcaptcha): Promise<CaptchaAnswer> {
14791481
}
14801482
}
14811483

1484+
/**
1485+
* ### Canvas method
1486+
*
1487+
* This method can be used to bypass tasks in which you need to circle an object or line in an image.
1488+
*
1489+
* @param {{ body, textinstructions, imginstructions, canSkip, lang, pingback}} params Parameters Canvas as an object.
1490+
* @param {string} params.body `Base64`- encoded captcha image.
1491+
* @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`.
1492+
* @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`.
1493+
* @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.
1494+
* @param {string} params.lang Language code. [See the list of supported languages](https://2captcha.com/2captcha-api#language).
1495+
* @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).
1496+
*
1497+
* @example
1498+
* solver.canvas({
1499+
* body: 'iVBORw0KGgoAAAANSgAAAcIA...',
1500+
* imginstructions: '/9j/4AAQSkZJRgABAQEA...',
1501+
* textinstructions: 'Highlight the red CIRCLE'
1502+
* })
1503+
* .then((res) => {
1504+
* console.log(res);
1505+
* })
1506+
* .catch((err) => {
1507+
* console.log(err);
1508+
* })
1509+
*/
1510+
public async canvas(params: paramsGrid): Promise<CaptchaAnswer> {
1511+
checkCaptchaParams(params, "canvas")
1512+
1513+
params = await renameParams(params)
1514+
1515+
const payload = {
1516+
...params,
1517+
recaptcha: 1,
1518+
canvas: 1,
1519+
method: "base64",
1520+
...this.defaultPayload,
1521+
}
1522+
1523+
const URL = this.in
1524+
const response = await fetch(URL, {
1525+
body: JSON.stringify( payload ),
1526+
method: "post",
1527+
headers: {'Content-Type': 'application/json'}
1528+
})
1529+
const result = await response.text()
1530+
1531+
let data;
1532+
try {
1533+
data = JSON.parse(result)
1534+
} catch {
1535+
throw new APIError(result)
1536+
}
1537+
1538+
if (data.status == 1) {
1539+
return this.pollResponse(data.request)
1540+
} else {
1541+
throw new APIError(data.request)
1542+
}
1543+
}
1544+
14821545
/**
14831546
* Reports a captcha as correctly solved.
14841547
*

src/utils/checkCaptchaParams.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Captcha methods for which parameter checking is available
22
const supportedMethods = ["userrecaptcha", "hcaptcha", "geetest", "geetest_v4","yandex","funcaptcha","lemin","amazon_waf",
3-
"turnstile", "base64", "capy","datadome", "cybersiara", "mt_captcha", "bounding_box", 'friendly_captcha', 'grid', 'textcaptcha']
3+
"turnstile", "base64", "capy","datadome", "cybersiara", "mt_captcha", "bounding_box", 'friendly_captcha', 'grid', 'textcaptcha', 'canvas']
44

55
// Names of required fields that must be contained in the parameters captcha
66
const recaptchaRequiredFields = ['pageurl','googlekey']
@@ -22,6 +22,7 @@ const boundingBoxRequiredFields = ['image'] // and textinstructions or imginstru
2222
const friendlyCaptchaFields = ['pageurl','sitekey']
2323
const gridRequiredFields = ['body'] // and textinstructions or imginstructions
2424
const textCaptchaRequiredFields = ['textcaptcha']
25+
const canvasRequiredFields = ['body'] // надо проверку, если какая нибудь инструкция текст\картинка
2526

2627
/**
2728
* Getting required arguments for a captcha.
@@ -87,6 +88,9 @@ const getRequiredFildsArr = (method: string):Array<string> => {
8788
case "textcaptcha":
8889
requiredFieldsArr = textCaptchaRequiredFields
8990
break;
91+
case "canvas":
92+
requiredFieldsArr = canvasRequiredFields
93+
break;
9094
}
9195
return requiredFieldsArr
9296
}

0 commit comments

Comments
 (0)