adblocker icon indicating copy to clipboard operation
adblocker copied to clipboard

Adding multiple puppeteer blockers on single page object

Open teammakdi opened this issue 1 year ago • 2 comments

Does adding multiple puppeteer blockers on single puppeteer page object work

Example both

await PuppeteerBlocker.fromLists(fetch, [
    'https://secure.fanboy.co.nz/fanboy-annoyance.txt'
]).then(async (blocker) => {
    await blocker.enableBlockingInPage(page);
});

await PuppeteerBlocker.fromPrebuiltAdsOnly(fetch).then(async (blocker) => {
    await blocker.enableBlockingInPage(page);
});

Not sure if its similar issue as https://github.com/ghostery/adblocker/issues/4133

teammakdi avatar Jul 31 '24 11:07 teammakdi

Hi @teammakdi ,

We don't support putting multiple blocker instances into the single page. In other words, we don't know about the side-effects of doing so.

Also, the "context" created by "PuppeteerBlocker" for the "page" will be discarded when you bind another blocker to the "page".

Best

seia-soto avatar Aug 01 '24 06:08 seia-soto

@seia-soto Now since https://github.com/ghostery/adblocker/issues/4133 has been merged

import puppeteer from 'puppeteer'
import fetch from 'cross-fetch'
import { PuppeteerBlocker } from '@cliqz/adblocker-puppeteer'

const url = 'https://www.rightmove.co.uk'

const browser = await puppeteer.launch()
const page = await browser.newPage()

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

const adsAndTrackingEngine = await PuppeteerBlocker.fromPrebuiltAdsAndTracking(fetch);

const cookieEngine = await PuppeteerBlocker.fromLists(fetch, [
  'https://secure.fanboy.co.nz/fanboy-cookiemonster.txt',
  'https://raw.githubusercontent.com/cliqz-oss/adblocker/master/packages/adblocker/assets/easylist/easylist-cookie.txt',
  'https://raw.githubusercontent.com/cliqz-oss/adblocker/master/packages/adblocker/assets/ublock-origin/annoyances-cookies.txt'
])

const mergedEngine = PuppeteerBlocker.merge([adsAndTrackingEngine, cookieEngine]);

await mergedEngine.enableBlockingInPage(page)

await page.goto(url)

await page.screenshot({
  path: 'screenshot.jpg'
})

await browser.close()

should work as expected right.

teammakdi avatar Aug 05 '24 05:08 teammakdi

Hi there,

Sorry for the late reply. I believe you can bring all the source URLs from our codebase: https://github.com/ghostery/adblocker/blob/master/packages/adblocker/src/fetch.ts#L70.

Thanks for the interest towards our library. We also have a discussion section where everyone can better involve and giving a separate space for questions.

Best

seia-soto avatar Jul 08 '25 07:07 seia-soto