@gorhill/ubo-core

中文文档
0.1.30发布于 2024/10/07GPL-3.0-or-laterES Module零依赖近 30 天下载 1.1万

创建 uBlock Origin 的静态网络过滤引擎的工作实例

中文使用文档
文档目录5 节
  1. 安装
  2. 说明
  3. 使用量
  4. 额外
  5. HNTrie 连接器

uBlock 源代码核心

UBlock Origin("uBO")扩展中使用的核心过滤引擎,并且有 没有外部依赖关系.

安装

安装 : npm install @gorhill/ubo-core

这是一个非常早期的版本,API随时会被更改.

此软件包使用 本地 JavaScript 模块。 。 。 .

说明

包中包含uBO的静态网络过滤引擎("SNFE"),它 目的是分析并强制执行过滤列表。 匹配算法很高 高效率 特别是 优化以匹配大组纯度 主机名称.

SNFE可以从各种来源,例如: EasyList/简陋, (中文(简体) ), uBlock 过滤器, (中文(简体) ), 以及域名或主机文件格式列表(即来自 区块列表项目, (中文(简体) ), 史蒂文・布莱克的宿营(见e/cn.4/sub.2/2002/l.48等).

使用量

见 ./demo.js 用于快速启动指令.

目前只有一次静态网络过滤 引擎 ("SNFE"),其代理 API 必须导入如下 :

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

如果您必须导入为节点JS模块 :

const { StaticNetFilteringEngine } = await import('@gorhill/ubo-core');

创建 SNFE 实例 :

const snfe = await StaticNetFilteringEngine.create();

用过滤列表输入 SNFE -- useLists() 接受一组 显示列表原始文本的对象(或承诺反对) 通过 raw 属性,通过 name 属性( 如何获取列表由您决定 ):

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

现在我们准备满足网络要求:

// 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());
}

一旦所有过滤列表被装入静态网络过滤引擎, 您可以将引擎内容序列化为 JS 串:

const serializedData = await snfe.serialize();

您可以保存并使用 JS 字符串快速装入内容 静态网络过滤引擎, 无需解析和编译列表 :

const snfe = await StaticNetFilteringEngine.create();
await snfe.deserialize(serializedData);

额外

您可以直接使用此软件包所曝光的特定 API, 这里有一些 它们被UBO的SNFE内部使用.

HNTrie 连接器

完美优化 压缩三进制 专门存储和查找主机名的容器.

匹配算法是为主机名设计,即主机名标签 组成主机名从右到左匹配, 如 www.example.org 与匹配,如果 example.org 保存在三进制中,而 anotherexample.org 不会是匹配.

HNTrieContainer 用于存储大量 CPU 和 内存效率作为一个主要关切,也是uBO的关键组成部分.

创建和使用独立的 HNTrieContainer 对象 :

import HNTrieContainer from '@gorhill/ubo-core/js/hntrie.js';

const trieContainer = new HNTrieContainer();

const aTrie = trieContainer.createOne();
trieContainer.add(aTrie, 'example.org');
trieContainer.add(aTrie, 'example.com');

const anotherTrie = trieContainer.createOne();
trieContainer.add(anotherTrie, 'foo.invalid');
trieContainer.add(anotherTrie, 'bar.invalid');

// matches() return the position at which the match starts, or -1 when
// there is no match.

// Matches: return 4
console.log("trieContainer.matches(aTrie, 'www.example.org')", trieContainer.matches(aTrie, 'www.example.org'));

// Does not match: return -1
console.log("trieContainer.matches(aTrie, 'www.foo.invalid')", trieContainer.matches(aTrie, 'www.foo.invalid'));

// Does not match: return -1
console.log("trieContainer.matches(anotherTrie, 'www.example.org')", trieContainer.matches(anotherTrie, 'www.example.org'));

// Matches: return 0
console.log("trieContainer.matches(anotherTrie, 'foo.invalid')", trieContainer.matches(anotherTrie, 'foo.invalid'));

这个 reset() 必须使用方法去除三重容器中的所有尝试, 无法从容器中取出一条三角形.

trieContainer.reset();

重设 三进制容器时, 无法使用前例的引用 3,即: 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3, 3, 3, 5, 3, 3, 3, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3, 3, 3, 3, 5, 3, 3, 4, 3, 3, 3, 3, 3, 3, 5, 3, 3, 3, 3, 3, 3, 3, . aTrie 财务报告和审定财务报表 anotherTrie 不再有效,不应该 用于重置后.

版本与发布记录

@gorhill/ubo-core 共发布 29 个版本,首次发布于 2021/08/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.30latest2024/10/072 年前
0.1.292024/10/072 年前
0.1.282024/10/072 年前
0.1.272024/10/072 年前
0.1.262023/03/163 年前
0.1.252022/03/314 年前

关于 @gorhill/ubo-core 的常见问题

@gorhill/ubo-core 是什么?

创建 uBlock Origin 的静态网络过滤引擎的工作实例

如何安装 @gorhill/ubo-core?

在项目目录运行 npm install @gorhill/ubo-core,或使用 pnpm add / yarn add / bun add。

@gorhill/ubo-core 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/gorhill__ubo-core 是否存在。

@gorhill/ubo-core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@gorhill/ubo-core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@gorhill/ubo-core 可以商用吗?

@gorhill/ubo-core 使用 GPL-3.0-or-later 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@gorhill/ubo-core 的下载量有多少?

根据 npm 官方下载统计,@gorhill/ubo-core 在 2026/10/17 – 2026/11/15 这 30 天内被下载 10,982 次,日均约 366 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gorhill/ubo-core 还在维护吗?

最新版本 0.1.30 发布于 2024/10/07(2 年前);至今共发布 29 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/08/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。