AdGuard Scriptlets and Redirect resources
AdGuard's Scriptlets and Redirect resources library which provides extended capabilities for content blocking.
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.
Syntax
rule = [domains] "#%#//scriptlet(" scriptletName arguments ")"
scriptletName
(mandatory) is a name of the scriptlet from AdGuard's scriptlets libraryarguments
(optional) a list of String
arguments (no other types of arguments are supported)
Remarks
- The meanining of the arguments depends on the scriptlet.
- You can use either single or double quotes for the scriptlet name and arguments.
- Special characters must be escaped properly:
'prop["nested"]'
- valid"prop['nested']"
- valid"prop[\"nested\"]"
- also valid"prop["nested"]"
- not valid'prop['nested']'
- not valid
Example
example.org#%#//scriptlet('abort-on-property-read', 'alert')
example.org#%#//scriptlet('remove-class', 'branding', 'div[class^="inner"]')
This rule applies the abort-on-property-read
scriptlet on all pages of example.org
and its subdomains, and passes one orgument to it (alert
).
Redirect resources
AdGuard is able to redirect web requests to a local "resource".
Syntax
AdGuard uses the same filtering rule syntax as uBlock Origin. Also, it is compatible with ABP $rewrite=abp-resource
modifier.
$redirect
is a modifier for the basic filtering 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 resources below.
Examples
||example.org/script.js$script,redirect=noopjs
-- redirects all requests to script.js
to the resource named noopjs
.||example.org/test.mp4$media,redirect=noopmp4-1s
-- requests to example.org/test.mp4
will be redirected to the resource named noopmp4-1s
.
$redirect
rules priority is higher than the regular basic blocking rules' priority. This means that if there's a basic blocking rule (even with $important
modifier), $redirect
rule will prevail over it. If there's a whitelist (@@
) rule matching the same URL, it will disable redirecting as well (unless the $redirect
rule is also marked as $important
).
uBlock Origin specifies additional resource name none
that can disable other redirect rules. AdGuard does not support it, use $badfilter
to disable specific rules.
How to build
Install dependencies
yarn install
Build for CoreLibs
yarn corelibs
Build dev (rebuild js files on every change)
yarn watch
Build for Extension
In scriptlets directory install dependencies, build scriptlets bundle, and create scriptlets link.
yarn
yarn build
yarn link
In tsurlfilter directory install and link dependencies, link scriptlets, move into package and build, and create tsurlfilter link.
lerna bootstrap
yarn link "@adguard/scriptlets"
cd ./packages/tsurlfilter
yarn build
yarn link
In extension directory install dependincies, link packages and build
yarn
yarn link @adguard/scriptlets
yarn link @adguard/tsurlfilter
yarn dev
Build output
Scriptlets library
You are welcome to use scriptlets and redirect resources as a CJS module. They can be imported from dist/cjs/scriptlets.cjs.js
:
const scriptlets = require('scriptlets');
const { redirects } = require('scriptlets');
And also there is a module at dist/scriptlets.js
which has been exported to a global variable scriptlets
with such methods:
scriptlets.invoke(source);
scriptlets.isValidScriptletName(name);
scriptlets.isValidScriptletRule(input);
scriptlets.isAdgScriptletRule(rule);
scriptlets.isUboScriptletRule(rule);
scriptlets.isAbpSnippetRule(rule);
scriptlets.convertUboToAdg(rule);
Note that parameters in UBO rule should be separated by comma + space. Otherwise, the rule is not valid.
scriptlets.convertAbpToAdg(rule);
scriptlets.convertScriptletToAdg(rule);
scriptlets.convertAdgToUbo(rule);
Imported redirects
has such methods:
redirects.getCode(source);
redirects.isAdgRedirectRule(rule)
redirects.isValidAdgRedirectRule(rule);
redirects.isAdgRedirectCompatibleWithUbo(rule);
redirects.isUboRedirectCompatibleWithAdg(rule);
redirects.isAbpRedirectCompatibleWithAdg(rule);
redirects.convertUboRedirectToAdg(rule);
redirects.convertAbpRedirectToAdg(rule);
redirects.convertRedirectToAdg(rule);
redirects.convertAdgRedirectToUbo(rule);
Corelibs library
dist/scriptlets.corelibs.json
File example
{
"version": "1.0.0",
"scriptlets": [
{
"names": [
"abort-on-property-read",
"ubo-abort-on-property-read.js",
"abp-abort-on-property-read"
],
"scriptlet": "function() { ...code... }"
},
]
}
Schema
{
"type": "object",
"properties": {
"version": {
"type": "string"
},
"scriptlets": {
"type": "array",
"items": {
"type": "object",
"properties": {
"names": {
"type": "array",
"items": {
"type": "string"
}
},
"scriptlet": {
"type": "string"
}
},
}
}
}
}
Redirects library
dist/redirects.js
dist/redirects.yml
Creates a global variable Redirects
.
const redirects = new Redirects(rawYaml)
const redirect = redirect.getRedirect('noopjs');
How to test
Run node testing
yarn test
Run tests gui
yarn gui-test
Watcher is available
yarn test-watch
Limit testing by commenting out corresponding values in rollup.config.js
const MULTIPLE_TEST_FILES_DIRS = [
// 'scriptlets',
// 'redirects',
];
const ONE_TEST_FILE_DIRS = [
'lib-tests',
// 'helpers',
];
or index.test.js
// import './scriptlets/index.test';
import './redirects/index.test';
// import './lib-tests/index.test';
// import './helpers/index.test';
It is also possible to exclude libtests in tests/lib-tests/index.test.js
Run specific scriptlet or redirect test by editing rollup.config.js
.filter((el) => {
return el !== 'index.test.js'
// Uncomment next line and use required scriptlet/redirect name
// && el === 'gemius.test.js'
&& el.includes(TEST_FILE_NAME_MARKER);
});
To run browserstack tests create .env
file or copy and rename .env-example
.
Fill in and with data from your Browserstack profile.
Run next command
yarn browserstack
Debugging
Use debugger;
statement where you need it, run
yarn test
and open needed HTML file from tests/dist
in your browser with devtools
Chrome | Edge | Firefox | IE | Opera | Safari |
---|
55 | 15 | 52 | 11 | 42 | 10 |