Skip to content

Commit 9ebdeb9

Browse files
committed
👔 兼容油猴match http*
1 parent d9b9af0 commit 9ebdeb9

3 files changed

Lines changed: 157 additions & 157 deletions

File tree

src/pkg/match.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@ export class Match<T> {
99
return {
1010
scheme: match[1],
1111
host: match[2],
12-
path: match[4] || "/",
12+
path: match[4] || '/',
1313
search: match[5],
1414
};
1515
}
16+
if (url == 'http*') {
17+
return { scheme: '*', host: '*', path: '', search: '' };
18+
}
1619
match = /^(.*?)((\/.*?)(\?.*?|)|)$/.exec(url);
1720
if (match) {
1821
return {
1922
scheme: '*',
2023
host: match[1],
21-
path: match[3] || "/",
24+
path: match[3] || '/',
2225
search: match[4],
2326
};
2427
}
2528
return undefined;
2629
}
2730

2831
protected compileRe(url: string): string {
29-
let u = this.parseURL(url);
32+
const u = this.parseURL(url);
3033
if (!u) {
3134
return '';
3235
}
@@ -40,27 +43,27 @@ export class Match<T> {
4043
}
4144
u.host = u.host.replace(/\*/g, '.*?');
4245
// 处理 *.开头
43-
if (u.host.startsWith(".*?.")) {
44-
u.host = "(.*?\.?)" + u.host.substr(4);
46+
if (u.host.startsWith('.*?.')) {
47+
u.host = '(.*?\.?)' + u.host.substr(4);
4548
}
4649
// 处理顶域
4750
if (u.host.endsWith('tld')) {
4851
u.host = u.host.substr(0, u.host.length - 3) + '.*?';
4952
}
50-
let re: string = `^${u.scheme}://${u.host}`;
53+
let re = `^${u.scheme}://${u.host}`;
5154
if (u.path == '/') {
5255
re += '[/]?';
5356
} else {
5457
re += u.path.replace(/\*/g, '.*?');
5558
}
5659
if (u.search) {
57-
re += u.search.replace(/([\?])/g, "\\$1").replace(/\*/g, '.*?');
60+
re += u.search.replace(/([\?])/g, '\\$1').replace(/\*/g, '.*?');
5861
}
59-
return re.replace(/\//g, "\/") + '$';
62+
return re.replace(/\//g, '\/') + '$';
6063
}
6164

6265
public add(url: string, val: T) {
63-
let re = this.compileRe(url);
66+
const re = this.compileRe(url);
6467
if (!re) {
6568
return;
6669
}
@@ -70,7 +73,7 @@ export class Match<T> {
7073
this.rule.set(re, rule);
7174
}
7275
rule.push(val);
73-
this.delCache(val);
76+
this.delCache();
7477
}
7578

7679
public match(url: string): T[] {
@@ -81,9 +84,9 @@ export class Match<T> {
8184
ret = [];
8285
this.rule.forEach((val, key) => {
8386
try {
84-
let re = new RegExp(key);
87+
const re = new RegExp(key);
8588
if (re.test(url)) {
86-
ret!.push(...val);
89+
ret && ret.push(...val);
8790
}
8891
} catch (_) {
8992

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

9699
protected getId(val: T): string {
97100
if (typeof val == 'object') {
98-
return (<any>val).id;
101+
return (<{ id: string }><unknown>val).id;
99102
}
100103
return <string><unknown>val;
101104
}
102105

103106
public del(val: T) {
104-
let id = this.getId(val);
107+
const id = this.getId(val);
105108
this.rule.forEach((rule, key) => {
106-
let tmp: T[] = [];
109+
const tmp: T[] = [];
107110
rule.forEach(val => {
108111
if (this.getId(val) != id) {
109112
tmp.push(val);
@@ -115,10 +118,10 @@ export class Match<T> {
115118
this.rule.delete(key);
116119
}
117120
});
118-
this.delCache(val);
121+
this.delCache();
119122
}
120123

121-
protected delCache(delVal: T) {
124+
protected delCache() {
122125
this.cache.clear();
123126
}
124127

@@ -134,19 +137,19 @@ export class UrlMatch<T> extends Match<T>{
134137
public match(url: string): T[] {
135138
let ret = super.match(url);
136139
// 排除
137-
let includeMap = new Map();
140+
const includeMap = new Map();
138141
ret.forEach(val => {
139142
includeMap.set(this.getId(val), val);
140143
})
141-
let exclude = this.excludeMatch.match(url);
142-
let excludeMap = new Map();
144+
const exclude = this.excludeMatch.match(url);
145+
const excludeMap = new Map();
143146
exclude.forEach(val => {
144147
excludeMap.set(this.getId(val), 1);
145148
})
146149
ret = [];
147-
includeMap.forEach((val, key) => {
150+
includeMap.forEach((val: T, key) => {
148151
if (!excludeMap.has(key)) {
149-
ret!.push(val);
152+
ret.push(val);
150153
}
151154
})
152155
this.cache.set(url, ret);

0 commit comments

Comments
 (0)