Socket
Socket
Sign inDemoInstall

@cliqz/adblocker-playwright

Package Overview
Dependencies
19
Maintainers
2
Versions
72
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @cliqz/adblocker-playwright

Cliqz adblocker Playwright wrapper


Version published
Weekly downloads
2.2K
decreased by-24.76%
Maintainers
2
Install size
12.3 MB
Created
Weekly downloads
 

Readme

Source

Playwright Adblocker

Efficient · Minimal · JavaScript · TypeScript · uBlock Origin- and Easylist-compatible
Node.js · Puppeteer · Electron · WebExtension

Github Actions Build Status Github Actions Assets Status Blazing Fast npm version weekly downloads from npm
code style: prettier Follow Cliqz on Twitter Dependabot License Badge LGTM Badge


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`!
});

FAQs

Last updated on 10 Jun 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc