1
1
export class Rule {
2
2
constructor ( host , language ) {
3
- this . host = host || "" ;
4
- this . language = language || "" ;
3
+ // "host" is legacy name, better be "expr"
4
+ // The true host name is `this.canonicalDomain`
5
+ this . host = host || '' ;
6
+ this . language = language || '' ;
7
+ }
8
+
9
+ get isUniversalWidlcard ( ) {
10
+ return this . host === '*' ;
11
+ }
12
+
13
+ get isSubdomainWildcard ( ) {
14
+ return this . host . startsWith ( '*.' ) ;
5
15
}
6
16
7
17
get canonicalDomain ( ) {
8
- if ( this . host === '*' ) {
9
- return '*' ;
10
- } if ( this . host . startsWith ( '*.' ) ) {
11
- return '*.' + new URL ( `http://${ this . host . slice ( 2 ) } ` ) . host ;
18
+ if ( this . isUniversalWidlcard ) {
19
+ return null ;
20
+ } else if ( this . isSubdomainWildcard ) {
21
+ return new URL ( `http://${ this . host . slice ( 2 ) } ` ) . host ;
12
22
} else {
13
23
return new URL ( `http://${ this . host } ` ) . host ;
14
24
}
15
25
}
16
26
17
- get urlFilter ( ) {
18
- return `*://${ this . canonicalDomain } /*` ;
27
+ get permissionOrigins ( ) {
28
+ if ( this . isUniversalWidlcard ) {
29
+ return '*://*/*' ;
30
+ } else {
31
+ return `*://*.${ this . canonicalDomain } /*` ;
32
+ }
33
+ }
34
+
35
+ get regexFilter ( ) {
36
+ if ( this . isUniversalWidlcard ) {
37
+ return "https?:\\/\\/.*" ;
38
+ }
39
+ const escapedHost = this . canonicalDomain . replace ( '.' , '\\.' ) ;
40
+ if ( this . isSubdomainWildcard ) {
41
+ return `https?:\\/\\/([^\\/]+\\.)?${ escapedHost } \\/.*` ;
42
+ } else {
43
+ return `https?:\\/\\/${ escapedHost } \\/.*` ;
44
+ }
19
45
}
20
46
21
47
get rule ( ) {
48
+ let priority = this . host . split ( "." ) . length ;
49
+ if ( ! this . isUniversalWidlcard && ! this . isSubdomainWildcard ) {
50
+ // Let `example.com` precedent `*.example.com`
51
+ priority += 1 ;
52
+ }
53
+ console . debug ( `Rule P${ priority } ${ this . language } [${ this . host } ] /${ this . regexFilter } /` )
54
+
22
55
return {
23
56
id : Math . floor ( Math . random ( ) * Number . MAX_SAFE_INTEGER ) ,
24
57
action : {
@@ -33,9 +66,9 @@ export class Rule {
33
66
} ,
34
67
condition : {
35
68
resourceTypes : [ "main_frame" , "sub_frame" ] ,
36
- urlFilter : this . urlFilter
69
+ regexFilter : this . regexFilter
37
70
} ,
38
- priority : this . host . split ( "." ) . length
71
+ priority
39
72
} ;
40
73
}
41
74
0 commit comments