Skip to content

Commit

Permalink
👔 兼容油猴match http*
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jan 1, 2022
1 parent d9b9af0 commit 9ebdeb9
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 157 deletions.
47 changes: 25 additions & 22 deletions src/pkg/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ export class Match<T> {
return {
scheme: match[1],
host: match[2],
path: match[4] || "/",
path: match[4] || '/',
search: match[5],
};
}
if (url == 'http*') {
return { scheme: '*', host: '*', path: '', search: '' };
}
match = /^(.*?)((\/.*?)(\?.*?|)|)$/.exec(url);
if (match) {
return {
scheme: '*',
host: match[1],
path: match[3] || "/",
path: match[3] || '/',
search: match[4],
};
}
return undefined;
}

protected compileRe(url: string): string {
let u = this.parseURL(url);
const u = this.parseURL(url);
if (!u) {
return '';
}
Expand All @@ -40,27 +43,27 @@ export class Match<T> {
}
u.host = u.host.replace(/\*/g, '.*?');
// 处理 *.开头
if (u.host.startsWith(".*?.")) {
u.host = "(.*?\.?)" + u.host.substr(4);
if (u.host.startsWith('.*?.')) {
u.host = '(.*?\.?)' + u.host.substr(4);
}
// 处理顶域
if (u.host.endsWith('tld')) {
u.host = u.host.substr(0, u.host.length - 3) + '.*?';
}
let re: string = `^${u.scheme}://${u.host}`;
let re = `^${u.scheme}://${u.host}`;
if (u.path == '/') {
re += '[/]?';
} else {
re += u.path.replace(/\*/g, '.*?');
}
if (u.search) {
re += u.search.replace(/([\?])/g, "\\$1").replace(/\*/g, '.*?');
re += u.search.replace(/([\?])/g, '\\$1').replace(/\*/g, '.*?');
}
return re.replace(/\//g, "\/") + '$';
return re.replace(/\//g, '\/') + '$';
}

public add(url: string, val: T) {
let re = this.compileRe(url);
const re = this.compileRe(url);
if (!re) {
return;
}
Expand All @@ -70,7 +73,7 @@ export class Match<T> {
this.rule.set(re, rule);
}
rule.push(val);
this.delCache(val);
this.delCache();
}

public match(url: string): T[] {
Expand All @@ -81,9 +84,9 @@ export class Match<T> {
ret = [];
this.rule.forEach((val, key) => {
try {
let re = new RegExp(key);
const re = new RegExp(key);
if (re.test(url)) {
ret!.push(...val);
ret && ret.push(...val);
}
} catch (_) {

Expand All @@ -95,15 +98,15 @@ export class Match<T> {

protected getId(val: T): string {
if (typeof val == 'object') {
return (<any>val).id;
return (<{ id: string }><unknown>val).id;
}
return <string><unknown>val;
}

public del(val: T) {
let id = this.getId(val);
const id = this.getId(val);
this.rule.forEach((rule, key) => {
let tmp: T[] = [];
const tmp: T[] = [];
rule.forEach(val => {
if (this.getId(val) != id) {
tmp.push(val);
Expand All @@ -115,10 +118,10 @@ export class Match<T> {
this.rule.delete(key);
}
});
this.delCache(val);
this.delCache();
}

protected delCache(delVal: T) {
protected delCache() {
this.cache.clear();
}

Expand All @@ -134,19 +137,19 @@ export class UrlMatch<T> extends Match<T>{
public match(url: string): T[] {
let ret = super.match(url);
// 排除
let includeMap = new Map();
const includeMap = new Map();
ret.forEach(val => {
includeMap.set(this.getId(val), val);
})
let exclude = this.excludeMatch.match(url);
let excludeMap = new Map();
const exclude = this.excludeMatch.match(url);
const excludeMap = new Map();
exclude.forEach(val => {
excludeMap.set(this.getId(val), 1);
})
ret = [];
includeMap.forEach((val, key) => {
includeMap.forEach((val: T, key) => {
if (!excludeMap.has(key)) {
ret!.push(val);
ret.push(val);
}
})
this.cache.set(url, ret);
Expand Down
Loading

0 comments on commit 9ebdeb9

Please sign in to comment.