fancy new browser
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: fix favicon support

+27
+27
Sources/WebKitEngine/WebKitWebContent.swift
··· 165 165 166 166 public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { 167 167 if let url = webView.url { eventContinuation.yield(.finished(url: url)) } 168 + Task { await emitFavicon() } 169 + } 170 + 171 + private func emitFavicon() async { 172 + let js = """ 173 + (function() { 174 + var sel = 'link[rel~="icon"], link[rel~="shortcut icon"], link[rel="apple-touch-icon"]'; 175 + var links = Array.from(document.querySelectorAll(sel)); 176 + links.sort(function(a, b) { 177 + var sa = (a.sizes && a.sizes[0]) ? parseInt(a.sizes[0]) : 0; 178 + var sb = (b.sizes && b.sizes[0]) ? parseInt(b.sizes[0]) : 0; 179 + return sb - sa; 180 + }); 181 + if (links.length && links[0].href) return links[0].href; 182 + return null; 183 + })() 184 + """ 185 + let result = try? await webView.evaluateJavaScript(js) 186 + let faviconURL: URL? 187 + if let href = result as? String, let url = URL(string: href) { 188 + faviconURL = url 189 + } else if let host = webView.url.flatMap({ URL(string: "\($0.scheme ?? "https")://\($0.host ?? "")") } ) { 190 + faviconURL = host.appendingPathComponent("favicon.ico") 191 + } else { 192 + faviconURL = nil 193 + } 194 + eventContinuation.yield(.faviconChanged(url: faviconURL)) 168 195 } 169 196 170 197 public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {