uBlock/platform/nodejs
Raymond Hill 76abd04898
New revision for npm package
2021-08-09 19:34:35 -04:00
..
README.md Revisit the nodejs API 2021-08-08 09:17:14 -04:00
eslintrc.json Add ESLint rules for possible errors (#3804) 2021-08-04 15:27:32 -04:00
index.js Fix pslInit() for install script (#3807) 2021-08-09 19:12:04 -04:00
install.js Create build folder in npm install script (#3799) 2021-08-03 07:02:48 -04:00
package.json New revision for npm package 2021-08-09 19:34:35 -04:00
test.js Add serialization API to StaticNetFilteringEngine (#3806) 2021-08-09 16:52:41 -04:00

README.md

uBlock Origin Core

The core filtering engines used in the uBlock Origin ("uBO") extension, and has no external dependencies.

Installation

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.

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 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, uBlock filters, and also lists of domain names or hosts file format (i.e. block lists from The Block List Project, Steven Black's HOSTS, etc).

Usage

At the moment, there can be only one instance of the static network filtering engine ("SNFE"), which proxy API must be imported as follow:

import { StaticNetFilteringEngine } from '@gorhill/ubo-core';

If you must import as a NodeJS module:

const { StaticNetFilteringEngine } await import from '@gorhill/ubo-core';

Create an instance of SNFE:

const snfe = StaticNetFilteringEngine.create();

Feed the SNFE with filter lists -- useLists() accepts an array of 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):

await snfe.useLists([
    fetch('easylist').then(raw => ({ name: 'easylist', raw })),
    fetch('easyprivacy').then(raw => ({ name: 'easyprivacy', raw })),
]);

Now we are ready to match network requests:

// Not blocked
if ( snfe.matchRequest({
    originURL: 'https://www.bloomberg.com/',
    url: 'https://www.bloomberg.com/tophat/assets/v2.6.1/that.css',
    type: 'stylesheet'
}) !== 0 ) {
    console.log(snfe.toLogData());
}

// Blocked
if ( snfe.matchRequest({
    originURL: 'https://www.bloomberg.com/',
    url: 'https://securepubads.g.doubleclick.net/tag/js/gpt.js',
    type: 'script'
}) !== 0 ) {
    console.log(snfe.toLogData());
}

// Unblocked
if ( snfe.matchRequest({
    originURL: 'https://www.bloomberg.com/',
    url: 'https://sourcepointcmp.bloomberg.com/ccpa.js',
    type: 'script'
}) !== 0 ) {
    console.log(snfe.toLogData());
}

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.