zfcsoftware / puppeteer-real-browser

This package is designed to bypass puppeteer's bot-detecting captchas such as Cloudflare. It acts like a real browser and can be managed with puppeteer.
https://www.npmjs.com/package/puppeteer-real-browser
MIT License
354 stars 46 forks source link

Support for adblock and other puppeter extra plugins #32

Open sharpanimes opened 3 months ago

sharpanimes commented 3 months ago

Hey i was wondering if i could use the puppeter extra adblock feature, cause i couldnt find a way to do this currently.

zfcsoftware commented 3 months ago

Hello, you will need to modify the library files for this. First you need to set the tf variable to false when starting the browser and then set it to true with settarget. Then you can add the plugins you want to the index.js file of the library. https://github.com/zfcsoftware/puppeteer-real-browser/blob/main/src/index.js


import { connect } from 'puppeteer-real-browser'

connect({
    tf: false, // If a feature you want to use at startup is not working, you can initialize the tf variable false and update it later.
    turnstile: true
})
.then(async response => {
        const { page, browser, setTarget } = response
     setTarget({ status: true })
        page.goto('https://nopecha.com/demo/cloudflare', {
            waitUntil: 'domcontentloaded'
        })

})
sharpanimes commented 3 months ago
import { startSession, closeSession } from './module/chromium.js'
import puppeteer from 'puppeteer-extra';
import { notice, slugify } from './module/general.js'
import { autoSolve, setSolveStatus } from './module/turnstile.js'
import { fp } from './module/afp.js';
import { puppeteerRealBrowser } from './module/old.js'
import AdblockerPlugin from "puppeteer-extra-plugin-adblocker";

export { puppeteerRealBrowser };

const adblocker = AdblockerPlugin({
    blockTrackers: true, // default: false
    useCache: true,
})
var global_target_status = true

function targetFilter({ target, skipTarget }) {

    if (global_target_status === false) {
        return true
    }
    var response = !!target.url()
    if (skipTarget.find(item => String(target.url()).indexOf(String(item) > -1))) {
        response = true
    }
    return response;
}

async function handleNewPage(page) {
    fp(page);
    return page
}

const setTarget = ({ status = true }) => {
    global_target_status = status
}

export const connect = ({ args = [], headless = 'auto', customConfig = {}, proxy = {}, skipTarget = [], fingerprint = true, turnstile = false, connectOption = {}, tf = true }) => {
    return new Promise(async (resolve, reject) => {

        global_target_status = tf

        const { chromeSession, cdpSession, chrome, xvfbsession } = await startSession({
            args: args,
            headless: headless,
            customConfig: customConfig,
            proxy: proxy
        })

        puppeteer.use(adblocker);   //added adblocker here

        console.log("Changes working");
        const browser = await puppeteer.connect({
            targetFilter: (target) => targetFilter({ target: target, skipTarget: skipTarget }),
            browserWSEndpoint: chromeSession.browserWSEndpoint,
            ...connectOption
        });

        var page = await browser.pages()

        page = page[0]

        if (proxy && proxy.username && proxy.username.length > 0) {
            await page.authenticate({ username: proxy.username, password: proxy.password });
        }

        if (fingerprint === true) {
            handleNewPage(page);
        }
        if (turnstile === true) {
            setSolveStatus({ status: true })
            autoSolve({ page: page, browser: browser })
        }

        await page.setUserAgent(chromeSession.agent);

        await page.setViewport({
            width: 1920,
            height: 1080
        });

        browser.on('disconnected', async () => {
            notice({
                message: 'Browser Disconnected',
                type: 'info'
            })
            setSolveStatus({ status: false })
            await closeSession({
                xvfbsession: xvfbsession,
                cdpSession: cdpSession,
                chrome: chrome
            })
        });

        browser.on('targetcreated', async target => {
            var newPage = await target.page();

            try {
                await newPage.setUserAgent(chromeSession.agent);
            } catch (err) {
                // console.log(err.message);
            }

            try {
                await newPage.setViewport({
                    width: 1920,
                    height: 1080
                });
            } catch (err) {
                // console.log(err.message);
            }

            if (newPage && fingerprint === true) {
                try {
                    handleNewPage(newPage);
                } catch (err) { }
            }

            if (turnstile === true) {
                autoSolve({ page: newPage })
            }
        });

        resolve({
            browser: browser,
            page: page,
            xvfbsession: xvfbsession,
            cdpSession: cdpSession,
            chrome: chrome,
            setTarget: setTarget
        })
    })
}

Well i edited it like this, but ads are still there, (the addblock added here part) It would be helpful if u could guide me through

eindrawan commented 2 months ago

I guess its because the page already created, thus the plugin is not registered properly on that initial page you can try add adblocker.onPageCreated(page) after var page = page[0]

you can also check my fork: https://github.com/eindrawan/puppeteer-real-browser/blob/main/src/index.js

@zfcsoftware may I create a PR for this?

zfcsoftware commented 2 months ago

I guess its because the page already created, thus the plugin is not registered properly on that initial page you can try add adblocker.onPageCreated(page) after var page = page[0]

you can also check my fork: https://github.com/eindrawan/puppeteer-real-browser/blob/main/src/index.js

@zfcsoftware may I create a PR for this?

Hi, thanks for your support. I think some plugins may get errors when used in this way. I will update Puppeteer to allow the user to submit.

sharpanimes commented 2 months ago

hey thanks for the replies, i made it work in my on way i added the following to the index page

export const connect = ({ args = [], headless = 'auto', customConfig = {}, proxy = {}, skipTarget = [], fingerprint = true, turnstile = false, connectOption = {}, tf = true }) => {
    return new Promise(async (resolve, reject) => {

        global_target_status = tf

        const adblocker = AdblockerPlugin({
            blockTrackers: true, // default: false
            useCache: false,
        })
        const blocker = await adblocker.getBlocker()

then returned the blocker, and used the plugins manual blocking like this after creating each page

page = await browser.newPage();
setTarget({ status: true });
blocker.enableBlockingInPage(page);
alextran317 commented 2 weeks ago

@sharpanimes Could you please display the full code snippet that you edited?

sharpanimes commented 1 week ago

@sharpanimes Could you please display the full code snippet that you edited?

Hey, sorry if my instructions were unclear. Step-1:The first thing you need is to install the https://www.npmjs.com/package/puppeteer-extra-plugin-adblocker plugin. Step-2:After that you need to modify the puppeter-real-browser's index page. I will link the modified index.js page https://pastebin.com/CKvFwAjU Step-3:- Now in your main project file where you are using puppeter do the following

const response = await connect({
        headless: 'auto',
        args: [],
        customConfig: {},
        skipTarget: [],
        fingerprint: true,
        turnstile: true,

        tf: false
    });

    const { browser, page, setTarget, blocker } = response;
Now whenever you are creating a page do it like below 
 page = await browser.newPage();
 setTarget({ status: true });
            blocker.enableBlockingInPage(page);
        Now your adblocker should be working
        Be sure to import adblocker in you project also to be on the safer side
alextran317 commented 1 week ago

@sharpanimes I followed your instructions and encountered an error: Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'D:\laragon\www\node_modules\puppeteer-real-browser\src\module\afp.js' imported from D:\laragon\www\node_modules\puppeteer-real-browser\src\index.js

sharpanimes commented 6 days ago

@sharpanimes I followed your instructions and encountered an error: Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'D:\laragon\www\node_modules\puppeteer-real-browser\src\module\afp.js' imported from D:\laragon\www\node_modules\puppeteer-real-browser\src\index.js

I think there are some changes in the latest version. I am using version ^1.2.11 . So try installing this version of puppeteer-real-browser

alextran317 commented 2 days ago

@sharpanimes I followed your instructions and encountered an error: Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'D:\laragon\www\node_modules\puppeteer-real-browser\src\module\afp.js' imported from D:\laragon\www\node_modules\puppeteer-real-browser\src\index.js

I think there are some changes in the latest version. I am using version ^1.2.11 . So try installing this version of puppeteer-real-browser

Thank bro