Skip to content

Conversation

@CodFrm
Copy link
Member

@CodFrm CodFrm commented Dec 19, 2025

概述 Descriptions

close #1075

变更内容 Changes

截图 Screenshots

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

本 PR 解决了 early 脚本(document-start 脚本)的 URL 匹配问题。主要将 URL 匹配逻辑重构为可复用的函数,并为包含正则表达式的 URL 模式添加了运行时匹配检查。

关键变更:

  • UrlMatch 类中的 URL 匹配逻辑提取为独立的 urlMatchurlExclude 函数
  • 为 early 脚本添加 scriptUrlPatterns 字段,在运行时进行 URL 匹配验证
  • 更新了 USERSCRIPTS_REGISTER_CONTROL UUID 以触发脚本重新注册

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/pkg/utils/match.ts 提取 URL 匹配逻辑为 urlMatchurlExclude 函数,提高代码复用性
src/pkg/utils/match.test.ts 为新增的 urlMatchurlExclude 函数添加测试用例
src/app/service/service_worker/utils.ts 更新 parseScriptLoadInfo 以传递正则表达式 URL 模式给 early 脚本
src/app/service/service_worker/runtime.ts 更新脚本编译流程以包含 URL 模式参数,并更新缓存控制 UUID
src/app/service/content/script_executor.ts 在 content 环境中添加 early 脚本的运行时 URL 匹配检查
src/app/repo/scripts.ts 定义 EarlyScriptLoadInfo 接口以支持带 URL 模式的 early 脚本

}
let result: boolean;
if (isOnlyExclude) {
result = !urlExclude(window.location.href, detail.scriptInfo.scriptUrlPatterns); // 是否要排除
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 urlExclude 设计我再想一想

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

想完的结果是 urlExclude = !urlInclude ...

见单元测试新加的例子

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這個也很正常吧。不是互斥的話就很奇怪了
當然 isUrlExcluded也是可以保留的
看你吧

Copy link
Member Author

@CodFrm CodFrm Dec 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我的写法被你改动了,之前的逻辑是只校验这个url是不是被排除的,而不是现在的包含在include 或 排除,不过这样包含的情况更全面一些,那就不用检查表达式做判断了,直接判断 isUrlIncluded/isUrlExcluded 即可

const scriptCodes = {} as Record<string, string>;
// 更新资源使用了file协议的脚本
const scriptsWithUpdatedResources = new Map<string, ScriptLoadInfo>();
const scriptsWithUpdatedResources = new Map<string, ScriptLoadInfo & { scriptUrlPatterns: URLRuleEntry[] }>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EarlyScriptLoadInfo ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人建議 ScriptLoadInfo直接加個
scriptUrlPatterns?: URLRuleEntry[]
就好

不然一大堆 extends extends看到就頭暈

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确实越来越多乱起来了。。。

@CodFrm CodFrm merged commit a77effb into main Dec 20, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] early-start 无视了 urlPattern

3 participants