Skip to content

Commit

Permalink
Merge pull request #245 from wakatime/bugfix/fix-domain-parsing-witho…
Browse files Browse the repository at this point in the history
…ut-scheme

Fix parsing URL without scheme
  • Loading branch information
alanhamlett authored Mar 26, 2024
2 parents 23e7263 + 6a89542 commit f72a05f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions WakaTime/Extensions/AXUIElementExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ extension AXUIElement {
}

func domainFromUrl(_ url: String) -> String? {
guard let host = URL(string: url)?.host else { return nil }
guard let host = URL(stringWithoutScheme: url)?.host else { return nil }
let domain = host.replacingOccurrences(of: "^www.", with: "", options: .regularExpression)
guard let port = URL(string: url)?.port else { return domain }
guard let port = URL(stringWithoutScheme: url)?.port else { return domain }
return "\(domain):\(port)"
}

Expand Down
11 changes: 11 additions & 0 deletions WakaTime/Extensions/URLExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

extension URL {
init?(stringWithoutScheme string: String) {
if string.starts(with: "https?://") {
self.init(string: string)
} else {
self.init(string: "https://\(string)")
}
}
}

0 comments on commit f72a05f

Please sign in to comment.