重写UrlMatch#637
Conversation
9e2484d to
bd43ff5
Compare
|
我将原来的单元测试补充了过来,似乎有很多特殊的情况没有支持 |
|
与TM产生差异是不能接受的,因为大部分开发者是使用TM进行开发,如果不能包容TM,那么总会有用户来询问为什么无法使用XX脚本,虽然我的期待也是能够让开发者们遵循 Match patterns与 globs 的规范,但是历史我们无法改变,只能慢慢纠正,至少在脚本猫没有成为绝对的主流之前,我们无能为力 |
有差異,但兼容 写法都是参考TM文档 |
|
我已经修改了单元测试,很多写法没有文档写明,但是总会有神奇脚本来与我反馈,能查到这么多资料,也是辛苦你了 另外我增加了一个addMatch用于区分 match 和 include |
|
已測試,TM不支持 |
|
CustomSequencer 是為了讓 |
|
@cyfung1031 单元测试的顺序会有什么影响吗?按理来说应该是独立的 |
没有。只是我在修改 regex_to_glob 时,发现他会影响 url_matcher 其他单元测试不清楚。不过我相信也有这种依存的关係吧 不做这个 CustomSequencer 也可以的。完全对这个PR没影响 |
|
@cyfung1031 如果不需要就移除吧 |
移除了 |
|
@CodFrm 你新加的 |
6578ff0 to
4b29825
Compare
|
@cyfung1031 lint问题我处理好了 |
|
有没有需要我修改的部份? |
|
我还没有重新review,已经全部好了吗? |
全部好了呀。暂时先这样。不然单一PR内容会过多 我已经是在build了实际使用我那200个脚本 当然没有实际用logger 去看每一个脚本是不是根据 mv3 userscript api 的 pattern 做注入再做urlMatch,还是都fallback了到全部页面再做urlMatch |
|
自定义排除那个没显示在popup. 找到了 我看看到底怎樣改較好吧 selfMetadata 的代碼設計有問題。會針對這個改 |
There was a problem hiding this comment.
Pull Request Overview
This PR rewrites the URL matching system by replacing the regex-based UrlMatch implementation with a new one based on MV3 UserScript API patterns. The changes provide better compatibility with browser extension APIs and improve support for glob patterns while maintaining consistency with TamperMonkey behavior.
- Implements a new URL matcher that supports MV3 match patterns, glob patterns (including
?wildcards), and regex patterns - Adds comprehensive regex-to-glob conversion functionality with support for complex patterns like alternations and quantifiers
- Replaces the old regex-based matching system with rule-based matching that properly handles inclusion/exclusion logic
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/pkg/utils/url_matcher.ts | Core URL matching implementation with support for MV3 patterns, globs, and regex |
| src/pkg/utils/regex_to_glob.ts | Comprehensive regex-to-glob conversion utility with support for complex patterns |
| src/pkg/utils/match.ts | Simplified URL matching class using the new rule-based system |
| src/pkg/utils/url_matcher.test.ts | Extensive test suite for URL pattern matching functionality |
| src/pkg/utils/regex_to_glob.test.ts | Comprehensive test coverage for regex-to-glob conversion |
| src/pkg/utils/match.test.ts | Updated tests for the new URL matching system |
| src/pkg/utils/utils.ts | Added utility functions for blacklist handling and random number generation |
| src/pages/options/routes/Setting.tsx | Updated blacklist validation to use new matching system |
| src/pages/components/layout/MainLayout.tsx | Fixed file URL pattern format |
| src/pages/components/ScriptMenuList/index.tsx | Updated exclude logic to use new rule-based matching |
|
呀,merge了 之後提PR解決 |
重写了
现在不用依赖 RegEx
能够更直接利用 UserScript API 做注入
当然机制上还是会跑
getPageScriptUuidByUrl测试都加多很多
不会有问题的
也考虑了很多过往版本没考虑的部份
TM的
@include只支持 glob 的 *这个是按照UserScript API的设计,不但支持
glob *, 还支持glob ?详细看这个
https://developer.chrome.com/docs/extensions/reference/manifest/content-scripts?hl=en
(注意: 不要搞乱
@match和@include跟 "matches"和"include_globs" 。 不能把网页的范例直接抄到单元测试跑。getApiMatchesAndGlobs就是处理这个)glob 的部份本来就是为 GM TM 而加的
应尽量利用,不需要用RegEx.
但MV3的设计,除了glob *, 还有glob ?
这个需要留意。根据TM文档,glob ? 没有提及
可视作新功能
https://developer.chrome.com/docs/extensions/reference/manifest/content-scripts?hl=zh-CN#match-urls
checkUrlMatch
@match@include@exclude是否按照MV3的 match patternhttps://www.google.com/*会分析成 "https", "www.google.com", "*"*://*.google.com/会分析成 "*", ".google.com", ""globSplit
*hello****world*会变成*hello*world**hello****?????world*会变成*hello*world*(理论上MV3的处理可支持这个*?,但脚本的代码部份复杂,不考虑)metaUMatchAnalyze
["@match xxxx", "@include xxxx", "@exclude xxxx"]做拆件@include)@exlcude)isUrlMatch
isUrlMatchPattern
isUrlMatchGlob
isUrlMatchRegEx
addMatchesToGlobs
*://*/*跟 10个globisAllUrlsRequired
*://*/*要转成<all_urls>getApiMatchesAndGlobs
@match@include转化为 MV3 的 match 和 includeGlob注意点
参考: https://www.tampermonkey.net/documentation.php?locale=en
参考: https://wiki.greasespot.net/Include_and_exclude_rules
@include和@exclude的case-insensitive@include和@exclude中,glob pattern和regex pattern 的case-insensitive的行为。@include@exclude) 这会与TM有差异。case-sensitive. 改成regex的话无法利用原生API注入@include自动对应成@match@include的实际行为既非match pattern也非glob pattern*.developer.chrome.com等写法会因为视作match pattern而能匹配developer.chrome.com而 TM只当作glob pattern 因此不会匹配developer.chrome.commatch pattern的
port问题You can also use http://*:*/* to match localhost, IP addresses, and any port.但没有清楚写出 port 的部份@include@exclude@match, 而要放在@include。这会与TM有差异。scheme 不对应 about
about:blank的脚本执行。但感觉对写脚本没用scheme 不对应 unmht
不对应 tld (
.tld/会改为.??*/)tld这个pattern*://*.amazon.tld/*改成标準的glob*://*.amazon.??*/*支持
glob ?glob ?@include和@exclude的?不会被视为 query?regex pattern 支持 flag
@include /^https?://*GOOGLE*$/i这个写法可以/^https?://*GOOGLE*$/m这样宣告 )与VM的差异
@exclude-match. SC自动检查@exclude对应. 由於是排除,不用像@match那样做严格控制@match不支持http*://(在match pattern 定义裡,http*://等价於*://*)@match不支持www.google.*(支持的话其实是违反@match原意)@match不支持. tld(支持的话其实是违反@match原意)*严格跟随 glob 原则,如要至少一个,需要?*。因此按照MV3标準,https://www.google.com/*会匹对https://www.google.com/。此和TM一致往后应该可以在脚本猫的界面显示出,转化后的 match pattern, glob pattern, 和 regex. 这样用家在使用/开发时就能清晰些