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 exceptions (allowlist/whitelist) in adblocker #824

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/puppeteer-extra-plugin-adblocker/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,35 @@ test('will block ads', async t => {

await browser.close()
})

test("will adhere to allowlist rules", async t => {
const puppeteer = require('puppeteer-extra')
const adblockerPlugin = AdblockerPlugin({
allowlist: [
// Allow everything
/(.*?)/,
]
})

puppeteer.use(adblockerPlugin)

const browser = await puppeteer.launch({
args: PUPPETEER_ARGS,
headless: true
})

const blocker = await adblockerPlugin.getBlocker()

const page = await browser.newPage()

let blockedRequests = 0
blocker.on('request-blocked', (req) => {
blockedRequests += 1
})

await page.goto("https://www.google.com/search?q=rent%20a%20car", { waitUntil: 'networkidle0' })

t.is(blockedRequests, 0)

await browser.close();
})
20 changes: 19 additions & 1 deletion packages/puppeteer-extra-plugin-adblocker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { promises as fs } from 'fs'
import os from 'os'
import path from 'path'

import { PuppeteerBlocker } from '@cliqz/adblocker-puppeteer'
import { NetworkFilter, PuppeteerBlocker } from '@cliqz/adblocker-puppeteer'
import fetch from 'node-fetch'
import { PuppeteerExtraPlugin } from 'puppeteer-extra-plugin'

Expand All @@ -11,6 +11,8 @@ const engineCacheFilename = `${pkg.name}-${pkg.version}-engine.bin`

/** Available plugin options */
export interface PluginOptions {
/** Optional custom list of sites to allow. Default: [] */
allowlist: RegExp[]
/** Whether or not to block trackers (in addition to ads). Default: false */
blockTrackers: boolean
/** Whether or not to block trackers and other annoyances, including cookie
Expand Down Expand Up @@ -41,6 +43,7 @@ export class PuppeteerExtraPluginAdblocker extends PuppeteerExtraPlugin {

get defaults(): PluginOptions {
return {
allowlist: [],
blockTrackers: false,
blockTrackersAndAnnoyances: false,
useCache: true,
Expand All @@ -67,6 +70,18 @@ export class PuppeteerExtraPluginAdblocker extends PuppeteerExtraPlugin {
await fs.writeFile(this.engineCacheFile, blocker.serialize())
}

/**
* Return a list of exceptions to the blocking rules.
*/
private getExceptions(): NetworkFilter[] {
let list: RegExp[] = this.opts.allowlist || [];
return list
// Turn the list of regexps into an array of `NetworkFilter` instances.
.map((regex) => NetworkFilter.parse("@@" + regex.toString()))
// Remove any invalid filters.
.filter((filter): filter is NetworkFilter => filter !== undefined)
}

/**
* Initialize instance of `PuppeteerBlocker` from cache if possible.
* Otherwise, it throws and we will try to initialize it from remote instead.
Expand Down Expand Up @@ -118,6 +133,9 @@ export class PuppeteerExtraPluginAdblocker extends PuppeteerExtraPlugin {
await this.persistToCache(this.blocker)
}
}
this.blocker.update({
newNetworkFilters: this.getExceptions(),
})
return this.blocker
}

Expand Down