Scriptlets
AdGuard scriptlets library
Install / Use
/learn @AdguardTeam/ScriptletsREADME
AdGuard Scriptlets and Redirect Resources
AdGuard's Scriptlets and Redirect resources library which provides extended capabilities for content blocking.
- Scriptlets
- Redirect resources
- Development
- Usage
- API description
- Scriptlets API
- Redirects API
- Validators API <!-- markdownlint-disable-next-line -->
- Converters API
- Browser compatibility
- Projects using Scriptlets
Scriptlets
Scriptlet is a JavaScript function which can be used in a declarative manner in AdGuard filtering rules.
AdGuard supports a lot of different scriptlets. Please note, that in order to achieve cross-blocker compatibility, we also support syntax of uBO and ABP.
<a name="scriptlet-syntax"></a> Syntax
<a name="scriptlet-syntax--blocking"></a> Blocking rules
[domains]#%#//scriptlet(name[, arguments])
domains— optional, a list of domains where the rule should be applied;name— required, a name of the scriptlet from AdGuard Scriptlets library;arguments— optional, a list ofstringarguments (no other types of arguments are supported).
Remarks
The meaning of the arguments depends on the scriptlet.
Special characters in scriptlet argument must be escaped properly:
- valid:
'prop["nested"]'"prop['nested']"'prop[\'nested\']'"prop[\"nested\"]"- not valid:
'prop['nested']'"prop["nested"]"Scriptlet
nameand each of theargumentsshould be wrapped in quotes. You can use either single or double quotes for the scriptlet name and arguments. Single quote is recommended but not for cases when its usage makes readability worse, e.g.".css('display','block');"is more preferred then'.css(\'display\',\'block\');'.
<a name="scriptlet-syntax--exceptions"></a> Exception rules
[domains]#@%#//scriptlet([name[, arguments]])
domains— optional, a list of domains where the rule should be applied;name— optional, a name of the scriptlet to except from the applying; if not set, all scriptlets will not be applied;arguments— optional, a list ofstringarguments to match the same blocking rule and disable it.
Examples
-
Apply the
abort-on-property-readscriptlet on all pages ofexample.organd its subdomains, and passes one argument to it (alert):example.org#%#//scriptlet('abort-on-property-read', 'alert') -
Remove the
brandingclass from alldiv[class^="inner"]elements on all pages ofexample.organd its subdomains:example.org#%#//scriptlet('remove-class', 'branding', 'div[class^="inner"]') -
Apply
set-constantandset-cookieon any webpage, but because of specific scriptlet exception rule onlyset-constantscriptlet will be applied onexample.organd its subdomains:#%#//scriptlet('set-constant', 'adList', 'emptyArr') #%#//scriptlet('set-cookie', 'accepted', 'true') example.org#@%#//scriptlet('set-cookie') -
Apply
adjust-setIntervalon any webpage, andset-local-storage-itemon all pages ofexample.comand its subdomains, but there is also multiple scriptlet exception rule, so no scriptlet rules will be applied onexample.comand its subdomains:#%#//scriptlet('adjust-setInterval', 'count', '*', '0.001') example.com#%#//scriptlet('set-local-storage-item', 'ALLOW_COOKIES', 'false') example.com#@%#//scriptlet()
<a name="trusted-scriptlets"></a> Trusted scriptlets
Trusted scriptlets are scriptlets with extended functionality.
Their names are prefixed with trusted-, e.g trusted-click-element,
to be easily distinguished from common scriptlets.
<a name="trusted-scriptlets-restriction"></a> Restriction
Trusted scriptlets application must be restricted due to dangerous nature of their capabilities. Allowed sources of trusted scriptlets are:
- filters created by AdGuard Team,
- custom filters which were installed as
trusted, - user rules.
Trusted scriptlets has no compatibility table as they are not compatible with any other blocker.
Redirect resources
AdGuard is able to redirect web requests to a local "resource".
<a name="redirect-syntax"></a> Syntax
AdGuard uses the same filtering rule syntax as [uBlock Origin][ubo-redirect].
Also, it is compatible with ABP $rewrite=abp-resource modifier.
$redirect is a modifier for [the basic filtering rules][kb-basic-rules]
so rules with this modifier support all other basic modifiers like $domain, $third-party, $script, etc.
The value of the $redirect modifier must be the name of the resource that will be used for redirection.
See the list of available redirect resources.
Priority of
$redirectrules is described in the [Knowledge Base][kb-redirect-priority].
Examples
||example.org/script.js$script,redirect=noopjs— redirects all requests toscript.jsto the resource namednoopjs.||example.org/test.mp4$media,redirect=noopmp4-1s— requests toexample.org/test.mp4will be redirected to the resource namednoopmp4-1s.
uBlock Origin specifies additional resource name
nonethat can disable other redirect rules. AdGuard does not support it, use$badfilterto disable specific rules.
<a name="development"></a> Development
<a name="how-to-build"></a> How to build
Install dependencies:
pnpm install
Build dist:
pnpm build
In tsurlfilter directory install and link dependencies, link scriptlets, move into package and build, and create tsurlfilter link.
lerna bootstrap
pnpm link --global "@adguard/scriptlets"
cd ./packages/tsurlfilter
pnpm build
pnpm link --global
In extension directory install dependencies, link packages and build
pnpm install
pnpm link --global "@adguard/scriptlets"
# run build script
<a name="how-to-test"></a> How to test
Some tests are run in QUnit, some in Vitest.
Run all tests:
pnpm test
-
QUnit is used for testing of scriptlets, redirects, and helpers:
pnpm test:qunit [scriptlets | redirects | helpers]For scriptlets and redirects test run can be more specific:
// node test run pnpm test:qunit scriptlets --name set-cookie pnpm test:qunit redirects --name ati-smarttag // gui test run pnpm test:qunit scriptlets --name set-cookie --gui pnpm test:qunit redirects --name ati-smarttag --guiFor debugging purposes after some test is running in gui mode, you may change your scriptlet/redirect code, and without stopping the server run in new terminal:
pnpm test:qunit scriptlets --name set-cookie --build -
Run all jest tests:
pnpm test:vitestor limit the testing —
includemay
Related Skills
node-connect
343.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
92.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.3kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
