Skip to content

Commit f5183bd

Browse files
committed
🐛 修复端口通配的问题 #30
1 parent 1802c1c commit f5183bd

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/apps/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export const Server = process.env.NODE_ENV == 'production' ? 'https://sc.icodef.
55
export const ExternalWhitelist = [
66
'greasyfork.org',
77
'scriptcat.org',
8+
'tampermonkey.net.cn',
89
'openuserjs.org',
910
];

src/pkg/match.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class Match<T> {
1414
};
1515
}
1616
if (url == 'http*') {
17-
return { scheme: '*', host: '*', path: '', search: '' };
17+
return { scheme: '*', host: '*', path: '*', search: '*' };
1818
}
1919
match = /^(.*?)((\/.*?)(\?.*?|)|)$/.exec(url);
2020
if (match) {
@@ -41,10 +41,10 @@ export class Match<T> {
4141
u.scheme = 'http[s]';
4242
break;
4343
}
44-
u.host = u.host.replace(/\*/g, '.*?');
44+
u.host = u.host.replace(/\*/g, '[^/]*?');
4545
// 处理 *.开头
46-
if (u.host.startsWith('.*?.')) {
47-
u.host = '(.*?\.?)' + u.host.substr(4);
46+
if (u.host.startsWith('[^/]*?.')) {
47+
u.host = '([^/]*?\.?)' + u.host.substr(7);
4848
}
4949
// 处理顶域
5050
if (u.host.endsWith('tld')) {

tests/match.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ describe('UrlMatch2', () => {
7979
const url = new UrlMatch<string>();
8080
it('http*', () => {
8181
url.add('http*', 'ok1');
82-
expect(url.match('http://www.http*.com')).toEqual(['ok1']);
82+
expect(url.match('http://www.http.com')).toEqual(['ok1']);
83+
});
84+
it('port', () => {
85+
url.add('http://domain:8080', 'ok2');
86+
expect(url.match('http://domain:8080/')).toEqual(['ok1', 'ok2']);
87+
expect(url.match('http://domain:8080/123')).toEqual(['ok1']);
88+
});
89+
it('无/', () => {
90+
url.add('http://domain2', 'ok3');
91+
url.add('http://domain2*', 'ok4');
92+
expect(url.match('http://domain2/')).toEqual(['ok1', 'ok3', 'ok4']);
93+
expect(url.match('http://domain2.com/')).toEqual(['ok1', 'ok4']);
94+
expect(url.match('http://domain2/123')).toEqual(['ok1']);
95+
})
96+
});
97+
98+
describe('UrlMatch3', () => {
99+
const url = new UrlMatch<string>();
100+
it('port', () => {
101+
url.add('http://domain:*/', 'ok1');
102+
expect(url.match('http://domain:8080/')).toEqual(['ok1']);
103+
expect(url.match('http://domain:8080')).toEqual(['ok1']);
104+
expect(url.match('http://domain:8080/123')).toEqual([]);
83105
});
84106
});

0 commit comments

Comments
 (0)