An example for solving reCAPTCHA 2 without an extension installation

A recaptcha solving for headless browsers without AntiCaptcha plugin installation. API key initialization, JS code inclusion.

For those who use headless browsers or just can't/doesn't want to install our extension we've developed an includible JS file which solves a captcha. It works only for a Recaptcha at the moment, image captchas are not supported.

This includible JS file also good for iMacros application, the only difference is that it should be connected otherwise, you can see an example on the reCAPTCHA 2 solving in iMacros application web-page.

Anti-Captcha.com API key initialization

Before we use a main code that solves a Recaptcha we need to set our AntiCaptcha API key which is placed into a DIV element using the following code:

(function(){ // ... var d = document.getElementById("anticaptcha-imacros-account-key"); if (!d) { d = document.createElement("div"); d.innerHTML = "YOUR-ANTI-CAPTCHA-API-KEY"; d.style.display = "none"; d.id = "anticaptcha-imacros-account-key"; document.body.appendChild(d); } // ... })();

You only need to replace YOUR-ANTI-CAPTCHA-API-KEY with your key.

Nothing special here. We just created an invisible element with id=anticaptcha-imacros-account-key and placed an API key inside of it and then put this element into the target web-page. This API key will be used by the main JS code.

This piece of code can be run in any environment. For instance in a browser console (F12) or in another includible JS file or using any other extension e.g. TamperMonkey or Greasemonkey.

Recaptcha solving code inclusion

Now it time to connect a script itself which does all the job:

(function(){ // ... var s = document.createElement("script"); s.src = "https://cdn.antcpt.com/imacros_inclusion/recaptcha.js?" + Math.random(); document.body.appendChild(s); // ... })();

This piece of code creates a SCRIPT element with a src=https://cdn.antcpt.com/imacros_inclusion/recaptcha.js and appends it into DOM. It's always recommended to connect this JS file from our web-server to have a latest version of this script with all the fixes and new features.

As a result, we have the following script:

(function(){ // ... var d = document.getElementById("anticaptcha-imacros-account-key"); if (!d) { d = document.createElement("div"); d.innerHTML = "YOUR-ANTI-CAPTCHA-API-KEY"; d.style.display = "none"; d.id = "anticaptcha-imacros-account-key"; document.body.appendChild(d); } var s = document.createElement("script"); s.src = "https://cdn.antcpt.com/imacros_inclusion/recaptcha.js?" + Math.random(); document.body.appendChild(s); // ... })();

On certain websites it's forbidden to include any external JS file because of a Content-Security-Policy header and our recaptcha.js won't be embedded as well.

In such cases a whole code of a JS recaptcha solving file can be placed by one line. Instead of creating a SCRIPT element you just put into your script a content of the following file
https://cdn.antcpt.com/imacros_inclusion/recaptcha.ai-sl.js .
All the code that solves a captcha and modules are compiled into a big big single line. It's not convenient, ugly and not recommended to do. But somethimes there is simply no other way.

Recaptcha solving example for Headless Chrome using Puppeteer

Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It can sound scary, but in fact the Puppeteer is a good tool for headless Chrome automatisation, it allows you to do many thing with different ways. See documentation for installation guide and usage examples.

Code snippet below tells browser to go to the web-page with reCAPTCHA 2 demo form, solves the Recaptcha and sends a web-form. Further it makes a screen shot for clarity. For the Recaptcha solving it creates an DIV where your AntiCaptcha API should be placed and then it includes the https://cdn.antcpt.com/imacros_inclusion/recaptcha.js file, the same as in other examples.

const puppeteer = require('puppeteer'); puppeteer.launch().then(async (browser) => { const page = await browser.newPage(); // set a screen size page.setViewport({ width: 1024, height: 768 }); // watch debug info from anti-captcha solver page.on('console', msg => { for (let i = 0; i < msg.args().length; ++i) { console.log(`${i}: ${msg.args()[i]}`); } }); page.once('load', async () => { // Fetch Anti-Captcha API key in DIV#anticaptcha-imacros-account-key element await page.evaluate(() => { var antcptAccountKeyDiv = document.getElementById("anticaptcha-imacros-account-key"); if (!antcptAccountKeyDiv) { antcptAccountKeyDiv = document.createElement("div"); // put your API key here antcptAccountKeyDiv.innerHTML = "YOUR-ANTI-CAPTCHA-API-KEY"; antcptAccountKeyDiv.style.display = "none"; antcptAccountKeyDiv.id = "anticaptcha-imacros-account-key"; document.body.appendChild(antcptAccountKeyDiv); } }); // anticaptchaUserCallbackMethod will be called (if defined) when something changes // captchaSolvingInfo contains "status" field of a captcha solving process, // which might be: in_process, error, solved await page.exposeFunction('anticaptchaUserCallbackMethod', (captchaSolvingInfo) => { console.log('captchaSolvingInfo', captchaSolvingInfo); }); // Include recaptcha.js file with all the functional await page.addScriptTag({ url: 'https://cdn.antcpt.com/imacros_inclusion/recaptcha.js'}); // Fill the text field with test value await page.$eval('input[name=demo_text]', el => el.value = 'Test message'); // Most important part: we wait 120 seconds until an AntiCatcha indicator // with class "antigate_solver" gets in additional "solved" class await page.waitForSelector('.antigate_solver.solved', { timeout: 120 * 1000 }); // Send form and waiting for page being loaded const navigationPromise = page.waitForNavigation(); page.click(['input[type=submit]']); await navigationPromise; // check if we did it right await page.screenshot({ path: 'screenshot_solved.png' }); // closing browser await browser.close(); }); // Go to the test web-page await page.goto('https://antcpt.com/eng/information/demo-form/recaptcha-2.html'); });