From 6a8954245276273dd88caeb4fcddfff5061ba3d4 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Tue, 26 Mar 2024 11:31:01 -0400 Subject: [PATCH] Fix parsing URL without scheme --- WakaTime/Extensions/AXUIElementExtension.swift | 4 ++-- WakaTime/Extensions/URLExtension.swift | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 WakaTime/Extensions/URLExtension.swift diff --git a/WakaTime/Extensions/AXUIElementExtension.swift b/WakaTime/Extensions/AXUIElementExtension.swift index 132c346..a3267c4 100644 --- a/WakaTime/Extensions/AXUIElementExtension.swift +++ b/WakaTime/Extensions/AXUIElementExtension.swift @@ -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)" } diff --git a/WakaTime/Extensions/URLExtension.swift b/WakaTime/Extensions/URLExtension.swift new file mode 100644 index 0000000..d1f0d1d --- /dev/null +++ b/WakaTime/Extensions/URLExtension.swift @@ -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)") + } + } +}