Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for adblock and other puppeter extra plugins #32

Open
sharpanimes opened this issue Apr 5, 2024 · 5 comments
Open

Support for adblock and other puppeter extra plugins #32

sharpanimes opened this issue Apr 5, 2024 · 5 comments

Comments

@sharpanimes
Copy link

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

@zfcsoftware
Copy link
Owner

zfcsoftware commented Apr 5, 2024

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
Copy link
Author

sharpanimes commented Apr 5, 2024

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
Copy link

eindrawan commented Apr 16, 2024

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
Copy link
Owner

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
Copy link
Author

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants