Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/pkg/utils/match.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { dealPatternMatches, parsePatternMatchesURL, UrlMatch } from "./match";
import { v4 as uuidv4 } from "uuid";

// https://developer.chrome.com/docs/extensions/mv3/match_patterns/
describe("UrlMatch-google", () => {
Expand Down Expand Up @@ -132,6 +133,16 @@ describe("UrlMatch-port2", () => {
});
});

describe("UrlMatch-exclude", () => {
it("exclue-port", () => {
const url = new UrlMatch<string>();
url.add("*://*/*", "ok3");
url.exclude("*:5244*", "ok3");
expect(url.match("http://test.list.ggnb.top:5244/search")).toEqual([]);
expect(url.match("http://test.list.ggnb.top:80/search")).toEqual(["ok3"]);
});
});

// https://developer.chrome.com/docs/extensions/mv3/match_patterns/
describe("dealPatternMatches", () => {
it("https://developer.chrome.com/docs/extensions/develop/concepts/match-patterns?hl=zh-cn#examples", () => {
Expand Down Expand Up @@ -299,3 +310,50 @@ describe("parsePatternMatchesURL", () => {
});
});
});

const makeUrlMatcher = (uuid: string, matchesList: string[], excludeMatchesList: string[]) => {
const patternMatches = dealPatternMatches(matchesList);
const matchesResult = patternMatches.result;
const matches = patternMatches.patternResult;
const result = dealPatternMatches(excludeMatchesList, {
exclude: true,
});
const excludeMatchesResult = result.result;
const excludeMatches = result.patternResult;

const urlMatcher = new UrlMatch<string>();
for (const match of matchesResult) {
urlMatcher.add(match, uuid);
}
for (const exclude of excludeMatchesResult) {
urlMatcher.exclude(exclude, uuid);
}

return { urlMatcher, matches, excludeMatches };
};

describe("UrlMatch-exclusion", () => {
it("exclusion-1", () => {
const matchesList: string[] = ["*://**/*"];
const excludeMatchesList: string[] = [
"*://steamcommunity.com/*",
"*.jd.com/*",
"*docs.google.com/*",
"*://*.amazon.tld/*",
"*shop*",
"/.*(?<!test)store.*/",
"*/releases",
"*/releases/*",
"*:5244*",
];
const uuid = uuidv4();
const { urlMatcher } = makeUrlMatcher(uuid, matchesList, excludeMatchesList);
expect(urlMatcher.match("https://foo.api.bar/baz")).toEqual([uuid]);
expect(urlMatcher.match("https://steamcommunity.com/foo")).toEqual([]);
expect(urlMatcher.match("https://jd.com/foo")).toEqual([]);
expect(urlMatcher.match("https://docs.google.com/foo")).toEqual([]);
expect(urlMatcher.match("https://amazon.com/foo")).toEqual([]);
expect(urlMatcher.match("https://test.store.com/aaa")).toEqual([]);
expect(urlMatcher.match("https://foo.api.bar:5244/baz")).toEqual([]);
});
});
30 changes: 3 additions & 27 deletions src/pkg/utils/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,14 @@ export default class Match<T> {
search: match[5],
};
}
// 处理一些特殊情况
switch (url) {
case "*":
case "http*":
return {
scheme: "*",
host: "*",
path: "*",
search: "*",
};
default:
// 无*://的情况
if (!url.includes("://")) {
// 直接转为通配符
const match = /^(.*?)((\/.*?)(\?.*?|)|)$/.exec(url);
if (match) {
return {
scheme: "*",
host: match[1],
path: match[3] || (url[url.length - 1] === "*" ? "*" : "/"),
search: match[4],
};
}
}
}
return undefined;
}

protected compileRe(url: string): string {
const u = this.parseURL(url);
if (!u) {
return "";
// 直接将*替换为正则
return url.replace(/\*/g, ".*");
}
switch (u.scheme) {
case "*":
Expand All @@ -82,7 +58,7 @@ export default class Match<T> {
}
// 处理顶域
if (u.host.endsWith("tld")) {
u.host = `${u.host.substr(0, u.host.length - 3)}.*?`;
u.host = `${u.host.substring(0, u.host.length - 3)}.*?`;
}
// 处理端口
const pos2 = u.host.indexOf(":");
Expand Down