资讯动态

uBlock Origin 的 HNTrieContainer 怎么单独创建并验证主机名匹配?

发布时间:2026/9/9 21:46:38 来源:尧图企业网站定制
uBlock Origin 的 HNTrieContainer 怎么单独创建并验证主机名匹配【免费下载链接】uBlockuBlock Origin - An efficient blocker for Chromium and Firefox. Fast and lean.项目地址: https://gitcode.com/GitHub_Trending/ub/uBlock如果你想在 uBlock OriginuBO扩展之外单独使用它的主机名匹配组件任务就是从 npm 包gorhill/ubo-core中导入HNTrieContainer向其中写入一批主机名然后用matches()验证哪些主机名会命中、哪些不会。本文的操作路径来自仓库中 platform/npm/README.md 的 “Extras / HNTrieContainer” 一节适用前提是 Node.js 18platform/npm/package.json 中engines声明node: 18.0.0并且代码以原生 ES module 方式运行。安装 ubo-core 包HNTrieContainer随 uBO 的核心过滤引擎一起发布在 npm 包gorhill/ubo-core中仓库内版本见 platform/npm/package.json为 0.1.30。在一个新目录中安装npm install gorhill/ubo-core包本身声明了type: module。如果你的独立脚本要直接import这个包参照 demo.js 的说明可以在项目的package.json中加上type: module以避免警告。README 同时提醒这是一个非常早期的版本API 随时可能变化。创建容器并写入主机名HNTrieContainer是一个压缩 trie 容器专门用于存储和查找主机名。创建流程是先new一个容器再用createOne()在容器里创建独立的 trie 句柄然后用add(句柄, 主机名)逐条写入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);一个容器内可以持有多个 trie上面例子就是两个互不相干的集合aTrie和anotherTrie这正好用来验证“跨 trie 不会误匹配”。把脚本保存为例如trie-test.mjs或确保所在目录package.json含type: module即可node trie-test.mjs运行。用 matches() 验证主机名匹配matches()的返回值语义在 README 中写明返回匹配开始的位置无匹配时返回 -1。README 给出的预期结果如下运行后应逐项核对// 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));README 中标注的预期输出matches(aTrie, www.example.org)返回4matches(aTrie, www.foo.invalid)返回-1matches(anotherTrie, www.example.org)返回-1matches(anotherTrie, foo.invalid)返回0。这四组结果覆盖了三类必须区分的行为子域名命中匹配是“按主机名标签从右到左”进行的所以www.example.org会命中已存储的example.org命中位置 4 正是example.org在完整字符串中的起始下标反向不成立example.org不会命中www.example.org。相似前缀不算命中www.foo.invalid没有存入 trie存入的是foo.invalid返回-1。README 特别强调anotherexample.org这类“同后缀相似域名”不会误判为example.org。trie 之间隔离anotherTrie里查www.example.org返回-1说明查找只作用于指定句柄对应的集合。这个从右到左的子域名语义在实现源码 src/js/hntrie.js 头部注释中同样有说明www.abc.com视为匹配abc.com因为前者是后者的子域名。reset() 的清理限制与恢复手段README 明确了两点限制trieContainer.reset();reset()会移除容器里全部trie无法只删除单个 triereset 之后之前拿到的aTrie、anotherTrie等旧引用不再有效不应再使用。也就是说若只想“删掉一个集合”文档给出的路径是 reset 后重新createOne()重建而不是对单个句柄操作。延伸阅读与边界仓库的 docs/tests/hntrie-test.html 是基于 docs/tests/hostname-pool.js 主机名池跑的完整测试页docs/tests/index.html 说明该页通过 raw.githack.com 托管因为 WebAssembly 模块需要同源策略加载它引用的是固定历史版本的hntrie.js与 npm 包当前 API 不完全一致这里只作为存在性参考不再展开。src/js/hntrie.js 中 WASM 模块是可选加速项若 WASM 模块不可用例如 CPU 非小端会自动回落到 JS 实现功能不受影响。再次提醒 README 的声明gorhill/ubo-core属于早期版本API 随时可能变化集成到自有项目时建议锁定版本并回归验证matches()的返回值语义。【免费下载链接】uBlockuBlock Origin - An efficient blocker for Chromium and Firefox. Fast and lean.项目地址: https://gitcode.com/GitHub_Trending/ub/uBlock创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价