@cliqz/adblocker-electron
TypeScript icon, indicating that this package has built-in type declarations

1.33.2 • Public • Published

Electron 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 Ghostery on Twitter Dependabot License Badge LGTM Badge


Getting Started

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

Usage

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

Creating an instance of ElectronBlocker and start blocking ads!

import { ElectronBlocker } from '@cliqz/adblocker-electron';
import fetch from 'cross-fetch'; // required 'fetch'

ElectronBlocker.fromPrebuiltAdsAndTracking(fetch).then((blocker) => {
  blocker.enableBlockingInSession(session.defaultSession);
});

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 { ElectronBlocker } from '@cliqz/adblocker-electron';

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

Fetching lists from URLs:

import { ElectronBlocker } from '@cliqz/adblocker-electron';
import fetch from 'cross-fetch'; // required 'fetch'

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

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

import { ElectronBlocker } from '@cliqz/adblocker-electron';
import fetch from 'cross-fetch'; // required 'fetch'

let blocker = await ElectronBlocker.fromPrebuiltAdsOnly(fetch); // ads only
blocker = await ElectronBlocker.fromPrebuiltAdsAndTracking(fetch); // ads and tracking

Disabling Blocker in session

To stop blocking ads in a session:

blocker.disableBlockingInSession(session.defaultSession);

Caching Blocker using Serialization

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

import { ElectronBlocker } from '@cliqz/adblocker-electron';
import fetch from 'cross-fetch'; // required 'fetch'
import { promises as fs } from 'fs'; // used for caching

ElectronBlocker.fromPrebuiltAdsAndTracking(fetch, {
  path: 'engine.bin',
  read: fs.readFile,
  write: fs.writeFile,
}).then((blocker) => {
  blocker.enableBlockingInSession(session.defaultSession);
});

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

import { ElectronBlocker } from '@cliqz/adblocker-electron';
import fetch from 'cross-fetch'; // required 'fetch'

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

Readme

Keywords

none

Package Sidebar

Install

npm i @cliqz/adblocker-electron

Weekly Downloads

596

Version

1.33.2

License

MPL-2.0

Unpacked Size

93.2 kB

Total Files

23

Last publish

Collaborators

  • chrmod
  • remusao