-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from wakatime/main
Release v5.12.0
- Loading branch information
Showing
11 changed files
with
539 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Foundation | ||
|
||
extension String { | ||
func matchesRegex(_ pattern: String) -> Bool { | ||
if let regex = try? NSRegularExpression(pattern: pattern) { | ||
let range = NSRange(location: 0, length: self.utf16.count) | ||
return regex.firstMatch(in: self, options: [], range: range) != nil | ||
} | ||
return false | ||
} | ||
|
||
func trim() -> String { | ||
self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import Cocoa | ||
|
||
class FilterManager { | ||
static func filterBrowsedSites(app: NSRunningApplication, monitoredApp: MonitoredApp, activeWindow: AXUIElement) -> Bool { | ||
guard MonitoringManager.isAppBrowser(app) else { return true } | ||
|
||
if let address = activeWindow.address(for: monitoredApp) { | ||
let patterns = Self.parseList(PropertiesManager.currentFilterList) | ||
if patterns.isEmpty { return true } | ||
|
||
// Create scheme-prefixed address versions to allow regular expressions | ||
// that incorporate a scheme to match | ||
let httpAddress = "http://" + address | ||
let httpsAddress = "https://" + address | ||
|
||
switch PropertiesManager.filterType { | ||
case .denylist: | ||
for pattern in patterns { | ||
if address.matchesRegex(pattern) || httpAddress.matchesRegex(pattern) || httpsAddress.matchesRegex(pattern) { | ||
// Address matches a pattern on the denylist. Filter the site out. | ||
return false | ||
} | ||
} | ||
case .allowlist: | ||
let addressMatchesAllowlist = patterns.contains { pattern in | ||
address.matchesRegex(pattern) || httpAddress.matchesRegex(pattern) || httpsAddress.matchesRegex(pattern) | ||
} | ||
// If none of the patterns on the allowlist match the given address, filter the site out | ||
if !addressMatchesAllowlist { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
// The given address passed all filters and will be included | ||
return true | ||
} | ||
|
||
private static func parseList(_ listString: String) -> [String] { | ||
Self.sanitizeList(listString.components(separatedBy: "\n")) | ||
} | ||
|
||
private static func sanitizeList(_ urls: [String]) -> [String] { | ||
urls.map { $0.trimmingCharacters(in: CharacterSet.whitespaces) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.