rebrowser / rebrowser-patches

Collection of patches for puppeteer and playwright to avoid automation detection and leaks. Helps to avoid Cloudflare and DataDome CAPTCHA pages. Easy to patch/unpatch, can be enabled/disabled on demand.
https://rebrowser.net
375 stars 28 forks source link

How to back again to the main context #2

Closed ahmedmz877 closed 3 months ago

ahmedmz877 commented 3 months ago
import puppeteer from 'puppeteer-core';
import { DateTime } from 'luxon';
import { dirname, join } from 'path';
import xlsx from 'node-xlsx';
import { execSync } from 'child_process';
import { exec } from 'child_process';
import process from 'process';
import { promisify } from 'util';
import path from 'path';
import fsp from 'fs/promises';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { spawn } from 'child_process';
import { performance } from 'perf_hooks';
import axios from 'axios';
import FormData from 'form-data';

(async () => {
    // Set the environment variable to alwaysIsolated mode
  process.env.REBROWSER_PATCHES_RUNTIME_FIX_MODE = "alwaysIsolated";
  const __dirname = dirname(fileURLToPath(import.meta.url));
  const EXTENSION_PATH = `${__dirname}/turnstilePatch/`;
  const userAgent = getUserAgent();
  console.log(`Using User Agent: ${userAgent}`);
  console.log('Starting new browser...');
  const browser = await puppeteer.launch({
    executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe', 
    headless: false,
    args: [
      `--disable-extensions-except=${EXTENSION_PATH}`,
      `--load-extension=${EXTENSION_PATH}`,
      `--user-agent=${userAgent}`,
      '--window-size=1366,960',
      '--disable-blink-features=AutomationControlled',
      '--disable-infobars',
      '--disable-web-security',
      '--disable-site-isolation-trials',
      '--password-store=basic',
      '--no-service-autorun',
      '--homepage=about:blank',
      '--trace-deprecation',
      '--no-sandbox',
      '--no-pings',
      '--disable-breakpad',
      '--disable-component-update',
      '--disable-backgrounding-occluded-windows',
      '--disable-renderer-backgrounding',
      '--disable-background-networking',
      '--disable-dev-shm-usage',
      '--disable-features=IsolateOrigins,site-per-process',
      '--disable-setuid-sandbox',
      '--disable-session-crashed-bubble',
      '--remote-allow-origins=*',
      '--no-first-run',
      '--metrics-recording-only',
      '--no-default-browser-check',
      '--disable-background-mode',
      '--disable-features=FlashDeprecationWarning,EnablePasswordsAccountStorage',
      '--deny-permission-prompts',
      '--disable-gpu',
    ],
  });
  const page = await browser.newPage();
  await page.setViewport({ width: 1366, height: 960 });
  await page.goto('https://pixelscan.net/');
  // Close the initial blank page
    const pages = await browser.pages();
    if (pages.length > 0) {
        await pages[0].close();
    }
  setInterval(() => clickTurnstile(page), 1000);
  await page.setCacheEnabled(true);     
  let turnstileToken = null;
  while (true) {
      try {
          turnstileToken = await getTurnstileToken(page);
          if (turnstileToken) {
              break;
          }
      } catch (e) {
          console.error(`Error getting Turnstile token: ${e}`);
          break;
     }
  }
  // Reset the environment variable to its original value
  const customerData = await loadCustomerData();
  await login(page, customerData);
  clearInterval(cl)

  // Add any additional actions or interactions with the page here

})();

the patch worked very good and the cf turnstile passed i want to get back again to access the await login(page, customerData);

nwebson commented 3 months ago

I'm glad it worked for you.

Could you please clarify, what's the issue with this code? If you want to disable the patch after passing CF, you can disable the fix by:

 process.env.REBROWSER_PATCHES_RUNTIME_FIX_MODE = "";

But you might need to open a new page in order to disable the patch and get back to default Runtime.Enable behaviour.

Actually, I think it's not necessary to disable the patch as everything should work as it was before the patch. Let me know if it doesn't.

ahmedmz877 commented 3 months ago

I can't open new page because if the patch enabled the cf will succeed but the next step for me set the username and the password then sign in stop working and can't reach to the login function. when disable the patch again it will need to open new page the cf get failure and it's continue the username and password. but i can't sign in as i say the cf failure. The question here. How to continue using the patch and reach to the login function without disable it or even i close or restart the script?

nwebson commented 3 months ago

Interesting case, could you share your login function code? I'm curious where does it fail wit the patch.

ahmedmz877 commented 3 months ago

Your discord please or any social account. I will record a video to explain the issue

nwebson commented 3 months ago

Discord - @nwebson

ahmedmz877 commented 3 months ago

found the issue was in my codes😁😎

  setInterval(() => clickTurnstile(page), 1000);
  await page.setCacheEnabled(true);     
  let turnstileToken = null;
  while (true) {
      try {
          turnstileToken = await getTurnstileToken(page);
          if (turnstileToken) {
              break;
          }
      } catch (e) {
          console.error(`Error getting Turnstile token: ${e}`);
          break;
     }
  }
  // Reset the environment variable to its original value
  const customerData = await loadCustomerData();
  await login(page, customerData);
  clearInterval(cl)

the patch works with amazing way😍 this my code now👌

  setInterval(() => clickTurnstile(page), 1000); 
  const customerData = await loadCustomerData();
  await login(page, customerData);
ahmedmz877 commented 3 months ago

ه

nwebson commented 3 months ago

Thanks, I appreciate the feedback!