@adguard/agtree 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +336 -0
- package/dist/agtree.cjs +4985 -0
- package/dist/agtree.d.ts +1717 -0
- package/dist/agtree.esm.js +4963 -0
- package/dist/agtree.iife.min.js +7 -0
- package/dist/agtree.umd.min.js +7 -0
- package/dist/build.txt +1 -0
- package/package.json +87 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# AGTree Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog][keepachangelog], and this project
|
|
6
|
+
adheres to [Semantic Versioning][semver].
|
|
7
|
+
|
|
8
|
+
[keepachangelog]: https://keepachangelog.com/en/1.0.0/
|
|
9
|
+
[semver]: https://semver.org/spec/v2.0.0.html
|
|
10
|
+
|
|
11
|
+
## [Unreleased]
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- Migrated parser from AGLint to a separate package.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) AdGuard Software Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<picture>
|
|
5
|
+
<source
|
|
6
|
+
media="(prefers-color-scheme: dark)"
|
|
7
|
+
srcset="
|
|
8
|
+
https://cdn.adtidy.org/website/github.com/AGTree/agtree_darkmode.svg
|
|
9
|
+
"
|
|
10
|
+
/>
|
|
11
|
+
<img
|
|
12
|
+
alt="AGTree"
|
|
13
|
+
src="https://cdn.adtidy.org/website/github.com/AGTree/agtree_lightmode.svg"
|
|
14
|
+
width="350px"
|
|
15
|
+
/>
|
|
16
|
+
</picture>
|
|
17
|
+
</p>
|
|
18
|
+
<h3 align="center">Universal adblock filter list parser</h3>
|
|
19
|
+
<p align="center">Supported syntaxes:</p>
|
|
20
|
+
<p align="center">
|
|
21
|
+
<a href="https://adguard.com"
|
|
22
|
+
><img
|
|
23
|
+
src="https://cdn.adguard.com/website/github.com/AGLint/adg_logo.svg"
|
|
24
|
+
width="14px"
|
|
25
|
+
/>
|
|
26
|
+
AdGuard</a
|
|
27
|
+
>
|
|
28
|
+
|
|
|
29
|
+
<a href="https://github.com/gorhill/uBlock"
|
|
30
|
+
><img
|
|
31
|
+
src="https://cdn.adguard.com/website/github.com/AGLint/ubo_logo.svg"
|
|
32
|
+
width="14px"
|
|
33
|
+
/>
|
|
34
|
+
uBlock Origin</a
|
|
35
|
+
>
|
|
36
|
+
|
|
|
37
|
+
<a href="https://getadblock.com"
|
|
38
|
+
><img
|
|
39
|
+
src="https://cdn.adguard.com/website/github.com/AGLint/ab_logo.svg"
|
|
40
|
+
width="14px"
|
|
41
|
+
/>
|
|
42
|
+
AdBlock</a
|
|
43
|
+
>
|
|
44
|
+
|
|
|
45
|
+
<a href="https://adblockplus.org"
|
|
46
|
+
><img
|
|
47
|
+
src="https://cdn.adguard.com/website/github.com/AGLint/abp_logo.svg"
|
|
48
|
+
width="14px"
|
|
49
|
+
/>
|
|
50
|
+
Adblock Plus</a
|
|
51
|
+
>
|
|
52
|
+
</p>
|
|
53
|
+
|
|
54
|
+
<p align="center">
|
|
55
|
+
<a href="https://www.npmjs.com/package/@adguard/agtree"
|
|
56
|
+
><img src="https://img.shields.io/npm/v/@adguard/agtree" alt="NPM version"
|
|
57
|
+
/></a>
|
|
58
|
+
<a href="https://www.npmjs.com/package/@adguard/agtree"
|
|
59
|
+
><img
|
|
60
|
+
src="https://img.shields.io/npm/dm/@adguard/agtree"
|
|
61
|
+
alt="NPM Downloads"
|
|
62
|
+
/></a>
|
|
63
|
+
<a href="https://github.com/AdguardTeam/AGTree/blob/master/LICENSE"
|
|
64
|
+
><img src="https://img.shields.io/npm/l/@adguard/agtree" alt="License"
|
|
65
|
+
/></a>
|
|
66
|
+
</p>
|
|
67
|
+
|
|
68
|
+
Table of Contents:
|
|
69
|
+
|
|
70
|
+
- [Introduction](#introduction)
|
|
71
|
+
- [Usage](#usage)
|
|
72
|
+
- [Parsing rules](#parsing-rules)
|
|
73
|
+
- [Parsing filter lists](#parsing-filter-lists)
|
|
74
|
+
- [Development \& Contribution](#development--contribution)
|
|
75
|
+
- [Ideas \& Questions](#ideas--questions)
|
|
76
|
+
- [License](#license)
|
|
77
|
+
- [References](#references)
|
|
78
|
+
|
|
79
|
+
## Introduction
|
|
80
|
+
|
|
81
|
+
AGTree is a universal adblock filter list parser. It supports all syntaxes
|
|
82
|
+
currently in use: AdGuard, uBlock Origin and AdBlock / Adblock Plus.
|
|
83
|
+
|
|
84
|
+
## Usage
|
|
85
|
+
|
|
86
|
+
AGTree provides a powerful, error-tolerant parser for all kinds of ad blocking
|
|
87
|
+
rules. It fully supports AdGuard, uBlock Origin and Adblock Plus syntaxes, and
|
|
88
|
+
provides a high-detail AST for all rules.
|
|
89
|
+
|
|
90
|
+
Basically, the parser API has two main parts:
|
|
91
|
+
|
|
92
|
+
- Parser: parsing rules (string → AST)
|
|
93
|
+
- Generator: serialization of ASTs (AST → string)
|
|
94
|
+
|
|
95
|
+
### Parsing rules
|
|
96
|
+
|
|
97
|
+
You can parse individual rules using the `RuleParser` class. It has a `parse`
|
|
98
|
+
method that takes a rule string and returns an AST. For example, this code:
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { RuleParser } from "@adguard/agtree";
|
|
102
|
+
|
|
103
|
+
// RuleParser automatically determines the rule type
|
|
104
|
+
const ast = RuleParser.parse("/ads.js^$script");
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
will gives you this AST:
|
|
108
|
+
|
|
109
|
+
```json
|
|
110
|
+
{
|
|
111
|
+
"type": "NetworkRule",
|
|
112
|
+
"category": "Network",
|
|
113
|
+
"syntax": "Common",
|
|
114
|
+
"exception": false,
|
|
115
|
+
"pattern": {
|
|
116
|
+
"type": "Value",
|
|
117
|
+
"value": "/ads.js^"
|
|
118
|
+
},
|
|
119
|
+
"modifiers": {
|
|
120
|
+
"type": "ModifierList",
|
|
121
|
+
"children": [
|
|
122
|
+
{
|
|
123
|
+
"type": "Modifier",
|
|
124
|
+
"modifier": {
|
|
125
|
+
"type": "Value",
|
|
126
|
+
"value": "script"
|
|
127
|
+
},
|
|
128
|
+
"exception": false
|
|
129
|
+
}
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
<details>
|
|
136
|
+
|
|
137
|
+
<summary>Show full AST (with locations)</summary>
|
|
138
|
+
|
|
139
|
+
```json
|
|
140
|
+
{
|
|
141
|
+
"type": "NetworkRule",
|
|
142
|
+
"loc": {
|
|
143
|
+
"start": {
|
|
144
|
+
"offset": 0,
|
|
145
|
+
"line": 1,
|
|
146
|
+
"column": 1
|
|
147
|
+
},
|
|
148
|
+
"end": {
|
|
149
|
+
"offset": 15,
|
|
150
|
+
"line": 1,
|
|
151
|
+
"column": 16
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
"raws": {
|
|
155
|
+
"text": "/ads.js^$script"
|
|
156
|
+
},
|
|
157
|
+
"category": "Network",
|
|
158
|
+
"syntax": "Common",
|
|
159
|
+
"exception": false,
|
|
160
|
+
"pattern": {
|
|
161
|
+
"type": "Value",
|
|
162
|
+
"loc": {
|
|
163
|
+
"start": {
|
|
164
|
+
"offset": 0,
|
|
165
|
+
"line": 1,
|
|
166
|
+
"column": 1
|
|
167
|
+
},
|
|
168
|
+
"end": {
|
|
169
|
+
"offset": 8,
|
|
170
|
+
"line": 1,
|
|
171
|
+
"column": 9
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
"value": "/ads.js^"
|
|
175
|
+
},
|
|
176
|
+
"modifiers": {
|
|
177
|
+
"type": "ModifierList",
|
|
178
|
+
"loc": {
|
|
179
|
+
"start": {
|
|
180
|
+
"offset": 9,
|
|
181
|
+
"line": 1,
|
|
182
|
+
"column": 10
|
|
183
|
+
},
|
|
184
|
+
"end": {
|
|
185
|
+
"offset": 15,
|
|
186
|
+
"line": 1,
|
|
187
|
+
"column": 16
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
"children": [
|
|
191
|
+
{
|
|
192
|
+
"type": "Modifier",
|
|
193
|
+
"loc": {
|
|
194
|
+
"start": {
|
|
195
|
+
"offset": 9,
|
|
196
|
+
"line": 1,
|
|
197
|
+
"column": 10
|
|
198
|
+
},
|
|
199
|
+
"end": {
|
|
200
|
+
"offset": 15,
|
|
201
|
+
"line": 1,
|
|
202
|
+
"column": 16
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
"modifier": {
|
|
206
|
+
"type": "Value",
|
|
207
|
+
"loc": {
|
|
208
|
+
"start": {
|
|
209
|
+
"offset": 9,
|
|
210
|
+
"line": 1,
|
|
211
|
+
"column": 10
|
|
212
|
+
},
|
|
213
|
+
"end": {
|
|
214
|
+
"offset": 15,
|
|
215
|
+
"line": 1,
|
|
216
|
+
"column": 16
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
"value": "script"
|
|
220
|
+
},
|
|
221
|
+
"exception": false
|
|
222
|
+
}
|
|
223
|
+
]
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
</details>
|
|
229
|
+
|
|
230
|
+
As you can see, this AST is very detailed and contains all the information about
|
|
231
|
+
the rule: syntax, category, exception, modifiers, node locations, and so on.
|
|
232
|
+
Locations are especially useful for linters, since they allow you to point to
|
|
233
|
+
the exact place in the rule where the error occurred.
|
|
234
|
+
|
|
235
|
+
And this code:
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
RuleParser.generate(ast);
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
will generate the adblock rule from the AST (serialization):
|
|
242
|
+
|
|
243
|
+
```adblock
|
|
244
|
+
/ads.js^$script
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Please keep in mind that the parser omits unnecessary whitespaces, so the
|
|
248
|
+
generated rule may not match with the original rule character by character.
|
|
249
|
+
Only the formatting can change, the rule itself remains the same. You can
|
|
250
|
+
pass any rule to the parser, it automatically determines the type and category
|
|
251
|
+
of the rule. If the rule is syntactically incorrect, the parser will throw an
|
|
252
|
+
error.
|
|
253
|
+
|
|
254
|
+
### Parsing filter lists
|
|
255
|
+
|
|
256
|
+
You can also parse complete filter lists using the `FilterListParser` class.
|
|
257
|
+
It works the same way as the `RuleParser` class. Here is an example of parsing
|
|
258
|
+
[EasyList](https://easylist.to/easylist/easylist.txt) and generating it back:
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
import { FilterListParser } from "@adguard/agtree";
|
|
262
|
+
import { writeFile } from "fs/promises";
|
|
263
|
+
// Requires installing "node-fetch" package
|
|
264
|
+
import fetch from "node-fetch";
|
|
265
|
+
|
|
266
|
+
// Download EasyList
|
|
267
|
+
const easyList = await (
|
|
268
|
+
await fetch("https://easylist.to/easylist/easylist.txt")
|
|
269
|
+
).text();
|
|
270
|
+
|
|
271
|
+
// Or read it from file
|
|
272
|
+
// const easyList = await readFile('easylist.txt', 'utf-8');
|
|
273
|
+
|
|
274
|
+
// Parse EasyList to AST. By default, parser is very tolerant,
|
|
275
|
+
// if it can't parse some rules, it will just mark them as "raw".
|
|
276
|
+
// If you want to disable this behavior, you can pass the second
|
|
277
|
+
// argument as "false" to the "parse" method, like this:
|
|
278
|
+
// const ast = FilterListParser.parse(easyList, false);
|
|
279
|
+
const ast = FilterListParser.parse(easyList);
|
|
280
|
+
|
|
281
|
+
// Generate filter list from filter list AST
|
|
282
|
+
const easyListGenerated = FilterListParser.generate(ast);
|
|
283
|
+
|
|
284
|
+
// Write generated filter list to file
|
|
285
|
+
await writeFile("easylist-generated.txt", easyListGenerated);
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## Development & Contribution
|
|
289
|
+
|
|
290
|
+
Please read the [CONTRIBUTING.md](CONTRIBUTING.md) file for details on how to
|
|
291
|
+
contribute to this project.
|
|
292
|
+
|
|
293
|
+
## Ideas & Questions
|
|
294
|
+
|
|
295
|
+
If you have any questions or ideas for new features, please open an issue or a
|
|
296
|
+
discussion. We will be happy to discuss it with you.
|
|
297
|
+
|
|
298
|
+
## License
|
|
299
|
+
|
|
300
|
+
AGTree is licensed under the MIT License. See the [LICENSE](LICENSE) file for
|
|
301
|
+
details.
|
|
302
|
+
|
|
303
|
+
## References
|
|
304
|
+
|
|
305
|
+
Here are some useful links to help you write adblock rules. This list is not
|
|
306
|
+
exhaustive, so if you know any other useful resources, please let us know.
|
|
307
|
+
|
|
308
|
+
- Syntax documentation:
|
|
309
|
+
- [AdGuard: _How to create your own ad filters_][adg-filters]
|
|
310
|
+
- [uBlock Origin: _Static filter syntax_][ubo-filters]
|
|
311
|
+
- [Adblock Plus: _How to write filters_][abp-filters]
|
|
312
|
+
- Extended CSS documentation:
|
|
313
|
+
- [MDN: _CSS selectors_][mdn-css-selectors]
|
|
314
|
+
- [AdGuard: _Extended CSS capabilities_][adg-ext-css]
|
|
315
|
+
- [uBlock Origin: _Procedural cosmetic filters_][ubo-procedural]
|
|
316
|
+
- [Adblock Plus: _Extended CSS selectors_][abp-ext-css]
|
|
317
|
+
- Scriptlets:
|
|
318
|
+
- [AdGuard scriptlets][adg-scriptlets]
|
|
319
|
+
- [uBlock Origin scriptlets][ubo-scriptlets]
|
|
320
|
+
- [Adblock Plus snippets][abp-snippets]
|
|
321
|
+
- Third party libraries:
|
|
322
|
+
- [CSSTree docs][css-tree-docs]
|
|
323
|
+
- [AdGuard's compatibility table][adg-compatibility-table]
|
|
324
|
+
|
|
325
|
+
[abp-ext-css]: https://help.eyeo.com/adblockplus/how-to-write-filters#elemhide-emulation
|
|
326
|
+
[abp-filters]: https://help.eyeo.com/adblockplus/how-to-write-filters
|
|
327
|
+
[abp-snippets]: https://help.eyeo.com/adblockplus/snippet-filters-tutorial#snippets-ref
|
|
328
|
+
[adg-compatibility-table]: https://github.com/AdguardTeam/Scriptlets/blob/master/wiki/compatibility-table.md
|
|
329
|
+
[adg-ext-css]: https://github.com/AdguardTeam/ExtendedCss/blob/master/README.md
|
|
330
|
+
[adg-filters]: https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters
|
|
331
|
+
[adg-scriptlets]: https://github.com/AdguardTeam/Scriptlets/blob/master/wiki/about-scriptlets.md#scriptlets
|
|
332
|
+
[css-tree-docs]: https://github.com/csstree/csstree/tree/master/docs
|
|
333
|
+
[mdn-css-selectors]: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors
|
|
334
|
+
[ubo-filters]: https://github.com/gorhill/uBlock/wiki/Static-filter-syntax
|
|
335
|
+
[ubo-procedural]: https://github.com/gorhill/uBlock/wiki/Procedural-cosmetic-filters
|
|
336
|
+
[ubo-scriptlets]: https://github.com/gorhill/uBlock/wiki/Resources-Library#available-general-purpose-scriptlets
|