-
Notifications
You must be signed in to change notification settings - Fork 307
early脚本处理url匹配问题 #1096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
early脚本处理url匹配问题 #1096
Conversation
There was a problem hiding this 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 匹配逻辑提取为独立的urlMatch和urlExclude函数 - 为 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 匹配逻辑为 urlMatch 和 urlExclude 函数,提高代码复用性 |
| src/pkg/utils/match.test.ts | 为新增的 urlMatch 和 urlExclude 函数添加测试用例 |
| 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); // 是否要排除 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 urlExclude 设计我再想一想
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
想完的结果是 urlExclude = !urlInclude ...
见单元测试新加的例子
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這個也很正常吧。不是互斥的話就很奇怪了
當然 isUrlExcluded也是可以保留的
看你吧
There was a problem hiding this comment.
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[] }>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EarlyScriptLoadInfo ?
There was a problem hiding this comment.
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看到就頭暈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确实越来越多乱起来了。。。
概述 Descriptions
close #1075
变更内容 Changes
截图 Screenshots