@@ -160,12 +160,9 @@ export function checkBlackWhiteList(script, url) {
160160 b = script . blackList ,
161161 hasWhiteList = w ?. length > 0 ,
162162 hasBlackList = b ?. length > 0 ,
163- // inWhiteList = w?.findIndex((_) => isUrlMatchPattern(url, _)) >= 0 ?? true,
164- // inBlackList = b?.findIndex((_) => isUrlMatchPattern(url, _)) >= 0 ?? false,
165- inWhiteList = isUrlMatchPattern ( url , w ) ?? true ,
166- inBlackList = isUrlMatchPattern ( url , b ) ?? false ,
167- inGlobalBlackList =
168- GlobalBlackList . findIndex ( ( _ ) => isUrlMatchPattern ( url , _ ) ) >= 0 ?? true ;
163+ inWhiteList = matchPatterns ( url , w ) ?? true ,
164+ inBlackList = matchPatterns ( url , b ) ?? false ,
165+ inGlobalBlackList = matchPatterns ( url , GlobalBlackList ) ;
169166
170167 let willRun =
171168 ! inGlobalBlackList &&
@@ -176,23 +173,50 @@ export function checkBlackWhiteList(script, url) {
176173 return willRun ;
177174}
178175
179- export function isUrlMatchPattern ( url , pattern ) {
180- // https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API
181- // return new URLPattern(pattern).test(url);
182-
183- // if (pattern.indexOf("*") < 0)
184- // return new URL(url).toString() == new URL(pattern).toString();
176+ // Source: https://github.com/fregante/webext-patterns/blob/main/index.ts
177+ function matchPatterns ( url , patterns ) {
178+ const patternValidationRegex =
179+ / ^ ( h t t p s ? | w s s ? | f i l e | f t p | \* ) : \/ \/ ( \* | \* \. [ ^ * / ] + | [ ^ * / ] + ) \/ .* $ | ^ f i l e : \/ \/ \/ .* $ | ^ r e s o u r c e : \/ \/ ( \* | \* \. [ ^ * / ] + | [ ^ * / ] + ) \/ .* $ | ^ a b o u t : / ;
180+ const isFirefox =
181+ typeof navigator === "object" && navigator . userAgent . includes ( "Firefox/" ) ;
182+ const allStarsRegex = isFirefox
183+ ? / ^ ( h t t p s ? | w s s ? ) : [ / ] [ / ] [ ^ / ] + ( [ / ] .* ) ? $ /
184+ : / ^ h t t p s ? : [ / ] [ / ] [ ^ / ] + ( [ / ] .* ) ? $ / ;
185+ const allUrlsRegex = / ^ ( h t t p s ? | f i l e | f t p ) : [ / ] + / ;
186+
187+ function getRawPatternRegex ( pattern ) {
188+ if ( ! patternValidationRegex . test ( pattern ) )
189+ throw new Error (
190+ pattern +
191+ " is an invalid pattern, it must match " +
192+ String ( patternValidationRegex )
193+ ) ;
194+ let [ , protocol , host , pathname ] = pattern . split ( / ( ^ [ ^ : ] + : [ / ] [ / ] ) ( [ ^ / ] + ) ? / ) ;
195+ protocol = protocol
196+ . replace ( "*" , isFirefox ? "(https?|wss?)" : "https?" )
197+ . replace ( / [ / ] / g, "[/]" ) ;
198+ host = ( host ?? "" )
199+ . replace ( / ^ [ * ] [ . ] / , "([^/]+.)*" )
200+ . replace ( / ^ [ * ] $ / , "[^/]+" )
201+ . replace ( / [ . ] / g, "[.]" )
202+ . replace ( / [ * ] $ / g, "[^.]+" ) ;
203+ pathname = pathname
204+ . replace ( / [ / ] / g, "[/]" )
205+ . replace ( / [ . ] / g, "[.]" )
206+ . replace ( / [ * ] / g, ".*" ) ;
207+ return "^" + protocol + host + "(" + pathname + ")?$" ;
208+ }
185209
186- // let curIndex = 0,
187- // visiblePartsInPattern = pattern.split("*").filter((_) => _ ! == "") ;
188- // for (let p of visiblePartsInPattern) {
189- // let index = url.indexOf(p, curIndex) ;
190- // if (index < 0) return false;
191- // curIndex = index + p.length;
192- // }
193- // return true;
210+ function patternToRegex ( matchPatterns ) {
211+ if ( matchPatterns . length = == 0 ) return / $ . / ;
212+ if ( matchPatterns . includes ( "<all_urls>" ) ) return allUrlsRegex ;
213+ if ( matchPatterns . includes ( "*://*/*" ) ) return allStarsRegex ;
214+ return new RegExp (
215+ matchPatterns . map ( ( x ) => getRawPatternRegex ( x ) ) . join ( "|" )
216+ ) ;
217+ }
194218
195- return patternToRegex ( pattern ) . test ( url ) ;
219+ return patternToRegex ( patterns ) . test ( url ) ;
196220}
197221
198222// https://stackoverflow.com/a/68634884/11898496
0 commit comments