Utility to create AdGuard DNR rulesets for mv3 extensions
Utility to load prebuilt AdGuard DNR rulesets for mv3 extensions.
The list of available filters can be found by filters in the metadata.
- Dnr-rulesets
- Basic usage
- CLI
- API
- Output structure
- Utils
- getVersion
- getVersionTimestampMs
- Advanced usage
- Injecting rulesets to the manifest object
- Example
- Included filter lists
- Ad Blocking
- AdGuard Base filter
- AdGuard Mobile Ads filter
- AdGuard Quick Fixes filter
- Privacy
- AdGuard Tracking Protection filter
- AdGuard URL Tracking filter
- Social Widgets
- AdGuard Social Media filter
- Annoyances
- AdGuard Cookie Notices filter
- AdGuard Popups filter
- AdGuard Mobile App Banners filter
- AdGuard Other Annoyances filter
- AdGuard Widgets filter
- Security
- Online Malicious URL Blocklist
- Phishing URL Blocklist
- Scam Blocklist by DurableNapkin
- uBlock Origin – Badware risks
- Other
- AdGuard Experimental filter
- Filter unblocking search ads and self-promotion
- Language-specific
- AdGuard Russian filter
- AdGuard German filter
- AdGuard Japanese filter
- AdGuard Dutch filter
- AdGuard Spanish/Portuguese filter
- AdGuard Turkish filter
- AdGuard French filter
- AdGuard Ukrainian filter
- Bulgarian list
- EasyList Czech and Slovak
- EasyList Hebrew
- EasyList Italy
- EasyList Lithuania
- Latvian List
- Liste AR
- AdBlockID
- EasyList Thailand
- Hungarian filter
- ABPVN List
- Official Polish filters for AdBlock, uBlock Origin \& AdGuard
- Polish GDPR-Cookies Filters
- Estonian List
- AdGuard Chinese filter
- List-KR
- Adblock List for Finland
- Persian Blocker
- Polish Anti Adblock Filters
- Frellwit's Swedish Filter
- Dandelion Sprout's Nordic Filters
- Dandelion Sprout's Serbo-Croatian List
- IndianList
- Macedonian adBlock Filters
- Development
- build:assets
- build:lib
- build:cli
- build:docs
- build
Install package.
> NOTE: To update filters in time, make sure you have the latest version of the package installed.
``bash`
npm install --save-dev @adguard/dnr-rulesets
1. Add scripts to your package.json to load DNR rulesets and patch extension manifest.
`json`
{
"scripts": {
"load-dnr-rulesets": "dnr-rulesets load
"patch-manifest": "dnr-rulesets manifest
}
}
Available commands:
#### load command
Downloads and saves DNR rulesets to the specified directory.
`bash`
dnr-rulesets load
Options for load command:
- -l, --latest-filters - download latest text filters instead of DNR rulesets (default: false)
#### manifest command
Patches the extension manifest to include DNR rulesets.
`bash`
dnr-rulesets manifest
Options for manifest command:
- -f, --force-update - force update rulesets with existing id (default: false)-i, --ids
- - filters ids to append, others will be ignored (default: [] - append all)-e, --enable
- - enable filters by default (default: [])-r, --ruleset-prefix
- - prefix for filters ids (default: "ruleset_")-m, --filters-match
- - filters files match glob pattern (default: "filter_+([0-9]).txt")
Note about array options: For options that accept multiple values (ids and enable), use please following syntax:
`bash`
--ids=1,2
#### watch command
Watches for changes in the filter files and rebuilds DNR rulesets.
`bash`
dnr-rulesets watch
Arguments:
- - path to the manifest.json file
- - folder with resources to build $redirect rules (can be obtained via @adguard/tswebextension war command)
Options for watch command:
- -p, --path-to-filters - path to filters and i18n metadata file (default: ./filters relative to manifest folder)-o, --output-path-for-rulesets
- - output path for rulesets (default: ./filters/declarative relative to manifest folder)-f, --force-update
- - force update rulesets with existing id (default: true)-i, --ids
- - filters ids to process, others will be ignored (default: [] - process all filters matched via --filters-match)-e, --enable
- - enable filters by default in manifest.json (default: [])-r, --ruleset-prefix
- - prefix for filters ids (default: "ruleset_")-m, --filters-match
- - filters files match glob pattern (default: "filter_+([0-9]).txt")-l, --latest-filters
- - download latest text filters on first start before watch (default: false)-d, --debug
- - enable extended logging during conversion (default: false)-j, --prettify-json
- - prettify JSON output (default: true)
Scans rulesets in the specified directory, excludes unsafe rules, and saves
excluded unsafe rules to the metadata files, and update rulesets checksums.
`bash`
dnr-rulesets exclude-unsafe-rules
Arguments:
-
: Path to the folder containing rulesets to process.Options:
-
-j, --prettify-json : Prettify JSON output (true or false, default: true)
- -l, --limit : Limit the number of unsafe rules to exclude. If the number of unsafe rules exceeds this limit, the command will throw an error.Example:
`bash
dnr-rulesets exclude-unsafe-rules ./filters/declarative --prettify-json false --limit 100
`
Note about array options: For options that accept multiple values (
ids and enable), use please following syntax:`bash
--ids=1,2
`1. Run the script to load DNR rulesets as part of your build flow.
`bash
npm run load-dnr-rulesets
`1. Patch your extension manifest to include DNR rulesets.
`bash
npm run patch-manifest
`$3
You can also integrate functions for downloading and updating the manifest into your build script:
1. Load DNR rulesets.
This method copies prebuilt assets to the specified output directory, including:
- DNR rulesets in JSON format for all available filters
-
filters_i18n.json - translations file with filter names and descriptions
- local_script_rules.js - local script rules file in JS module format for
Manifest v3 extensions where it is highly recommended to provide local script rules. If not provided during build, all script rules (except scriptlets) will not be injected to ensure compliance with Chrome Web Store policies.
- local_script_rules.json - local script rules in JSON format for Manifest v2. If not provided, all script rules are treated as allowed. Should be provided in Firefox AMO according to their policies.
`ts
import { AssetsLoader } from '@adguard/dnr-rulesets'; const loader = new AssetsLoader();
await loader.load('');
`2. Copy only local script rules.
Option A: Copy JavaScript format rules
`ts
import { AssetsLoader } from '@adguard/dnr-rulesets'; const loader = new AssetsLoader();
await loader.copyLocalScriptRulesJs('');
` This method copies only the
local_script_rules.js file to the specified destination directory. Option B: Copy JSON format rules
`ts
import { AssetsLoader } from '@adguard/dnr-rulesets'; const loader = new AssetsLoader();
await loader.copyLocalScriptRulesJson('');
` This method copies only the
local_script_rules.json file to the specified destination directory.3. Extend local script rules with custom rules.
Option A: Using
extendLocalScriptRulesJs (Manifest V3)
`ts
import { AssetsLoader } from '@adguard/dnr-rulesets'; const loader = new AssetsLoader();
await loader.extendLocalScriptRulesJs(
'',
[
'example.com#%#const ad = document.querySelector(".ad"); ad.remove();',
'example.org#%#console.log("Custom script");'
]
);
` This method parses custom filtering rules, extracts JavaScript injection rules from them, and appends them to an existing
local_script_rules.js file. It's useful for dynamically adding custom JS rules to your extension at build time. Option B: Using
extendLocalScriptRulesJson (Manifest V2)
`ts
import { AssetsLoader } from '@adguard/dnr-rulesets'; const loader = new AssetsLoader();
await loader.extendLocalScriptRulesJson(
'',
[
'example.com#%#const ad = document.querySelector(".ad"); ad.remove();',
'example.org,~sub.example.org#%#console.log("Custom script");'
]
);
` This method parses custom filtering rules, extracts JavaScript injection rules with their domain configurations (both permitted and restricted domains), and merges them into an existing
local_script_rules.json file. Use this method when you need to maintain domain-specific rule associations. > Note: The
extendLocalScriptRulesJs and extendLocalScriptRulesJson methods are only available in the programmatic API and not in the CLI. These methods require programmatic access to parse and manipulate existing local script rule files, which is more suitable for build scripts and custom automation workflows rather than command-line usage.4. Patch extension manifest.
`ts
import { ManifestPatcher } from '@adguard/dnr-rulesets'; const patcher = new ManifestPatcher();
patcher.path(
'',
'',
{
// Optional: specify filter IDs to include
ids: ['2', '3'],
// Optional: specify enabled filter IDs
enabled: ['2'],
// Optional: set to true to overwrite existing rulesets
forceUpdate: true,
// Optional: set prefix for ruleset paths
rulesetPrefix: 'ruleset_',
// Optional: specify filter files matching glob pattern
filtersMatch: 'filter_+([0-9]).txt',
},
)
`Also you can call to
excludeUnsafeRules in your custom build flows or automation scripts.`ts
import { excludeUnsafeRules } from '@adguard/dnr-rulesets';await excludeUnsafeRules('', {
prettifyJson: true, // optional
limit: 100, // optional
});
`
$3
`bash
/
|
|declarative
| |
| |ruleset_
| |
| |ruleset_.json // DNR ruleset converted from filter_.txt
| |metadata.json // Ruleset metadata with source mapping
| |lazy_Metadata.json // Additional ruleset metadata for lazy loading
|
|filter_.txt // Original filter rules with specified id
`$3
The package provides a set of utility functions for working with DNR rulesets.
####
getVersion()Returns the version of the package.
`ts
import { getVersion } from '@adguard/dnr-rulesets/utils';const dnrRulesetsVersion = getVersion();
`####
getVersionTimestampMs()Returns the timestamp of the dnr-rulesets build, based on the patch version,
or current timestamp of the function call if date and time is not present in the patch version.
`ts
import { getVersionTimestampMs } from '@adguard/dnr-rulesets/utils';const dnrRulesetsBuildTimestamp = getVersionTimestampMs();
`Advanced usage
$3
We also provide flexible API to apply rulesets to the manifest object.
It can be useful if you want to patch to the manifest while bundling.
`ts
import { RulesetsInjector } from '@adguard/dnr-rulesets';const injector = new RulesetsInjector();
const manifest = {
// Your manifest data
};
const ManifestWithRulesets = injector.applyRulesets(
(id) =>
,
manifest,
['2', '3'],
{
// Optional: specify filter IDs to include
ids: ['2', '3'],
// Optional: specify enabled filter IDs
enabled: ['2'],
// Optional: set to true to overwrite existing rulesets
forceUpdate: true,
// Optional: set prefix for ruleset paths
rulesetPrefix: 'ruleset_',
},
);
`Example
Example of usage: adguard-api-mv3
Included filter lists
$3
#### AdGuard Base filter
EasyList + AdGuard English filter. This filter is necessary for quality ad blocking.
- Filter ID: 2
- Path:
#### AdGuard Mobile Ads filter
Filter for all known mobile ad networks. Useful for mobile devices.
- Filter ID: 11
- Path:
$3
#### AdGuard Tracking Protection filter
The most comprehensive list of various online counters and web analytics tools. Use this filter if you do not want your actions on the Internet to be tracked.
- Filter ID: 3
- Path:
#### AdGuard URL Tracking filter
Filter that enhances privacy by removing tracking parameters from URLs.
- Filter ID: 17
- Path:
$3
#### AdGuard Social Media filter
Filter for social media widgets such as 'Like' and 'Share' buttons and more.
- Filter ID: 4
- Path:
$3
#### AdGuard Cookie Notices filter
Blocks cookie notices on web pages.
- Filter ID: 18
- Path:
#### AdGuard Popups filter
Blocks all kinds of pop-ups that are not necessary for websites' operation according to our Filter policy.
- Filter ID: 19
- Path:
#### AdGuard Mobile App Banners filter
Blocks irritating banners that promote mobile apps of websites.
- Filter ID: 20
- Path:
#### AdGuard Other Annoyances filter
Blocks irritating elements on web pages that do not fall under the popular categories of annoyances.
- Filter ID: 21
- Path:
#### AdGuard Widgets filter
Blocks annoying third-party widgets: online assistants, live support chats, etc.
- Filter ID: 22
- Path:
$3
#### Online Malicious URL Blocklist
Blocks domains that are known to be used to propagate malware and spyware.
- Filter ID: 208
- Path:
#### Phishing URL Blocklist
Phishing URL blocklist for uBlock Origin (uBO), AdGuard, Vivaldi, Pi-hole, Hosts file, Dnsmasq, BIND, Unbound, Snort and Suricata.
- Filter ID: 255
- Path:
#### Scam Blocklist by DurableNapkin
List for blocking untrustworthy websites.
- Filter ID: 256
- Path:
#### uBlock Origin – Badware risks
Filter for risky sites, warning users of potential threats.
- Filter ID: 257
- Path:
#### Dandelion Sprout's Anti-Malware List
Blocks more malware than most other major anti-malware lists - domains and URL patterns used in malware redirection chains, IP addresses that are solely used by malware, PUP nags, and a few scammers. Already included in Dandelion Sprout's Annoyances List.
- Filter ID: 259
- Path:
$3
#### AdGuard Experimental filter
Filter designed to test certain hazardous filtering rules before they are added to the basic filters.
- Filter ID: 5
- Path:
#### Filter unblocking search ads and self-promotion
Filter that unblocks search ads in Google, DuckDuckGo, Bing, or Yahoo and self-promotion on websites.
- Filter ID: 10
- Path:
$3
#### AdGuard Russian filter
Filter that enables ad blocking on websites in Russian language.
- Filter ID: 1
- Path:
#### AdGuard German filter
EasyList Germany + AdGuard German filter. Filter list that specifically removes ads on websites in German language.
- Filter ID: 6
- Path:
#### AdGuard Japanese filter
Filter that enables ad blocking on websites in Japanese language.
- Filter ID: 7
- Path:
#### AdGuard Dutch filter
EasyList Dutch + AdGuard Dutch filter. Filter list that specifically removes ads on websites in Dutch language.
- Filter ID: 8
- Path:
#### AdGuard Spanish/Portuguese filter
Filter list that specifically removes ads on websites in Spanish, Portuguese, and Brazilian Portuguese languages.
- Filter ID: 9
- Path:
#### AdGuard Turkish filter
Filter list that specifically removes ads on websites in Turkish language.
- Filter ID: 13
- Path:
#### AdGuard French filter
Liste FR + AdGuard French filter. Filter list that specifically removes ads on websites in French language.
- Filter ID: 16
- Path:
#### AdGuard Ukrainian filter
Filter that enables ad blocking on websites in Ukrainian language.
- Filter ID: 23
- Path:
#### Bulgarian list
Additional filter list for websites in Bulgarian.
- Filter ID: 103
- Path:
#### EasyList Czech and Slovak
Additional filter list for websites in Czech and Slovak.
- Filter ID: 105
- Path:
#### EasyList Hebrew
Additional filter list for websites in Hebrew.
- Filter ID: 108
- Path:
#### EasyList Italy
Additional filter list for websites in Italian.
- Filter ID: 109
- Path:
#### EasyList Lithuania
Additional filter list for websites in Lithuanian.
- Filter ID: 110
- Path:
#### Latvian List
Additional filter list for websites in Latvian.
- Filter ID: 111
- Path:
#### Liste AR
Additional filter list for websites in Arabic.
- Filter ID: 112
- Path:
#### AdBlockID
Additional filter list for websites in Indonesian.
- Filter ID: 120
- Path:
#### EasyList Thailand
Filter that blocks ads on Thai sites.
- Filter ID: 202
- Path:
#### Hungarian filter
Hufilter. Filter list that specifically removes ads on websites in the Hungarian language.
- Filter ID: 203
- Path:
#### ABPVN List
Vietnamese adblock filter list.
- Filter ID: 214
- Path:
#### Official Polish filters for AdBlock, uBlock Origin & AdGuard
Additional filter list for websites in Polish.
- Filter ID: 216
- Path:
#### Polish GDPR-Cookies Filters
Polish filter list for cookies blocking.
- Filter ID: 217
- Path:
#### Estonian List
Filter for ad blocking on Estonian sites.
- Filter ID: 218
- Path:
#### AdGuard Chinese filter
EasyList China + AdGuard Chinese filter. Filter list that specifically removes ads on websites in Chinese language.
- Filter ID: 224
- Path:
#### List-KR
Filter that removes ads and various scripts from websites with Korean content. Combined and augmented with AdGuard-specific rules for enhanced filtering. This filter is expected to be used alongside with AdGuard Base filter.
- Filter ID: 227
- Path:
#### Adblock List for Finland
Finnish ad blocking filter list.
- Filter ID: 233
- Path:
#### Persian Blocker
Filter list for blocking ads and trackers on websites in Persian.
- Filter ID: 235
- Path:
#### Polish Anti Adblock Filters
Official Polish filters against Adblock alerts.
- Filter ID: 238
- Path:
#### Frellwit's Swedish Filter
Filter that aims to remove regional Swedish ads, tracking, social media, annoyances, sponsored articles etc.
- Filter ID: 243
- Path:
#### Dandelion Sprout's Nordic Filters
This list covers websites for Norway, Denmark, Iceland, Danish territories, and the Sami indigenous population.
- Filter ID: 249
- Path:
#### Dandelion Sprout's Serbo-Croatian List
A filter list for websites in Serbian, Montenegrin, Croatian, and Bosnian.
- Filter ID: 252
- Path:
#### IndianList
Additional filter list for websites in Hindi, Tamil and other Dravidian and Indic languages.
- Filter ID: 253
- Path:
#### Macedonian adBlock Filters
Blocks ads and trackers on various Macedonian websites.
- Filter ID: 254
- Path:
Development
$3
Downloads original rules, converts it to DNR rule sets via TSUrlFilter declarative-converter and generates extension manifest with predefined rules resources.
`bash
pnpm build:assets
`$3
Builds SDK to load DNR rule sets to the specified directory.
`bash
pnpm build:lib
`$3
Builds CLI utility to load DNR rule sets to the specified directory, inject rulesets to the manifest object and can be used for local development for DNR rulesets.
`bash
pnpm build:cli
`$3
Generates Included filter lists section.
`bash
pnpm build:docs
`$3
Clears
dist folder and runs build:assets, build:cli and build:lib scripts.`bash
pnpm build
`$3
Watches for changes in the dist/filters folder and rebuilds DNR rulesets.`bash
pnpm watch
``