Skip to content

Commit 1b70fce

Browse files
CodFrmcyfung1031
andauthored
🐛 修复 include *?* 表达式处理问题 #1271 (#1272)
* 🐛 修复 include *?* 表达式处理问题 #1271 * Added error handling to avoid crash * update globSplit * update globSplit * update globSplit --------- Co-authored-by: cyfung1031 <44498510+cyfung1031@users.noreply.github.com>
1 parent 4617709 commit 1b70fce

6 files changed

Lines changed: 34 additions & 10 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scriptcat",
3-
"version": "1.3.0-beta.4",
3+
"version": "1.3.0",
44
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
55
"author": "CodFrm",
66
"license": "GPLv3",

src/app/repo/resource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class ResourceDAO extends Repo<Resource> {
6464
}
6565

6666
// CompiledResource结构变更时,建议修改 CompiledResourceNamespace 以删除旧Cache
67-
export const CompiledResourceNamespace = "a51b9167-fdde-467a-a86f-75e5636adda2";
67+
export const CompiledResourceNamespace = "57d79c56-231a-42d3-b6e3-d2004ba0866f";
6868

6969
export class CompiledResourceDAO extends Repo<CompiledResource> {
7070
constructor() {

src/app/service/content/script_executor.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ export class ScriptExecutor {
106106
// "@exclude /REGEX/" 的情况下,MV3 UserScripts API 基础匹配范围不会扩大,然后在 earlyScript 把符合 REGEX 的匹配除去
107107
// (Any @exclude = true -> 除去)
108108
// 注:如果一早已被除排,根本不会被 MV3 UserScripts API 注入。所以只考虑排除「多余的匹配」。(略过注入)
109-
if (isUrlExcluded(window.location.href, detail.scriptInfo.scriptUrlPatterns)) {
110-
// 「多余的匹配」-> 略过注入
111-
return;
109+
try {
110+
if (isUrlExcluded(window.location.href, detail.scriptInfo.scriptUrlPatterns)) {
111+
// 「多余的匹配」-> 略过注入
112+
return;
113+
}
114+
} catch (e) {
115+
console.warn("Unexpected match error", e);
112116
}
113117
}
114118
this.execEarlyScript(scriptFlag, detail.scriptInfo, envInfo);

src/pkg/utils/match.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,3 +889,13 @@ describe.concurrent("@include /REGEX/", () => {
889889
expect(isUrlIncluded("http://www.hlample.com/", url.rulesMap.get("ok1")!)).toEqual(false);
890890
});
891891
});
892+
893+
describe.concurrent("invalid or unsupported glob #1271", () => {
894+
const url = new UrlMatch<string>();
895+
url.addInclude("*://*?*", "ok1");
896+
url.addInclude("*://*?page*", "ok2");
897+
it.concurrent("include *://*?*", () => {
898+
expect(url.urlMatch("http://www.example.com/?a=1")).toEqual(["ok1"]);
899+
expect(url.urlMatch("http://www.example.com/?page=1")).toEqual(["ok1", "ok2"]);
900+
});
901+
});

src/pkg/utils/match.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ export class UrlMatch<T> {
1818
if (cacheMap.has(url)) return cacheMap.get(url) as T[];
1919
const res: T[] = [];
2020
for (const [uuid, rules] of this.rulesMap) {
21-
if (isUrlIncluded(url, rules)) {
22-
res.push(uuid);
21+
try {
22+
if (isUrlIncluded(url, rules)) {
23+
res.push(uuid);
24+
}
25+
} catch (e) {
26+
console.warn("Unexpected match error", e);
2327
}
2428
}
2529
const sorter = this.sorter;

src/pkg/utils/url_matcher.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ export function checkUrlMatch(s: string) {
5757
}
5858

5959
const globSplit = (text: string) => {
60-
text = text.replace(/\*{2,}/g, "*"); // api定义的 glob * 是等价于 glob **
61-
text = text.replace(/\*(\?+)/g, "$1*"); // "*????" 改成 "????*",避免 backward 处理
62-
return text.split(/([*?])/g);
60+
const split = text.split(/([*?]{2,})/g);
61+
for (let i = 1; i < split.length; i += 2) {
62+
// "*????" 改成 "????*",避免 backward 处理
63+
// api定义的 glob * 是等价于 glob **
64+
const p = split[i]; // **??**??**
65+
const q = p.replace(/\*/g, ""); // ????
66+
if (p !== q) split[i] = `${q}*`; // ????*
67+
}
68+
return split.join("").split(/([*?])/g);
6369
};
6470

6571
export const extractUrlPatterns = (lines: string[]): URLRuleEntry[] => {

0 commit comments

Comments
 (0)