1.27.2 • Published 11 days ago

@cliqz/adblocker-playwright v1.27.2

Weekly downloads
212
License
MPL-2.0
Repository
github
Last release
11 days ago

Getting Started

Install: npm install --save @cliqz/adblocker-playwright.

Usage

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

Creating an instance of PlaywrightBlocker and start blocking ads!

import * as pw from 'playwright';
import { PlaywrightBlocker } from '@cliqz/adblocker-playwright';
import fetch from 'cross-fetch'; // required 'fetch'

const browser = await pw.firefox.launch({ headless: false });
const page = await browser.newPage();

PlaywrightBlocker.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 { PlaywrightBlocker } from '@cliqz/adblocker-playwright';

const blocker = PlaywrightBlocker.parse(fs.readFileSync('easylist.txt', 'utf-8'));

Fetching lists from URLs:

import { PlaywrightBlocker } from '@cliqz/adblocker-playwright';
import fetch from 'cross-fetch'; // required 'fetch'

const blocker = await PlaywrightBlocker.fromLists(fetch, [
  'https://easylist.to/easylist/easylist.txt'
]);

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

import { PlaywrightBlocker } from '@cliqz/adblocker-playwright';
import fetch from 'cross-fetch'; // required 'fetch'

let blocker = await PlaywrightBlocker.fromPrebuiltAdsOnly(fetch); // ads only
blocker = await PlaywrightBlocker.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 PlaywrightBlocker all over again, you can serialize it to a byte-array which you can store on disk for faster loading.

import * as pw from 'playwright';
import { PlaywrightBlocker } from '@cliqz/adblocker-playwright';
import fetch from 'cross-fetch'; // required 'fetch'
import { promises as fs } from 'fs'; // used for caching

const browser = await pw.firefox.launch({ headless: false });
const page = await browser.newPage();

PlaywrightBlocker.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 { PlaywrightBlocker } from '@cliqz/adblocker-playwright';
import fetch from 'cross-fetch'; // required 'fetch'

PlaywrightBlocker.fromPrebuiltAdsAndTracking(fetch).then((blocker) => {
  const buffer = blocker.serialize();
  const restoredBlocker = PlaywrightBlocker.deserialize(buffer);
  // `restoredBlocker` is deep-equal to `blocker`!
});
1.27.2

11 days ago

1.27.1

25 days ago

1.27.0

1 month ago

1.26.16

2 months ago

1.26.15

4 months ago

1.26.14

4 months ago

1.26.13

4 months ago

1.26.11

6 months ago

1.26.10

6 months ago

1.26.12

6 months ago

1.26.7

8 months ago

1.26.8

7 months ago

1.26.9

6 months ago

1.26.6

11 months ago

1.26.3

1 year ago

1.26.4

1 year ago

1.26.5

1 year ago

1.26.2

1 year ago

1.26.1

1 year ago

1.26.0

1 year ago

1.25.2

1 year ago

1.25.0

2 years ago

1.25.1

2 years ago

1.24.0

2 years ago

1.23.9

2 years ago

1.23.8

2 years ago

1.23.7

2 years ago

1.23.2

2 years ago

1.23.3

2 years ago

1.23.6

2 years ago

1.23.4

2 years ago

1.23.5

2 years ago

1.23.1

2 years ago

1.23.0

3 years ago

1.22.7

3 years ago

1.22.6

3 years ago

1.22.5

3 years ago

1.22.4

3 years ago

1.22.3

3 years ago

1.22.2

3 years ago

1.22.1

3 years ago

1.22.0

3 years ago

1.20.5

3 years ago

1.20.6

3 years ago

1.20.4

3 years ago

1.20.3

3 years ago

1.20.2

3 years ago

1.20.1

3 years ago

1.20.0

3 years ago

1.19.0

3 years ago

1.18.8

3 years ago

1.18.7

3 years ago

1.18.6

3 years ago

1.18.5

3 years ago

1.18.4

4 years ago

1.18.3

4 years ago

1.18.2

4 years ago

1.18.1

4 years ago

1.18.0

4 years ago

1.17.0

4 years ago

1.16.1

4 years ago

1.16.0

4 years ago