Skip to content

Commit 468563f

Browse files
committed
update match() function to support wildcards
1 parent b49b581 commit 468563f

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

generate-userscript.sh

100644100755
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ END
2828
)
2929
SCRIPT+=$'\n'
3030

31-
3231
# Loop to add @include entries
3332
while IFS= read -r europress_single_url; do
3433
SCRIPT+="// @include $europress_single_url"$'\n'
@@ -70,8 +69,18 @@ SCRIPT+=$'//
7069
const hostname = window.location.hostname;
7170
7271
function match(str, rule) {
73-
var escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
74-
return new RegExp("^" + rule.split("*").map(escapeRegex).join(".*") + "$").test(str);
72+
// Extract domain pattern from the rule (ignore https:// and /*)
73+
var domainRule = rule.replace(/^https?:\/\//, '\'''\'').replace(/\/\*$/, '\'''\'');
74+
75+
// Convert the wildcard pattern to a proper regex
76+
var regexPattern = "^" + domainRule.split('\''*'\'').map(function(part) {
77+
// Escape special regex characters in each part
78+
return part.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
79+
}).join('\''.*'\'') + "$";
80+
81+
// Create and test the regex against the input string
82+
var regex = new RegExp(regexPattern);
83+
return regex.test(str);
7584
}
7685
7786
function pasteStyle(str) {
@@ -171,10 +180,10 @@ while IFS= read -r europress_single_url; do
171180
if [[ "$counter" -eq "$europress_urls_count" ]]; then
172181
# last element, close the parenthesis.
173182
SCRIPT+=$'
174-
"'"$europress_single_url"'".includes(hostname)) {'
183+
match(hostname, "'"$europress_single_url"'")) {'
175184
else
176185
SCRIPT+=$'
177-
"'"$europress_single_url"'".includes(hostname) ||'
186+
match(hostname, "'"$europress_single_url"'") ||'
178187
fi
179188
((counter++))
180189
done <<< "$europress_urls"
@@ -370,7 +379,7 @@ while IFS= read -r line; do
370379
fi
371380

372381
SCRIPT+=$'
373-
if ("'"$matches"'".includes(hostname)) {
382+
if (match(hostname, "'"$matches"'")) {
374383
375384
window.addEventListener("load", function(event) {
376385
'"$js_str"'

0 commit comments

Comments
 (0)