79338754

Date: 2025-01-08 10:29:49
Score: 0.5
Natty:
Report link

Still asking myself why @ChrisF deleted my previous answer where I added the YouTube video URL to give credit to the person who wrote the code and shared it on YouTube. I will write the scripts I found that helped me solve the same problem.

// inject.js
console.clear = () => console.log('Console was cleared');
const i = setInterval(() => {
  if (window.turnstile) {
    clearInterval(i);
    window.turnstile.render = (a, b) => {
      let params = {
        sitekey: b.sitekey,
        pageurl: window.location.href,
        data: b.cData,
        pagedata: b.chlPageData,
        action: b.action,
        userAgent: navigator.userAgent,
        json: 1,
      };
      // we will intercept the message
      console.log('intercepted-params:' + JSON.stringify(params));
      window.cfCallback = b.callback;
      return;
    };
  }
},5);


// index.js

const { chromium } = require('playwright');
 const { Solver } = require('@2captcha/captcha-solver');

 const solver = new Solver('Your twocaptcha API key');
 const proxyServer = 'Proxy server';    // Proxy server manager
 const proxyUser = 'Proxy user';
 const prpxyPassword = 'Proxy Password';


 const example = async () => {
    const browser = await chromium.launch({
        headless: false,
        devtools: false,
        proxy: { "server": proxyServer, "username": proxyUser, "password": prpxyPassword },
    });

    const context = await browser.newContext({ ignoreHTTPSErrors: true });

    const page = await context.newPage();
    await page.addInitScript({ path: './inject.js' });

    page.on('console', async (msg) => {
        const txt = msg.text();
        if (txt.includes('intercepted-params:')) {
            const params = JSON.parse(txt.replace('intercepted-params:', ''));
            console.log(params);

            try {
                console.log(`Solving the captcha...`);
                const res = await solver.cloudflareTurnstile(params);
                console.log(`Solved the captcha ${res.id}`);
                console.log(res);
                await page.evaluate((token) => {
                    cfCallback(token);
                }, res.data);
            } catch (e) {
                console.log(e.err);
                return process.exit();
            }
        } else {
            return;
        }
    });

    await page.goto('site url');

    await page.waitForTimeout(5000);
    await page.reload({ waitUntil: "networkidle" });
    console.log('Reloaded');

};

    example();

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @ChrisF
  • Low reputation (1):
Posted by: CosminVizitiu