@@ -16,8 +16,8 @@ export const cl = (message: any, type?: Log, name?: string) => {
1616 [ Log . ACTIONS ] : true ,
1717 [ Log . TABS ] : false ,
1818 [ Log . RULES ] : true ,
19- [ Log . STORAGE ] : false ,
20- [ Log . EVENTS ] : false ,
19+ [ Log . STORAGE ] : true ,
20+ [ Log . EVENTS ] : true ,
2121 [ Log . ICON ] : false ,
2222 [ Log . CONTEXT_MENUS ] : false ,
2323 } ;
@@ -113,30 +113,74 @@ export const getScopeSetting = (incognito: chrome.tabs.Tab["incognito"]) => {
113113 return incognito ? "incognito_session_only" : "regular" ;
114114} ;
115115export const getUrlAsObject = ( url : string ) => {
116- const { domain, subdomain } = parse ( url ) ;
117- const { pathname, protocol, hostname } = new URL ( url ) ;
116+ // Utiliser une regex pour capturer les différentes parties de l'URL manuellement
117+ const urlRegex = / ^ ( .* ?) : \/ \/ ( [ ^ \/ ] * ) ( \/ ? .* ) $ / ;
118+ const matches = url . match ( urlRegex ) ;
118119
119- /** http://, https:// etc... */
120- const scheme = protocol . replace ( / \: $ / , "" ) ;
120+ let scheme = "" ;
121+ let hostname = "" ;
122+ let pathname = "" ;
121123
122- const schemeSuffix = scheme === "file" ? ":///" : "://" ;
124+ if ( matches ) {
125+ scheme = matches [ 1 ] ; // Récupérer le scheme (ex: http, https, *)
126+ hostname = matches [ 2 ] ; // Récupérer le hostname (domaine + sous-domaine)
127+ pathname = matches [ 3 ] ; // Récupérer le chemin (path)
128+ }
123129
130+ const schemeSuffix = scheme === "file" ? ":///" : "://" ;
124131 const pathnameUntilLastSlash = pathname . substr ( 0 , pathname . lastIndexOf ( "/" ) ) ;
125132
126- const fixedSubdomain = subdomain && subdomain . length ? `${ subdomain } .` : "" ;
133+ // Diviser le hostname en sous-domaine et domaine
134+ const domainParts = hostname . split ( "." ) ;
135+ let domain = "" ;
136+ let subdomain = "" ;
137+
138+ if ( domainParts . length > 2 ) {
139+ domain = domainParts . slice ( - 2 ) . join ( "." ) ;
140+ subdomain = domainParts . slice ( 0 , - 2 ) . join ( "." ) ;
141+ } else {
142+ domain = hostname ;
143+ subdomain = "" ;
144+ }
145+
146+ const fixedSubdomain = subdomain . length ? `${ subdomain } .` : "" ;
127147
128148 return {
129149 hostname,
130150 scheme,
131151 schemeSuffix,
132- protocol,
133152 domain,
134153 subdomain : fixedSubdomain ,
135154 pathname,
136155 path : pathname ,
137156 pathnameUntilLastSlash,
138157 } ;
139158} ;
159+ // export const getUrlAsObject = (url: string) => {
160+ // const { domain, subdomain } = parse(url);
161+ // const { pathname, protocol, hostname } = new URL(url);
162+
163+ // /** http://, https:// etc... */
164+ // const scheme = protocol.replace(/\:$/, "");
165+
166+ // const schemeSuffix = scheme === "file" ? ":///" : "://";
167+
168+ // const pathnameUntilLastSlash = pathname.substr(0, pathname.lastIndexOf("/"));
169+
170+ // const fixedSubdomain = subdomain && subdomain.length ? `${subdomain}.` : "";
171+
172+ // return {
173+ // hostname,
174+ // scheme,
175+ // schemeSuffix,
176+ // //protocol,
177+ // domain,
178+ // subdomain: fixedSubdomain,
179+ // pathname,
180+ // path: pathname,
181+ // pathnameUntilLastSlash,
182+ // };
183+ // };
140184
141185export const isValidScheme = ( scheme : string ) => {
142186 return [ "*" , "http" , "https" , "file" , "ftp" , "urn" ] . includes ( scheme )
@@ -201,3 +245,43 @@ export const retry = <T>(fn: () => Promise<T>, ms: number): Promise<T> =>
201245 } , ms ) ;
202246 } ) ;
203247 } ) ;
248+
249+ export function sortUrlsByPatternPrecedence ( urls : string [ ] ) {
250+ // Fonction pour déterminer la priorité des motifs
251+ function getPatternScore ( url : string ) {
252+ let score = 0 ;
253+
254+ // Priorité par schéma (https > http)
255+ if ( url . startsWith ( "https://" ) ) score += 3 ;
256+ else if ( url . startsWith ( "http://" ) ) score += 2 ;
257+
258+ // Priorité par la spécificité du domaine
259+ const domainParts = url . split ( "/" ) [ 2 ] . split ( "." ) ;
260+ if ( domainParts . length > 2 ) {
261+ // Plus le sous-domaine est spécifique, plus le score est élevé
262+ score += 1 * ( domainParts . length - 2 ) ;
263+ }
264+
265+ // Priorité par longueur du chemin (plus c'est long, plus c'est spécifique)
266+ const pathLength = url . split ( "/" ) . length - 3 ;
267+ score += pathLength ;
268+
269+ return score ;
270+ }
271+
272+ // Trier les URLs selon la priorité (score décroissant)
273+ return urls . sort ( ( a , b ) => getPatternScore ( b ) - getPatternScore ( a ) ) ;
274+ }
275+
276+
277+ export function getDomainAndSubdomain ( url : string ) {
278+ try {
279+ const parsedUrl = new URL ( url ) ;
280+ const hostname = parsedUrl . hostname ;
281+
282+ return hostname ;
283+ } catch ( error ) {
284+ console . error ( 'Invalid url:' , error ) ;
285+ return null ;
286+ }
287+ }
0 commit comments