Skip to content
/ abp2dnr Public

Script to produce chrome.declarativeNetRequest rules from an Adblock Plus filter list. Based upon abp2blocklist.

License

Notifications You must be signed in to change notification settings

kzar/abp2dnr

Repository files navigation

abp2dnr

This is a script to convert Adblock Plus filter lists to chrome.declarativeNetRequest rulesets as far as is possible.

See also Chromium's built-in filter list converter.

Requirements

Before you begin, make sure to install Node.js version 16 or higher.

Then the required packages can be installed via Git and npm:

npm install

The declarativeNetRequest rulesets generated by this script require Chrome >= 101 to function correctly. (See this announcement for context.)

Usage

Command line interface

Create a declarativeNetRequest ruleset output.json from an Adblock Plus filter list input.txt:

node abp2dnr.js < input.txt > output.json

JavaScript API

Behind that, there's a JavaScript API which the command line interface uses. It works something like this:

const {convertFilter, compressRules} = require("./lib/abp2dnr");

let rules = []

// Convert the filters to declarativeNetRequest rules.
for (let filter of filters)
  rules.push(await convertFilter(filter));

// Optionally combine rules where possible.
rules = compressRules(rules)

// Assign the rules an ID (must not be before compressRules()).
let id = 1;
for (let rule of rules)
  rule.id = id++;

It's important to note that convertFilter expects a Filter Object and not a string containing the filter's text. To parse filter text you'll need to do something like this first:

const {Filter} = require("adblockpluscore/lib/filterClasses");
Filter.fromText(Filter.normalize(filterText));

Tests

Unit tests live in the tests/ directory. You can run them by typing this command:

npm test

Linting

You can lint the code by typing this command:

npm run lint

About

Script to produce chrome.declarativeNetRequest rules from an Adblock Plus filter list. Based upon abp2blocklist.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published