@@ -2,26 +2,28 @@ import { allScripts } from "../../scripts/index.js";
22import { getCurrentURL } from "./utils.js" ;
33
44export async function getAvailableScripts ( ) {
5- let hostname = ( await getCurrentURL ( ) ) . hostname ;
5+ let url = await getCurrentURL ( ) ;
66 let avai = [ ] ;
77 for ( let script of Object . values ( allScripts ) ) {
8- if ( await checkBlackWhiteList ( script , hostname ) ) {
8+ if ( await checkBlackWhiteList ( script , url ) ) {
99 avai . push ( script ) ;
1010 }
1111 }
1212
1313 return avai ;
1414}
1515
16- export async function checkBlackWhiteList ( script , hostname = null ) {
17- if ( ! hostname ) {
18- hostname = ( await getCurrentURL ( ) ) . hostname ;
16+ export async function checkBlackWhiteList ( script , url = null ) {
17+ if ( ! url ) {
18+ url = await getCurrentURL ( ) ;
1919 }
2020
21- let hasWhiteList = script . whiteList ?. length > 0 ;
22- let hasBlackList = script . blackList ?. length > 0 ;
23- let inWhiteList = script . whiteList ?. findIndex ( ( _ ) => _ === hostname ) >= 0 ;
24- let inBlackList = script . blackList ?. findIndex ( ( _ ) => _ === hostname ) >= 0 ;
21+ let w = script . whiteList ,
22+ b = script . blackList ,
23+ hasWhiteList = w ?. length > 0 ,
24+ hasBlackList = b ?. length > 0 ,
25+ inWhiteList = w ?. findIndex ( ( _ ) => isUrlMatchPattern ( url , _ ) ) >= 0 ,
26+ inBlackList = b ?. findIndex ( ( _ ) => isUrlMatchPattern ( url , _ ) ) >= 0 ;
2527
2628 let willRun =
2729 ( ! hasWhiteList && ! hasBlackList ) ||
@@ -30,3 +32,16 @@ export async function checkBlackWhiteList(script, hostname = null) {
3032
3133 return willRun ;
3234}
35+
36+ export function isUrlMatchPattern ( url , pattern ) {
37+ let curIndex = 0 ,
38+ visiblePartsInPattern = pattern . split ( "*" ) . filter ( ( _ ) => _ !== "" ) ;
39+
40+ for ( let p of visiblePartsInPattern ) {
41+ let index = url . indexOf ( p , curIndex ) ;
42+ if ( index < 0 ) return false ;
43+ curIndex = index + p . length ;
44+ }
45+
46+ return true ;
47+ }
0 commit comments