import puppeteer from 'puppeteer-extra'; import StealthPlugin from 'puppeteer-extra-plugin-stealth'; import fs from 'fs';

let chromiumPath = process.platform === 'linux' ? '/usr/bin/chromium-browser' : null; if (chromiumPath && !fs.existsSync(chromiumPath)) console.log('[node_characterai] Puppeteer - Warning: the specified Chromium path for puppeteer could not be located. If the script does not work properly, you may need to specify a path to the Chromium binary file/executable.');

class Requester { browser = undefined; page = undefined;

#initialized = false;
#hasDisplayed = false;

#headless = 'new';
puppeteerPath = undefined;
puppeteerLaunchArgs = [
    '--fast-start',
    '--disable-extensions',
    '--no-sandbox',
    '--disable-setuid-sandbox',
    '--no-gpu',
    '--disable-background-timer-throttling',
    '--disable-renderer-backgrounding',
    '--override-plugin-power-saver-for-testing=never',
    '--disable-extensions-http-throttling',
    '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.3'
];
puppeteerNoDefaultTimeout = false;
puppeteerProtocolTimeout = 0;
usePlus = false;
forceWaitingRoom = false;

constructor() {}
isInitialized() {
    return this.#initialized;
}
async waitForWaitingRoom(page) {
    // Enable force waiting room to ensure you check for waiting room even on C.AI+
    if (!this.usePlus || (this.usePlus && this.forceWaitingRoom)) {
        return new Promise(async (resolve) => {
            try {
                let interval;
                let pass = true;

                const minute = 60000; // Update every minute

                // Keep waiting until false
                async function check() {
                    if (pass) {
                        pass = false;

                        const waitingRoomTimeLeft = await page.evaluate(async() => {
                            try {
                                const contentContainer = document.querySelector('.content-container');
                                const sections = contentContainer.querySelectorAll('section');
                                const h2Element = sections[1].querySelector('h2');
                                const h2Text = h2Element.innerText;
                                const regex = /\d+/g;
                                const matches = h2Text.match(regex);

                                if (matches) return matches[0];
                            } catch (error) {
                                return;
                            }
                        }, minute);

                        const waiting = (waitingRoomTimeLeft != null);
                        if (waiting) {
                            console.log(`[node_characterai] Puppeteer - Currently in cloudflare's waiting room. Time left: ${waitingRoomTimeLeft}`);
                        } else {
                            resolve();
                            clearInterval(interval);
                        }
                        pass = true;
                    };
                } as any);

                interval = setInterval(check, minute);
            } catch (error) {
                console.error(`[node_characterai] Puppeteer - Error in waitForWaitingRoom: ${error}`);
            }
        });
    }
}

}

export default Requester;

Puppeteer: Cloudflare Waiting Room Detection and Handling in Node.js

原文地址: https://www.cveoy.top/t/topic/mnW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录