user: from here remove the prediction and image analyzation: const express = require('express');
const { connect } = require('puppeteer-real-browser');
const path = require('path');
const fs = require('fs');
const onnx = require('onnxjs-node');
const jimp = require('jimp');

const router = express.Router();

const folderPath = path.join(__dirname, 'recaptchav2');
if (!fs.existsSync(folderPath)) {
  fs.mkdirSync(folderPath);
}

const modelPath = path.join(__dirname, 'models', 'recaptcha.onnx');
let model;

async function loadModel() {
  if (!model) {
    model = new onnx.InferenceSession();
    await model.loadModel(modelPath);
  }
  return model;
}

async function launchChrome() {
  const chromeLauncher = await import('chrome-launcher');
  return await chromeLauncher.launch({
    chromeFlags: [
      '--headless',
      '--disable-gpu',
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-dev-shm-usage',
      '--disable-extensions',
      '--disable-component-extensions-with-background-pages',
      '--disable-default-apps',
      '--mute-audio',
      '--no-default-browser-check',
      '--no-pings',
      '--disable-sync',
      '--disable-background-networking',
      '--disable-background-timer-throttling',
      '--disable-backgrounding-occluded-windows',
      '--disable-breakpad',
      '--disable-client-side-phishing-detection',
      '--disable-features=TranslateUI,BlinkGenPropertyTrees',
      '--disable-ipc-flooding-protection',
      '--disable-prompt-on-repost',
      '--force-color-profile=srgb',
      '--metrics-recording-only',
      '--password-store=basic',
    ]
  });
}

async function takeScreenshotCarefully(element, path) {
  await element.evaluate(el => {
    return new Promise(resolve => {
      const observer = new MutationObserver(() => {
        observer.disconnect();
        setTimeout(resolve, 500);
      });
      observer.observe(el, { attributes: true, childList: true, subtree: true });
      setTimeout(resolve, 2000);
    });
  });
  await element.screenshot({ path });
}

async function analyzeImage(imagePath) {
  try {
    const model = await loadModel();
    const image = await jimp.read(imagePath);
    image.resize(224, 224); // Resize to match YOLO input size
    const imageData = new Float32Array(image.bitmap.data);
    
    // Normalize the image data (assuming 0-255 range to 0-1)
    for (let i = 0; i < imageData.length; i++) {
      imageData[i] = imageData[i] / 255.0;
    }

    const tensor = new onnx.Tensor(imageData, 'float32', [1, 3, 224, 224]);
    const output = await model.run([tensor]);
    return output[0].data;
  } catch (error) {
    console.error('Error analyzing image:', error);
    return { error: error.message };
  }
}

router.get('/captcha-screenshot', async (req, res) => {
  let chrome, browser, page;
  try {
    console.log('Processing captcha screenshot request');
    
    chrome = await launchChrome();
    
    const response = await connect({
      browserWSEndpoint: `ws://localhost:${chrome.port}`,
      ignoreHTTPSErrors: true,
      defaultViewport: null,
      turnstile: true
    });

    browser = response.browser;
    page = response.page;

    console.log('Navigating to https://2captcha.com/demo/recaptcha-v2');
    await page.goto('https://2captcha.com/demo/recaptcha-v2', { 
      waitUntil: 'networkidle0',
      timeout: 60000
    });

    await page.waitForSelector('iframe[src^="https://www.google.com/recaptcha/api2/anchor"]', { timeout: 60000 });
    const frame = page.frames().find(frame => frame.url().includes('https://www.google.com/recaptcha/api2/anchor'));
    
    if (frame) {
      await frame.click('div.recaptcha-checkbox-border');
    } else {
      throw new Error('reCAPTCHA frame not found');
    }

    await page.waitForSelector('iframe[src^="https://www.google.com/recaptcha/api2/bframe"]', { timeout: 60000 });
    const challengeFrame = page.frames().find(frame => frame.url().includes('https://www.google.com/recaptcha/api2/bframe'));

    if (challengeFrame) {
      await challengeFrame.waitForSelector('#rc-imageselect', { timeout: 60000 });
      
      console.log('Processing image selection challenge');
      
      const instructionElement = await challengeFrame.$('.rc-imageselect-desc-no-canonical strong');
      let instructionText = '';
      if (instructionElement) {
        instructionText = await challengeFrame.evaluate(el => el.textContent, instructionElement);
        console.log(`Instruction says, look for: ${instructionText}`);
      } else {
        console.log('Instruction element not found');
      }

      const is3x3 = await challengeFrame.$('.rc-imageselect-table-33');
      const is4x4 = await challengeFrame.$('.rc-imageselect-table-44');
      
      const gridType = is3x3 ? '3x3' : (is4x4 ? '4x4' : 'unknown');
      console.log(`Detected ${gridType} CAPTCHA grid`);

      const targetElement = await challengeFrame.$('#rc-imageselect-target');
      if (targetElement) {
        const targetPath = path.join(folderPath, 'target.png');
        await takeScreenshotCarefully(targetElement, targetPath);
        console.log(`Target screenshot saved as ${targetPath}`);
      }

      const tiles = await challengeFrame.$$('.rc-imageselect-tile');
      const predictions = [];

      for (let i = 0; i < tiles.length; i++) {
        const imagePath = path.join(folderPath, `image-${i + 1}.png`);
        await takeScreenshotCarefully(tiles[i], imagePath);
        console.log(`Image ${i + 1} saved as ${imagePath}`);

        const imagePredictions = await analyzeImage(imagePath);
        predictions.push({
          imageNumber: i + 1,
          predictions: imagePredictions
        });

        await new Promise(resolve => setTimeout(resolve, 1000));
      }

      res.json({ 
        success: true, 
        message: 'CAPTCHA images saved and analyzed', 
        instruction: instructionText,
        gridType: gridType,
        predictions: predictions
      });
    } else {
      throw new Error('Challenge frame not found');
    }
  } catch (error) {
    console.error('Error:', error);
    res.status(500).json({ success: false, error: 'An error occurred', details: error.message });
  } finally {
    if (browser) {
      await browser.close();
    }
    if (chrome) {
      await chrome.kill();
    }
  }
});

module.exports = router;
PROMPT: user: from here remove the prediction and image analyzation: const express = require('express');
const { connect } = require('puppeteer-real-browser');
const path = require('path');
const fs = require('fs');
const onnx = require('onnxjs-node');
const jimp = require('jimp');

const router = express.Router();

const folderPath = path.join(__dirname, 'recaptchav2');
if (!fs.existsSync(folderPath)) {
  fs.mkdirSync(folderPath);
}

const modelPath = path.join(__dirname, 'models', 'recaptcha.onnx');
let model;

async function loadModel() {
  if (!model) {
    model = new onnx.InferenceSession();
    await model.loadModel(modelPath);
  }
  return model;
}

async function launchChrome() {
  const chromeLauncher = await import('chrome-launcher');
  return await chromeLauncher.launch({
    chromeFlags: [
      '--headless',
      '--disable-gpu',
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-dev-shm-usage',
      '--disable-extensions',
      '--disable-component-extensions-with-background-pages',
      '--disable-default-apps',
      '--mute-audio',
      '--no-default-browser-check',
      '--no-pings',
      '--disable-sync',
      '--disable-background-networking',
      '--disable-background-timer-throttling',
      '--disable-backgrounding-occluded-windows',
      '--disable-breakpad',
      '--disable-client-side-phishing-detection',
      '--disable-features=TranslateUI,BlinkGenPropertyTrees',
      '--disable-ipc-flooding-protection',
      '--disable-prompt-on-repost',
      '--force-color-profile=srgb',
      '--metrics-recording-only',
      '--password-store=basic',
    ]
  });
}

async function takeScreenshotCarefully(element, path) {
  await element.evaluate(el => {
    return new Promise(resolve => {
      const observer = new MutationObserver(() => {
        observer.disconnect();
        setTimeout(resolve, 500);
      });
      observer.observe(el, { attributes: true, childList: true, subtree: true });
      setTimeout(resolve, 2000);
    });
  });
  await element.screenshot({ path });
}

async function analyzeImage(imagePath) {
  try {
    const model = await loadModel();
    const image = await jimp.read(imagePath);
    image.resize(224, 224); // Resize to match YOLO input size
    const imageData = new Float32Array(image.bitmap.data);
    
    // Normalize the image data (assuming 0-255 range to 0-1)
    for (let i = 0; i < imageData.length; i++) {
      imageData[i] = imageData[i] / 255.0;
    }

    const tensor = new onnx.Tensor(imageData, 'float32', [1, 3, 224, 224]);
    const output = await model.run([tensor]);
    return output[0].data;
  } catch (error) {
    console.error('Error analyzing image:', error);
    return { error: error.message };
  }
}

router.get('/captcha-screenshot', async (req, res) => {
  let chrome, browser, page;
  try {
    console.log('Processing captcha screenshot request');
    
    chrome = await launchChrome();
    
    const response = await connect({
      browserWSEndpoint: `ws://localhost:${chrome.port}`,
      ignoreHTTPSErrors: true,
      defaultViewport: null,
      turnstile: true
    });

    browser = response.browser;
    page = response.page;

    console.log('Navigating to https://2captcha.com/demo/recaptcha-v2');
    await page.goto('https://2captcha.com/demo/recaptcha-v2', { 
      waitUntil: 'networkidle0',
      timeout: 60000
    });

    await page.waitForSelector('iframe[src^="https://www.google.com/recaptcha/api2/anchor"]', { timeout: 60000 });
    const frame = page.frames().find(frame => frame.url().includes('https://www.google.com/recaptcha/api2/anchor'));
    
    if (frame) {
      await frame.click('div.recaptcha-checkbox-border');
    } else {
      throw new Error('reCAPTCHA frame not found');
    }

    await page.waitForSelector('iframe[src^="https://www.google.com/recaptcha/api2/bframe"]', { timeout: 60000 });
    const challengeFrame = page.frames().find(frame => frame.url().includes('https://www.google.com/recaptcha/api2/bframe'));

    if (challengeFrame) {
      await challengeFrame.waitForSelector('#rc-imageselect', { timeout: 60000 });
      
      console.log('Processing image selection challenge');
      
      const instructionElement = await challengeFrame.$('.rc-imageselect-desc-no-canonical strong');
      let instructionText = '';
      if (instructionElement) {
        instructionText = await challengeFrame.evaluate(el => el.textContent, instructionElement);
        console.log(`Instruction says, look for: ${instructionText}`);
      } else {
        console.log('Instruction element not found');
      }

      const is3x3 = await challengeFrame.$('.rc-imageselect-table-33');
      const is4x4 = await challengeFrame.$('.rc-imageselect-table-44');
      
      const gridType = is3x3 ? '3x3' : (is4x4 ? '4x4' : 'unknown');
      console.log(`Detected ${gridType} CAPTCHA grid`);

      const targetElement = await challengeFrame.$('#rc-imageselect-target');
      if (targetElement) {
        const targetPath = path.join(folderPath, 'target.png');
        await takeScreenshotCarefully(targetElement, targetPath);
        console.log(`Target screenshot saved as ${targetPath}`);
      }

      const tiles = await challengeFrame.$$('.rc-imageselect-tile');
      const predictions = [];

      for (let i = 0; i < tiles.length; i++) {
        const imagePath = path.join(folderPath, `image-${i + 1}.png`);
        await takeScreenshotCarefully(tiles[i], imagePath);
        console.log(`Image ${i + 1} saved as ${imagePath}`);

        const imagePredictions = await analyzeImage(imagePath);
        predictions.push({
          imageNumber: i + 1,
          predictions: imagePredictions
        });

        await new Promise(resolve => setTimeout(resolve, 1000));
      }

      res.json({ 
        success: true, 
        message: 'CAPTCHA images saved and analyzed', 
        instruction: instructionText,
        gridType: gridType,
        predictions: predictions
      });
    } else {
      throw new Error('Challenge frame not found');
    }
  } catch (error) {
    console.error('Error:', error);
    res.status(500).json({ success: false, error: 'An error occurred', details: error.message });
  } finally {
    if (browser) {
      await browser.close();
    }
    if (chrome) {
      await chrome.kill();
    }
  }
});

module.exports = router;