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.
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 installThe declarativeNetRequest rulesets generated by this script require Chrome >= 101 to function correctly. (See this announcement for context.)
Create a declarativeNetRequest ruleset output.json from an Adblock Plus
filter list input.txt:
node abp2dnr.js < input.txt > output.jsonBehind 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));Unit tests live in the tests/ directory. You can run them by typing this command:
npm testYou can lint the code by typing this command:
npm run lint