修复一些资源加载问题
This commit is contained in:
@ -43,10 +43,10 @@ describe("UrlMatch-google-error", () => {
|
||||
url.add("https://*foo/bar", "ok1");
|
||||
}).toThrow(Error);
|
||||
});
|
||||
// 从v0.17.0开始允许这种
|
||||
it("error-2", () => {
|
||||
expect(() => {
|
||||
url.add("https://foo.*.bar/baz", "ok1");
|
||||
}).toThrow(Error);
|
||||
url.add("https://foo.*.bar/baz", "ok1");
|
||||
expect(url.match("https://foo.api.bar/baz")).toEqual(["ok1"]);
|
||||
});
|
||||
it("error-3", () => {
|
||||
expect(() => {
|
||||
@ -70,6 +70,13 @@ describe("UrlMatch-search", () => {
|
||||
expect(url.match("https://bbs.tampermonkey.net.cn/")).toEqual(["ok2"]);
|
||||
expect(url.match("https://bbs.tampermonkey.net.cn/foo/bar.html")).toEqual([]);
|
||||
});
|
||||
it("http://api.*.example.com/*", () => {
|
||||
const url = new UrlMatch<string>();
|
||||
url.add("http://api.*.example.com/*", "ok1");
|
||||
expect(url.match("http://api.foo.example.com/")).toEqual(["ok1"]);
|
||||
expect(url.match("http://api.bar.example.com/")).toEqual(["ok1"]);
|
||||
expect(url.match("http://api.example.com/")).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("UrlMatch-port1", () => {
|
||||
@ -106,13 +113,47 @@ describe("UrlMatch-port2", () => {
|
||||
// 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", () => {
|
||||
const matches = dealPatternMatches(["https://*/*", "http://127.0.0.1/*", "http://127.0.0.1/"]);
|
||||
expect(matches.patternResult).toEqual(["https://*/*", "http://127.0.0.1/*", "http://127.0.0.1/"]);
|
||||
const matches = dealPatternMatches([
|
||||
"https://*/*",
|
||||
"http://127.0.0.1/*",
|
||||
"http://127.0.0.1/",
|
||||
"https://*.example.com/*",
|
||||
]);
|
||||
expect(matches.patternResult).toEqual([
|
||||
"https://*/*",
|
||||
"http://127.0.0.1/*",
|
||||
"http://127.0.0.1/",
|
||||
"https://*.example.com/*",
|
||||
]);
|
||||
});
|
||||
// 处理一些特殊情况
|
||||
it("*://link.17173.com*", () => {
|
||||
const matches = dealPatternMatches(["*://link.17173.com*"]);
|
||||
expect(matches.patternResult).toEqual(["*://link.17173.com/*"]);
|
||||
it("特殊情况", () => {
|
||||
const matches = dealPatternMatches([
|
||||
"*://www.example.com*",
|
||||
"*://api.*.example.com/*",
|
||||
"*://api.*.*.example.com/*",
|
||||
"*://*example.com/*",
|
||||
]);
|
||||
expect(matches.patternResult).toEqual([
|
||||
"*://www.example.com/*",
|
||||
"*://*.example.com/*",
|
||||
"*://*.example.com/*",
|
||||
"*://example.com/*",
|
||||
]);
|
||||
expect(matches.result).toEqual([
|
||||
"*://www.example.com*",
|
||||
"*://api.*.example.com/*",
|
||||
"*://api.*.*.example.com/*",
|
||||
"*://*example.com/*",
|
||||
]);
|
||||
});
|
||||
it("特殊情况-exclude", () => {
|
||||
const matches = dealPatternMatches(["*://api.*.example.com/*", "*://api.*.*.example.com/*"], {
|
||||
exclude: true,
|
||||
});
|
||||
console.log(matches);
|
||||
expect(matches.patternResult).toEqual(["*://example.com/*", "*://example.com/*"]);
|
||||
expect(matches.result).toEqual(["*://api.*.example.com/*", "*://api.*.*.example.com/*"]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -146,11 +187,19 @@ describe("parsePatternMatchesURL", () => {
|
||||
path: "*",
|
||||
});
|
||||
});
|
||||
it("*://link.17173.com*", () => {
|
||||
const matches = parsePatternMatchesURL("*://link.17173.com*");
|
||||
it("*://www.example.com*", () => {
|
||||
const matches = parsePatternMatchesURL("*://www.example.com*");
|
||||
expect(matches).toEqual({
|
||||
scheme: "*",
|
||||
host: "link.17173.com",
|
||||
host: "www.example.com",
|
||||
path: "*",
|
||||
});
|
||||
});
|
||||
it("*://api.*.example.com/*", () => {
|
||||
const matches = parsePatternMatchesURL("*://api.*.example.com/*");
|
||||
expect(matches).toEqual({
|
||||
scheme: "*",
|
||||
host: "*.example.com",
|
||||
path: "*",
|
||||
});
|
||||
});
|
||||
|
@ -69,8 +69,6 @@ export default class Match<T> {
|
||||
if (!u.host.endsWith(":*")) {
|
||||
u.host = u.host.substring(0, u.host.length - 1);
|
||||
}
|
||||
} else if (pos !== -1 && pos !== 0) {
|
||||
return "";
|
||||
}
|
||||
u.host = u.host.replace(/\*/g, "[^/]*?");
|
||||
// 处理 *.开头
|
||||
@ -225,7 +223,12 @@ export interface PatternMatchesUrl {
|
||||
}
|
||||
|
||||
// 解析URL, 根据https://developer.chrome.com/docs/extensions/develop/concepts/match-patterns?hl=zh-cn进行处理
|
||||
export function parsePatternMatchesURL(url: string): PatternMatchesUrl | undefined {
|
||||
export function parsePatternMatchesURL(
|
||||
url: string,
|
||||
options?: {
|
||||
exclude?: boolean;
|
||||
}
|
||||
): PatternMatchesUrl | undefined {
|
||||
let result: PatternMatchesUrl | undefined;
|
||||
const match = /^(.+?):\/\/(.*?)(\/(.*?)(\?.*?|)|)$/.exec(url);
|
||||
if (match) {
|
||||
@ -260,17 +263,38 @@ export function parsePatternMatchesURL(url: string): PatternMatchesUrl | undefin
|
||||
if (result.host.endsWith("*")) {
|
||||
result.host = result.host.slice(0, -1);
|
||||
}
|
||||
// 处理 www.*.example.com 的情况为 *.example.com
|
||||
const pos = result.host.lastIndexOf("*");
|
||||
if (pos > 0 && pos < result.host.length - 1) {
|
||||
if (options && options.exclude) {
|
||||
// 如果是exclude, 按最小匹配处理
|
||||
// 包括*也去掉
|
||||
result.host = result.host.substring(pos + 1);
|
||||
if (result.host.startsWith(".")) {
|
||||
result.host = result.host.substring(1);
|
||||
}
|
||||
} else {
|
||||
// 如果不是exclude
|
||||
// 将*前面的全部去掉
|
||||
result.host = result.host.substring(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 处理油猴的match和include为chrome的pattern-matche
|
||||
export function dealPatternMatches(matches: string[]) {
|
||||
export function dealPatternMatches(
|
||||
matches: string[],
|
||||
options?: {
|
||||
exclude?: boolean;
|
||||
}
|
||||
) {
|
||||
const patternResult: string[] = [];
|
||||
const result: string[] = [];
|
||||
for (let i = 0; i < matches.length; i++) {
|
||||
const url = parsePatternMatchesURL(matches[i]);
|
||||
const url = parsePatternMatchesURL(matches[i], options);
|
||||
if (url) {
|
||||
patternResult.push(`${url.scheme}://${url.host}/${url.path}`);
|
||||
result.push(matches[i]);
|
||||
|
Reference in New Issue
Block a user