Skip to content

Commit

Permalink
🐛 修复@match 端口匹配错误 #244
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Dec 18, 2023
1 parent c04a829 commit 2e378c3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scriptcat",
"version": "0.16.0-beta",
"version": "0.16.0-beta.1",
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
"author": "CodFrm",
"license": "GPLv3",
Expand Down
6 changes: 5 additions & 1 deletion src/pkg/utils/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ describe("UrlMatch-port2", () => {
url.add("http://test.list.ggnb.top:80/search", "ok1");
url.add("http://test.list.ggnb.top*/search", "ok2");
url.add("http://test.list.ggnb.top:*/search", "ok3");
url.add("http://localhost:3000/", "ok4");
it("match1", () => {
expect(url.match("http://test.list.ggnb.top:80/search")).toEqual([
"ok1",
"ok2",
"ok3",
]);
expect(url.match("http://test.list.ggnb.top:81/search")).toEqual([
"ok1",
"ok2",
"ok3",
]);
Expand All @@ -133,6 +133,10 @@ describe("UrlMatch-port2", () => {
"ok3",
]);
});
it("case2", () => {
expect(url.match("http://localhost:3000/")).toEqual(["ok4"]);
expect(url.match("http://localhost:8000/")).toEqual([]);
});
});

describe("特殊情况", () => {
Expand Down
11 changes: 9 additions & 2 deletions src/pkg/utils/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export default class Match<T> {
pos = -1;
} else if (u.host.endsWith("*")) {
// 处理*结尾
u.host = u.host.substring(0, u.host.length - 1);
if (!u.host.endsWith(":*")) {
u.host = u.host.substring(0, u.host.length - 1);
}
} else if (pos !== -1 && pos !== 0) {
return "";
}
Expand All @@ -75,7 +77,12 @@ export default class Match<T> {
if (pos2 === -1) {
u.host = `${u.host}(:\\d+)?`;
} else {
u.host = `${u.host.substring(0, pos2)}(:\\d+)?`;
const port = u.host.substring(pos2 + 1);
if (port === "*") {
u.host = `${u.host.substring(0, pos2)}(:\\d+)?`;
} else {
u.host = `${u.host.substring(0, pos2)}(:${port})?`;
}
}
let re = `^${u.scheme}://${u.host}`;
if (u.path === "/") {
Expand Down

0 comments on commit 2e378c3

Please sign in to comment.