uBlock/platform/nodejs/README.md

97 lines
2.9 KiB
Markdown
Raw Normal View History

2021-08-05 12:28:17 -06:00
# uBlock Origin Core
The core filtering engines used in the uBlock Origin ("uBO") extension, and has
no external dependencies.
2021-08-05 12:28:17 -06:00
## Installation
2021-08-05 12:28:17 -06:00
2021-08-07 09:38:22 -06:00
Install: `npm install @gorhill/ubo-core`
This is a very early version and the API is subject to change at any time.
This package uses [native JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules).
## Description
The package contains uBO's static network filtering engine ("SNFE"), which
purpose is to parse and enforce filter lists. The matching algorithm is highly
2021-08-06 06:29:13 -06:00
efficient, and _especially_ optimized to match against large sets of pure
hostnames.
The SNFE can be fed filter lists from a variety of sources, such as [EasyList/EasyPrivacy](https://easylist.to/),
[uBlock filters](https://github.com/uBlockOrigin/uAssets/tree/master/filters),
and also lists of domain names or hosts file format (i.e. block lists from [The Block List Project](https://github.com/blocklistproject/Lists#the-block-list-project),
[Steven Black's HOSTS](https://github.com/StevenBlack/hosts#readme), etc).
## Usage
At the moment, there can be only one instance of the static network filtering
2021-08-08 07:17:14 -06:00
engine ("SNFE"), which proxy API must be imported as follow:
```js
2021-08-08 07:17:14 -06:00
import { StaticNetFilteringEngine } from '@gorhill/ubo-core';
```
If you must import as a NodeJS module:
```js
2021-08-08 07:17:14 -06:00
const { StaticNetFilteringEngine } await import from '@gorhill/ubo-core';
```
2021-08-08 07:17:14 -06:00
Create an instance of SNFE:
2021-08-06 06:29:13 -06:00
```js
2021-08-08 07:17:14 -06:00
const snfe = StaticNetFilteringEngine.create();
2021-08-06 06:29:13 -06:00
```
2021-08-08 07:17:14 -06:00
Feed the SNFE with filter lists -- `useLists()` accepts an array of
2021-08-06 06:29:13 -06:00
objects (or promises to object) which expose the raw text of a list
through the `raw` property, and optionally the name of the list through the
`name` property (how you fetch the lists is up to you):
```js
2021-08-08 07:17:14 -06:00
await snfe.useLists([
2021-08-06 06:29:13 -06:00
fetch('easylist').then(raw => ({ name: 'easylist', raw })),
fetch('easyprivacy').then(raw => ({ name: 'easyprivacy', raw })),
]);
```
Now we are ready to match network requests:
```js
// Not blocked
2021-08-08 07:17:14 -06:00
if ( snfe.matchRequest({
originURL: 'https://www.bloomberg.com/',
url: 'https://www.bloomberg.com/tophat/assets/v2.6.1/that.css',
type: 'stylesheet'
}) !== 0 ) {
2021-08-06 06:29:13 -06:00
console.log(snfe.toLogData());
}
// Blocked
2021-08-08 07:17:14 -06:00
if ( snfe.matchRequest({
originURL: 'https://www.bloomberg.com/',
url: 'https://securepubads.g.doubleclick.net/tag/js/gpt.js',
type: 'script'
}) !== 0 ) {
2021-08-06 06:29:13 -06:00
console.log(snfe.toLogData());
}
// Unblocked
2021-08-08 07:17:14 -06:00
if ( snfe.matchRequest({
originURL: 'https://www.bloomberg.com/',
url: 'https://sourcepointcmp.bloomberg.com/ccpa.js',
type: 'script'
}) !== 0 ) {
2021-08-06 06:29:13 -06:00
console.log(snfe.toLogData());
}
```
2021-08-08 07:17:14 -06:00
It is possible to pre-parse filter lists and save the intermediate results for
later use -- useful to speed up the loading of filter lists. This will be
documented eventually, but if you feel adventurous, you can look at the code
and use this capability now if you figure out the details.