-
Notifications
You must be signed in to change notification settings - Fork 0
/
captchaSolver.js
83 lines (67 loc) · 3.08 KB
/
captchaSolver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const { getCaptchaParams } = require("./utils/getCaptchaParams");
const { clickAtCoordinates } = require('./utils/clickAtCoordinates');
const { clickRecaptchaVerifyButton } = require("./utils/clickRecaptchaVerifyButton");
const { isRecaptchaPassed } = require('./utils/isRecaptchaPassed');
const { initCaptchaParamsExtractor } = require('./utils/initCaptchaParamsExtractor')
const TwoCaptcha = require("@2captcha/captcha-solver");
const apikey = process.env.APIKEY;
const solver = new TwoCaptcha.Solver(apikey, 500);
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));
// The basic logic of the captcha solution
const captchaSolver = async function (page) {
// Set the value to `true` for visualization of clicks
const highlightClicks = false
const frameHandle = await page.waitForSelector(
'iframe[src*="https://www.google.com/recaptcha/api2/bframe"]'
);
let frame = await frameHandle.contentFrame();
// Initialize the captcha parameter extraction function in the frame
await initCaptchaParamsExtractor(frame)
let isCaptchaNotSolved = true
// The captcha solution cycle
while(isCaptchaNotSolved){
const captchaParams = await getCaptchaParams(frame);
console.log(`Successfully fetched captcha parameters. recaptcha size is ${captchaParams.columns}*${captchaParams.rows}`);
// Getting a captcha solution
const answer = await solver.grid({
body: captchaParams.body,
textinstructions: captchaParams.comment,
cols: captchaParams.columns,
rows: captchaParams.rows,
canSkip: 1,
"img_type": "recaptcha", // TODO: add param to lib
});
const isCapthcaSolved = answer.status === 1;
if (isCapthcaSolved) {
console.log(`The answer for captcha ${answer.id} was received successfully`);
console.log(answer);
if(answer.data === 'No_matching_images'){
// 'No_matching_images' - The captcha image does not contain images that meet the requirements. This means that the captcha has been solved.
await sleep(1213)
await clickRecaptchaVerifyButton(page)
}
} else {
// TODO: when you get "ERROR_CAPTCHA_UNSOLVABLE" you can try to solve captcha again
return false
}
// Parse the answer
let clicks = answer.data.replace("click:", ""); // removing the "click:" line from the response
clicks = clicks.split("/"); // '1/2/3/5/6/7' => ['1', '2', '3', '5', '6', '7']
clicks = clicks.map(el => Number(el)) // String => Number
console.log("Clicks:", clicks);
const captchaSize = captchaParams.columns
// Making clicks
let timeToSleep = 100 // ms
clicks.forEach(async (el, id) => {
await sleep(timeToSleep * id); // delay (number of seconds of delay for each click)
await clickAtCoordinates(page, captchaSize, el, highlightClicks);
})
await sleep(timeToSleep * (clicks.length + 1) + 2202)
await clickRecaptchaVerifyButton(page, highlightClicks)
await sleep(3000);
const isCaptchaSolved = await isRecaptchaPassed(page)
isCaptchaNotSolved = !isCaptchaSolved
}
return true
};
module.exports = { captchaSolver };