Skip to content

Commit 88b729c

Browse files
author
hoang.tran12
committed
fix run script check patterns
1 parent a5b5d60 commit 88b729c

File tree

3 files changed

+93
-160
lines changed

3 files changed

+93
-160
lines changed

scripts/content-scripts/run_scripts.js

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,61 @@
3636
}
3737
})();
3838

39-
function matchPattern(url, pattern) {
40-
if (pattern.indexOf("*") < 0)
41-
return new URL(url).toString() == new URL(pattern).toString();
42-
43-
let curIndex = 0,
44-
visiblePartsInPattern = pattern.split("*").filter((_) => _ !== "");
45-
for (let p of visiblePartsInPattern) {
46-
let index = url.indexOf(p, curIndex);
47-
if (index < 0) return false;
48-
curIndex = index + p.length;
49-
}
50-
return true;
51-
}
52-
function matchPatterns(url, patterns) {
53-
for (let p of patterns) {
54-
if (!matchPattern(url, p)) {
55-
return false;
56-
}
57-
}
58-
return true;
59-
}
6039
function checkWillRun(script) {
6140
let url = location.href;
6241
let hasWhiteList = script.whiteList?.length > 0;
6342
let hasBlackList = script.blackList?.length > 0;
64-
let inWhiteList = matchPatterns(url, script.whiteList ?? []);
65-
let inBlackList = matchPatterns(url, script.blackList ?? []);
43+
let inWhiteList = matchPatterns(url, script.whiteList || []);
44+
let inBlackList = matchPatterns(url, script.blackList || []);
6645
return (
6746
(!hasWhiteList && !hasBlackList) ||
6847
(hasWhiteList && inWhiteList) ||
6948
(hasBlackList && !inBlackList)
7049
);
7150
}
51+
52+
// Source: https://github.com/fregante/webext-patterns/blob/main/index.ts
53+
function matchPatterns(url, patterns) {
54+
const patternValidationRegex =
55+
/^(https?|wss?|file|ftp|\*):\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^file:\/\/\/.*$|^resource:\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^about:/;
56+
const isFirefox =
57+
typeof navigator === "object" && navigator.userAgent.includes("Firefox/");
58+
const allStarsRegex = isFirefox
59+
? /^(https?|wss?):[/][/][^/]+([/].*)?$/
60+
: /^https?:[/][/][^/]+([/].*)?$/;
61+
const allUrlsRegex = /^(https?|file|ftp):[/]+/;
62+
63+
function getRawPatternRegex(pattern) {
64+
if (!patternValidationRegex.test(pattern))
65+
throw new Error(
66+
pattern +
67+
" is an invalid pattern, it must match " +
68+
String(patternValidationRegex)
69+
);
70+
let [, protocol, host, pathname] = pattern.split(/(^[^:]+:[/][/])([^/]+)?/);
71+
protocol = protocol
72+
.replace("*", isFirefox ? "(https?|wss?)" : "https?")
73+
.replace(/[/]/g, "[/]");
74+
host = (host ?? "")
75+
.replace(/^[*][.]/, "([^/]+.)*")
76+
.replace(/^[*]$/, "[^/]+")
77+
.replace(/[.]/g, "[.]")
78+
.replace(/[*]$/g, "[^.]+");
79+
pathname = pathname
80+
.replace(/[/]/g, "[/]")
81+
.replace(/[.]/g, "[.]")
82+
.replace(/[*]/g, ".*");
83+
return "^" + protocol + host + "(" + pathname + ")?$";
84+
}
85+
86+
function patternToRegex(matchPatterns) {
87+
if (matchPatterns.length === 0) return /$./;
88+
if (matchPatterns.includes("<all_urls>")) return allUrlsRegex;
89+
if (matchPatterns.includes("*://*/*")) return allStarsRegex;
90+
return new RegExp(
91+
matchPatterns.map((x) => getRawPatternRegex(x)).join("|")
92+
);
93+
}
94+
95+
return patternToRegex(patterns).test(url);
96+
}

scripts/helpers/utils.js

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
/^(https?|wss?|file|ftp|\*):\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^file:\/\/\/.*$|^resource:\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^about:/;
180+
const isFirefox =
181+
typeof navigator === "object" && navigator.userAgent.includes("Firefox/");
182+
const allStarsRegex = isFirefox
183+
? /^(https?|wss?):[/][/][^/]+([/].*)?$/
184+
: /^https?:[/][/][^/]+([/].*)?$/;
185+
const allUrlsRegex = /^(https?|file|ftp):[/]+/;
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

scripts/helpers/webext-patterns.js

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)