UNPKG

clicks-recaptcha-solver

Version:

A powerful automation tool for solving Google reCAPTCHA challenges using Playwright and Puppeteer. This package intelligently detects and simulates human-like interactions, including mouse movements and precise clicks, to bypass reCAPTCHA v2 and other sim

109 lines (108 loc) 5.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.captchaSolver = void 0; const captcha_solver_1 = require("@2captcha/captcha-solver"); const initCaptchaParamsExtractor_1 = require("./utils/initCaptchaParamsExtractor"); const getCaptchaParams_1 = require("./utils/getCaptchaParams"); const clickRecaptchaVerifyButton_1 = require("./utils/clickRecaptchaVerifyButton"); const clickAtCoordinates_1 = require("./utils/clickAtCoordinates"); const isRecaptchaPassed_1 = require("./utils/isRecaptchaPassed"); const isFoundRecaptchaChallengeFrame_1 = require("./utils/isFoundRecaptchaChallengeFrame"); const isFoundReCaptchaBadge_1 = require("./utils/isFoundReCaptchaBadge"); const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time)); // The basic logic of the captcha solution const captchaSolver = async function (page, apikey) { const solver = new captcha_solver_1.Solver(apikey, 500); const recaptchaBadgeIframeSelector = 'iframe[title="reCAPTCHA"]'; const recaptchaCheckboxSelector = 'span.recaptcha-checkbox-unchecked'; await page.waitForSelector(recaptchaBadgeIframeSelector, { timeout: 30000 }); await sleep(5000); // Search for reCAPTCHA Badge on the page const isFoundReCaptcha = await (0, isFoundReCaptchaBadge_1.isFoundReCaptchaBadge)(page); // Checking whether the recaptcha is found on the page, if not, then we generate an error message if (isFoundReCaptcha) { console.log(`reCAPTCHA Badge is found.`); } else { throw new Error('reCAPTCHA Badge not found!'); } // Getting a recaptcha badge iframe const iframeElementHandle = await page.$(recaptchaBadgeIframeSelector); if (!iframeElementHandle) { throw new Error('reCAPTCHA Badge iframe not found!'); } const recaptchaBadgeIframe = await iframeElementHandle.contentFrame(); if (recaptchaBadgeIframe) { // Click on checkbox reCAPTCHA await recaptchaBadgeIframe.evaluate((recaptchaCheckboxSelector) => { const recaptchaCheckbox = document.querySelector(recaptchaCheckboxSelector); if (recaptchaCheckbox) { recaptchaCheckbox.click(); } else { throw new Error('reCAPTCHA checkbox not found!'); } }, recaptchaCheckboxSelector); await sleep(5000); // We check for the presence of a frame with a captcha task const isRecaptchaChallengeShow = await (0, isFoundRecaptchaChallengeFrame_1.isFoundRecaptchaChallengeFrame)(page); // Set the value to `true` for visualization of clicks const highlightClicks = false; if (isRecaptchaChallengeShow) { 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 (0, initCaptchaParamsExtractor_1.initCaptchaParamsExtractor)(frame); let isCaptchaNotSolved = true; // The captcha solution cycle while (isCaptchaNotSolved) { const captchaParams = await (0, getCaptchaParams_1.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, imgType: 'recaptcha', // TODO: add param to lib, recaptcha: 1 }); const isCapthcaSolved = answer.data; 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 (0, clickRecaptchaVerifyButton_1.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:', '') .split('/') .map((el) => Number(el)); // removing the "click:" line from the response and converting to 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 (0, clickAtCoordinates_1.clickAtCoordinates)(page, captchaSize, el, highlightClicks); }); await sleep(timeToSleep * (clicks.length + 1) + 2202); await (0, clickRecaptchaVerifyButton_1.clickRecaptchaVerifyButton)(page, highlightClicks); await sleep(3000); const isCaptchaSolved = await (0, isRecaptchaPassed_1.isRecaptchaPassed)(page); isCaptchaNotSolved = !isCaptchaSolved; } return true; } } }; exports.captchaSolver = captchaSolver;