Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

@cliqz/adblocker-puppeteer

1.9.1 • Public • Published

@cliqz/adblocker-puppeteer

@cliqz/adblocker enhanced with convenient Puppeteer wrapper

Features

  • extremely efficient adblocker (both in memory usage and raw speed)
  • pure JavaScript implementation
  • first-class support for Node.js, WebExtension, Electron and Puppeteer.
  • efficiently blocks all types of ads and tracking
  • supports cosmetics and scriptlet injection
  • small and minimal (only 64KB minified and gzipped)
  • support most filters: Easylist and uBlock Origin formats

The library provides all necessary building blocks to create a powerful and efficient content-blocker and gives full flexibility as to which lists should be used and how they should be fetched or updated. Being a pure JavaScript library it does not make any assumption regarding the environment it will run in (apart from the availability of a JavaScript engine) and is trivial to include in any new project. It can also be used as a building block for tooling.

For more details, check-out the following specialized guides:

Getting Started

Install: npm install --save @cliqz/adblocker-puppeteer

Usage

For a complete example check-out: @cliqz/adblocker-puppeteer-example

Creating an instance of PuppeteerBlocker and start blocking ads!

import puppeteer from 'puppeteer';
import { PuppeteerBlocker } from '@cliqz/adblocker-puppeteer';
import fetch from 'cross-fetch'; // required 'fetch'
 
const browser = await puppeteer.launch();
const page = await browser.newPage();
 
PuppeteerBlocker.fromPrebuiltAdsAndTracking(fetch).then((blocker) => {
  blocker.enableBlockingInPage(page);
});

You are ready to block ads!

There are other ways you can create an instance of the blocking engine to start blocking ads.

If you already have filters locally:

import { PuppeteerBlocker } from '@cliqz/adblocker-puppeteer';
 
const blocker = PuppeteerBlocker.parse(fs.readFileSync('easylist.txt', 'utf-8'));

Fetching lists from URLs:

import { PuppeteerBlocker } from '@cliqz/adblocker-puppeteer';
import fetch from 'cross-fetch'; // required 'fetch'
 
const blocker = await PuppeteerBlocker.fromLists(fetch, [
  'https://easylist.to/easylist/easylist.txt'
]);

Use ready-made configs to block ads and optionally trackers:

import { PuppeteerBlocker } from '@cliqz/adblocker-puppeteer';
import fetch from 'cross-fetch'; // required 'fetch'
 
let blocker = await PuppeteerBlocker.fromPrebuiltAdsOnly(fetch); // ads only
blocker = await PuppeteerBlocker.fromPrebuiltAdsAndTracking(fetch); // ads and tracking

Disabling Blocker in page

To stop blocking ads in a page:

await blocker.disableBlockingInPage(page);

Caching Blocker using Serialization

To avoid having to create the same instance of PuppeteerBlocker all over again, you can serialize it to a byte-array which you can store on disk for faster loading.

import puppeteer from 'puppeteer';
import { PuppeteerBlocker } from '@cliqz/adblocker-puppeteer';
import fetch from 'cross-fetch'; // required 'fetch'
import { promises as fs } from 'fs'; // used for caching
 
const browser = await puppeteer.launch();
const page = await browser.newPage();
 
PuppeteerBlocker.fromPrebuiltAdsAndTracking(fetch, {
  path: 'engine.bin',
  read: fs.readFile,
  write: fs.writeFile,
}).then((blocker) => {
  blocker.enableBlockingInPage(page);
});

Or you can do this manually to control the way caching is done:

import { PuppeteerBlocker } from '@cliqz/adblocker-puppeteer';
import fetch from 'cross-fetch'; // required 'fetch'
 
PuppeteerBlocker.fromPrebuiltAdsAndTracking(fetch).then((blocker) => {
  const buffer = blocker.serialize();
  const restoredBlocker = PuppeteerBlocker.deserialize(buffer);
  // `restoredBlocker` is deep-equal to `blocker`!
});

Keywords

none

Install

npm i @cliqz/adblocker-puppeteer

DownloadsWeekly Downloads

1,387

Version

1.9.1

License

MPL-2.0

Unpacked Size

390 kB

Total Files

12

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar