match
Some checks failed
build / Build (push) Failing after 6s
test / Run tests (push) Failing after 8s
Some checks failed
build / Build (push) Failing after 6s
test / Run tests (push) Failing after 8s
This commit is contained in:
11
src/pkg/utils/match.test.ts
Normal file
11
src/pkg/utils/match.test.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { describe, it } from "vitest";
|
||||
import { dealMatches, parseURL } from "./match";
|
||||
|
||||
// https://developer.chrome.com/docs/extensions/mv3/match_patterns/
|
||||
describe("dealMatches", () => {
|
||||
it("*://link.17173.com*", () => {
|
||||
const url = parseURL("*://link.17173.com*");
|
||||
const matches = dealMatches(["*://link.17173.com*"]);
|
||||
console.log(url, matches);
|
||||
});
|
||||
});
|
52
src/pkg/utils/match.ts
Normal file
52
src/pkg/utils/match.ts
Normal file
@ -0,0 +1,52 @@
|
||||
export interface Url {
|
||||
scheme: string;
|
||||
host: string;
|
||||
path: string;
|
||||
search: string;
|
||||
}
|
||||
|
||||
// 根据https://developer.chrome.com/docs/extensions/develop/concepts/match-patterns?hl=zh-cn进行匹配
|
||||
export class Match {}
|
||||
|
||||
export function parseURL(url: string): Url | undefined {
|
||||
const match = /^(.+?):\/\/(.*?)((\/.*?)(\?.*?|)|)$/.exec(url);
|
||||
if (match) {
|
||||
return {
|
||||
scheme: match[1],
|
||||
host: match[2],
|
||||
path: match[4] || (url[url.length - 1] === "*" ? "*" : "/"),
|
||||
search: match[5],
|
||||
};
|
||||
}
|
||||
// 处理一些特殊情况
|
||||
switch (url) {
|
||||
case "*":
|
||||
return {
|
||||
scheme: "*",
|
||||
host: "*",
|
||||
path: "*",
|
||||
search: "*",
|
||||
};
|
||||
default:
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// 处理油猴的match和include为chrome的matches
|
||||
export function dealMatches(matches: string[]) {
|
||||
const result: string[] = [];
|
||||
for (let i = 0; i < matches.length; i++) {
|
||||
const url = parseURL(matches[i]);
|
||||
if (url) {
|
||||
// *开头但是不是*.的情况
|
||||
if (url.host.startsWith("*")) {
|
||||
if (!url.host.startsWith("*.")) {
|
||||
// 删除开头的*号
|
||||
url.host = url.host.slice(1);
|
||||
}
|
||||
}
|
||||
result.push(`${url.scheme}://${url.host}${url.path}` + (url.search ? "?" + url.search : ""));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
@ -217,13 +217,3 @@ export function sleep(time: number) {
|
||||
setTimeout(resolve, time);
|
||||
});
|
||||
}
|
||||
|
||||
// 使service_worker长时间存活
|
||||
export async function waitUntil(promise: Promise<any>) {
|
||||
const keepAlive = setInterval(chrome.runtime.getPlatformInfo, 25 * 1000);
|
||||
try {
|
||||
await promise;
|
||||
} finally {
|
||||
clearInterval(keepAlive);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user