Skip to content

Commit f9d331f

Browse files
committed
rework getMatchedFiles func for include and exclude quoid#143
1 parent b7510d2 commit f9d331f

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

extension/Userscripts Extension/Functions.swift

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,8 @@ func getMatchedFiles(_ location: [String: Any]) -> [String]? {
10931093
guard
10941094
let ptcl = location["protocol"] as? String,
10951095
let host = location["host"] as? String,
1096-
let path = location["pathname"] as? String
1096+
let path = location["pathname"] as? String,
1097+
let href = location["href"] as? String
10971098
else {
10981099
err("could not get values from location object when attempting to get matched files")
10991100
return nil
@@ -1103,10 +1104,14 @@ func getMatchedFiles(_ location: [String: Any]) -> [String]? {
11031104
var excludedFilenames:[String] = []
11041105
// when code is loaded from a file, it's filename will be populated in the below array, to avoid duplication
11051106
var matchedFilenames:[String] = []
1106-
// all exclude patterns from manifest
1107-
let excludePatterns = manifestKeys.excludeMatch.keys
1107+
// all exclude-match patterns from manifest
1108+
let excludeMatchPatterns = manifestKeys.excludeMatch.keys
11081109
// all match patterns from manifest
11091110
let matchPatterns = manifestKeys.match.keys
1111+
// all include patterns from manifest
1112+
let includeExpressions = manifestKeys.include.keys
1113+
// all exclude patterns from manifest
1114+
let excludeExpressions = manifestKeys.exclude.keys
11101115

11111116
// if injection is disabled, return empty array
11121117
if active != "true" {
@@ -1125,7 +1130,7 @@ func getMatchedFiles(_ location: [String: Any]) -> [String]? {
11251130
excludedFilenames.append(contentsOf: manifestKeys.disabled)
11261131

11271132
// loop through exclude patterns and see if any match against page url
1128-
for pattern in excludePatterns {
1133+
for pattern in excludeMatchPatterns {
11291134
// if pattern matches page url, add filenames from page url to excludes array, code from those filenames won't be loaded
11301135
if match(ptcl, host, path, pattern) {
11311136
guard let filenames = manifestKeys.excludeMatch[pattern] else {
@@ -1140,6 +1145,21 @@ func getMatchedFiles(_ location: [String: Any]) -> [String]? {
11401145
}
11411146
}
11421147

1148+
// loop through exclude expressions and check for matches
1149+
for exp in excludeExpressions {
1150+
if include(href, exp) {
1151+
guard let filenames = manifestKeys.exclude[exp] else {
1152+
err("error parsing manifest when attempting to get code, excludeExpressions")
1153+
continue
1154+
}
1155+
for filename in filenames {
1156+
if !excludedFilenames.contains(filename) {
1157+
excludedFilenames.append(filename)
1158+
}
1159+
}
1160+
}
1161+
}
1162+
11431163
// loop through all match patterns from manifest to see if they match against the current page url
11441164
for pattern in matchPatterns {
11451165
if match(ptcl, host, path, pattern) {
@@ -1158,6 +1178,22 @@ func getMatchedFiles(_ location: [String: Any]) -> [String]? {
11581178

11591179
}
11601180
}
1181+
1182+
// loop through include expressions and check for matches
1183+
for exp in includeExpressions {
1184+
if include(href, exp) {
1185+
guard let filenames = manifestKeys.include[exp] else {
1186+
err("error parsing manifest when attempting to get code, includeExpressions")
1187+
continue
1188+
}
1189+
for filename in filenames {
1190+
if !excludedFilenames.contains(filename) && !matchedFilenames.contains(filename) {
1191+
matchedFilenames.append(filename)
1192+
}
1193+
}
1194+
}
1195+
}
1196+
11611197
return matchedFilenames
11621198
}
11631199

0 commit comments

Comments
 (0)