Conversation
7492b3a to
92114bf
Compare
|
日後會提PR重寫這個部份。
需參考 https://www.tampermonkey.net/documentation.php?locale=en#meta:include MV3 UserScriptAPI URL Matchingmatches 是 mandatory MV3 Patternhttps://developer.chrome.com/docs/extensions/develop/concepts/match-patterns?hl=en includeGlobs 同理 決定 match vs glob沒有 MV3 Pattern檢測如果是有效MV3 glob rule, 沒有正規表達,可以直接注入userscript API 而不用溝通。(跳過 pageLoad溝通 ) Glob PatternMV3 文檔標明 glob * () 和 glob ? 都支持 注:這裡的glob * (**) 是指glob ** (不考慮資料夾層結構) Match Patternhttps://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns
file 不包括在 * 內,與 TM文檔 一致 host 只有三個寫法
host定義 與 TM文檔 一致 pathname 支持 glob * (**) 和 glob ? 相容性
|
不清楚你想怎麼修 #617 (沒想到 上面寫的大多都是筆記用 |
src/pkg/utils/match.test.ts
Outdated
| const uuid = uuidv4(); | ||
| const { urlMatcher, excludeMatches } = makeUrlMatcher(uuid, matchesList, excludeMatchesList); | ||
| expect(urlMatcher.match("https://foo.api.bar/baz")).toEqual([uuid]); | ||
| expect(excludeMatches.includes("*://*/*")).toEqual(false); |
There was a problem hiding this comment.
似乎无法通过,你给的这三个表达式是无法转换成 PatternMatches 的,所以只能转换为 *://*/* ,然后再match的时候再用原表达式处理
'*://*.amazon.tld/*', // 因为tld是表示顶域,所以不可以转换
'*shop*',// 没有上下文,不知道这是域名,还是路径
'/.*(?<!jav)store.*/', // 正则表达式 无法处理
There was a problem hiding this comment.
'*shop*' 這個可以處理,但現在沒有相關功能,日後PR再算
目前的代碼,這些現在都是正規處理吧
excludeMatches 應該都是沒有,而放在 .rule 裡面做正規配對?
|
我修改了一版,删除了一些特殊的处理逻辑,直接一股脑的将*替换成正则表达式 原本我是希望尽量符合扩展的Match patterns规范的,但是UserScript的match实在是太乱了 |
我注释掉了,是这样吗? |
match.ts 是不是應該把 scriptcat/src/pkg/utils/match.ts Lines 351 to 376 in 71e97d5 |
不是轉換 符合扩展的Match patterns规范 都轉換 |
|
因为他会返回两个,一个是:result,另外一个是patternResult, 我大概懂你意思了,确实也不应该返回 |
|
scriptcat/src/app/service/service_worker/runtime.ts Lines 754 to 787 in e206562 这里是处理排除的逻辑,是使用的result,黑名单排除才是使用的patternResult |
|
如果是match匹配的逻辑,返回 scriptcat/src/app/service/service_worker/runtime.ts Lines 744 to 751 in e206562 |
https://www.tampermonkey.net/documentation.php?locale=en#meta:include 所以TM也是這樣寫。如果 |
在mv2的时候是区分 UrlInclude 和 UrlMatch的,MV3后,就索性合并一起了,可以考虑分开来吧,不过得梳理一下了 |
|
"轉換不太成功" 是指的哪个? |
|
寫了個方法判別是 標準match 還是要 regular expression 轉換 function checkUrlMatch(s: string) {
s = s.trim();
const idx1 = s.indexOf("://");
let idx2 = -1;
if (idx1 > 0) {
idx2 = s.indexOf("/", idx1 + 3);
}
let extMatch: string[] | null = null;
if (idx1 > 0 && idx2 > 0) {
const scheme = s.substring(0, idx1);
if (/^(\*|[-a-z]+)$/.test(scheme)) {
let host = s.substring(idx1 + 3, idx2);
if (!host.includes(":") && !host.startsWith(".")) {
if (/^(\*|\*\..+)$/.test(host)) host = host.substring(1);
if (!host.includes("*")) {
extMatch = [scheme, host, s.substring(idx2 + 1)];
}
}
}
}
return extMatch;
}
檢查用 function isUrlMatch(s: string, m: string[]) {
const url = new URL(s);
if (m[0] !== "*" && url.protocol !== `${m[0]}:`) return false;
if (m[1]) {
if (m[1].startsWith(".")) {
if (!`.${url.hostname}`.endsWith(`${m[1]}`)) return false;
} else {
if (`${url.hostname}` !== `${m[1]}`) return false;
}
}
const path = `${url.pathname}?${url.search}`;
const arr = m[2].split("*");
let idx = 0;
let k = 0;
const l = arr.length;
while (k < l) {
if (arr[k]) {
const jdx = path.indexOf(arr[k], idx);
if (jdx < 0) return false;
idx = jdx + arr[k].length;
}
k++;
}
// 未處理 path 完全一致的檢查
return true;
}function isUrlMatch(s: string, m: string[]) {
const url = new URL(s);
if (m[0] !== "*" && url.protocol !== `${m[0]}:`) return false;
if (m[1]) {
if (m[1].startsWith(".")) {
if (!`.${url.hostname}`.endsWith(`${m[1]}`)) return false;
} else {
if (`${url.hostname}` !== `${m[1]}`) return false;
}
}
const path = `${url.pathname}?${url.search}`.substring(1);
const arr = m[2].split("*");
let idx = 0;
let k = 0;
const l = arr.length;
const pathMatches = new Array(arr.length - 1);
if (!path.startsWith(`${arr[0]}`)) return false;
idx = `${arr[0]}`.length;
k = 1;
while (k < l) {
if (k === l - 1 && arr[k] === "") {
pathMatches[k - 1] = path.substring(idx);
idx = path.length;
break;
}
const jdx = path.indexOf(arr[k], idx);
if (jdx < 0) return false;
pathMatches[k - 1] = path.substring(idx, jdx);
idx = jdx + arr[k].length;
k++;
}
return idx === path.length || (path.endsWith("?") && idx === path.length - 1);
} |
|
似乎没有问题呀,可以匹配 |
應該是 |
|
懂你意思了,确实需要转义 |
|
假设你的表达式就是一个正则 |
要 /.*?example.com/ 這樣呀 https://www.tampermonkey.net/documentation.php?locale=en#meta:include 沒有 / / 就是要轉換
|



https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns#host
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns#path