AGTree is a universal adblock filter list parser. It supports all syntaxes
currently in use: AdGuard, uBlock Origin and AdBlock / Adblock Plus.
Usage
AGTree provides a powerful, error-tolerant parser for all kinds of ad blocking
rules. It fully supports AdGuard, uBlock Origin and Adblock Plus syntaxes, and
provides a high-detail AST for all rules.
Basically, the parser API has two main parts:
Parser: parsing rules (string → AST)
Generator: serialization of ASTs (AST → string)
Parsing rules
You can parse individual rules using the RuleParser class. It has a parse
method that takes a rule string and returns an AST. For example, this code:
As you can see, this AST is very detailed and contains all the information about
the rule: syntax, category, exception, modifiers, node locations, and so on.
Locations are especially useful for linters, since they allow you to point to
the exact place in the rule where the error occurred.
And this code:
RuleParser.generate(ast);
will generate the adblock rule from the AST (serialization):
/ads.js^$script
Please keep in mind that the parser omits unnecessary whitespaces, so the
generated rule may not match with the original rule character by character.
Only the formatting can change, the rule itself remains the same. You can
pass any rule to the parser, it automatically determines the type and category
of the rule. If the rule is syntactically incorrect, the parser will throw an
error.
Parsing filter lists
You can also parse complete filter lists using the FilterListParser class.
It works the same way as the RuleParser class. Here is an example of parsing
EasyList and generating it back:
import { FilterListParser } from"@adguard/agtree";
import { writeFile } from"fs/promises";
// Requires installing "node-fetch" packageimport fetch from"node-fetch";
// Download EasyListconst easyList = await (
awaitfetch("https://easylist.to/easylist/easylist.txt")
).text();
// Or read it from file// const easyList = await readFile('easylist.txt', 'utf-8');// Parse EasyList to AST. By default, parser is very tolerant,// if it can't parse some rules, it will just mark them as "raw".// If you want to disable this behavior, you can pass the second// argument as "false" to the "parse" method, like this:// const ast = FilterListParser.parse(easyList, false);const ast = FilterListParser.parse(easyList);
// Generate filter list from filter list ASTconst easyListGenerated = FilterListParser.generate(ast);
// Write generated filter list to fileawaitwriteFile("easylist-generated.txt", easyListGenerated);
Development & Contribution
Please read the CONTRIBUTING.md file for details on how to
contribute to this project.
Ideas & Questions
If you have any questions or ideas for new features, please open an issue or a
discussion. We will be happy to discuss it with you.
License
AGTree is licensed under the MIT License. See the LICENSE file for
details.
References
Here are some useful links to help you write adblock rules. This list is not
exhaustive, so if you know any other useful resources, please let us know.
The npm package @adguard/agtree receives a total of 1,224 weekly downloads. As such, @adguard/agtree popularity was classified as popular.
We found that @adguard/agtree demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 0 open source maintainers collaborating on the project.
Package last updated on 24 May 2023
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.
TC39 met in Seattle and advanced 9 JavaScript proposals, including three to Stage 4, introducing new features and enhancements for a future ECMAScript release.
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.