1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-23 02:42:41 +01:00
uBlock/platform/nodejs
2021-08-06 08:25:36 -04:00
..
eslintrc.json Add ESLint rules for possible errors (#3804) 2021-08-04 15:27:32 -04:00
index.js Expose ability to compile raw list into their intermeditate form 2021-08-05 13:37:41 -04:00
install.js Create build folder in npm install script (#3799) 2021-08-03 07:02:48 -04:00
package.json Provide basic documentation for the NPM package 2021-08-06 08:25:36 -04:00
README.md Provide basic documentation for the NPM package 2021-08-06 08:25:36 -04:00
test.js Provide basic documentation for the NPM package 2021-08-06 08:25:36 -04:00

uBlock Origin Core

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

Installation

Install: npm install --save @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 for large lists of 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, which API must be imported as follow:

import { FilteringContext, pslInit, useRawLists } from '@gorhill/ubo-core';

If you must import as a NodeJS module:

const { FilteringContext, pslInit, useRawLists } await import from '@gorhill/ubo-core';

async function main() { // Initialize the internal Public Suffix List database, which is necessary // for the filtering engine to assess whether a network request is // 3rd-party to the context from which it is fired. await pslInit();

// Feed EasyList and EasyPrivacy to the filtering engine. A list is
// an object exposing the property `raw`, which contains the raw text of
// the filter lists.
const snfe = await useRawLists([
    fetch('easylist').then(raw => ({ name: 'easylist', raw })),
    fetch('easyprivacy').then(raw => ({ name: 'easyprivacy', raw })),
]);

// Reuse a single instance of filtering context used for matching
const fctxt = new FilteringContext();

// Tests
// Not blocked
fctxt.setDocOriginFromURL('https://www.bloomberg.com/');
fctxt.setURL('https://www.bloomberg.com/tophat/assets/v2.6.1/that.css');
fctxt.setType('stylesheet');
if ( snfe.matchRequest(fctxt) !== 0 ) {
    console.log(snfe.toLogData());
}

// Blocked
fctxt.setDocOriginFromURL('https://www.bloomberg.com/');
fctxt.setURL('https://securepubads.g.doubleclick.net/tag/js/gpt.js');
fctxt.setType('script');
if ( snfe.matchRequest(fctxt) !== 0 ) {
    console.log(snfe.toLogData());
}

// Unblocked
fctxt.setDocOriginFromURL('https://www.bloomberg.com/');
fctxt.setURL('https://sourcepointcmp.bloomberg.com/ccpa.js');
fctxt.setType('script');
if ( snfe.matchRequest(fctxt) !== 0 ) {
    console.log(snfe.toLogData());
}

}