Skip to content

Commit

Permalink
refactor: favicon retrieval logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Daydreamer-riri committed Apr 13, 2024
1 parent aeed63a commit 2a6b61d
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Community.PowerToys.Run.Plugin.WebSearchShortcut/Models/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,23 @@ private async Task<byte[]> DownloadFaviconAsync()
{
try
{
string faviconUrl = Domain + "/favicon.ico";
using HttpClient client = new();
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; AcmeInc/1.0)");

string faviconUrl = Domain + "/favicon.ico";
HttpResponseMessage response = await client.GetAsync(faviconUrl);
if (response.IsSuccessStatusCode)
if (
response.IsSuccessStatusCode
&& response.Content.Headers.ContentType?.MediaType != "text/html"
)
{
return await response.Content.ReadAsByteArrayAsync();
}
HttpResponseMessage googleResponse = await client.GetAsync($"https://www.google.com/s2/favicons?sz=64&domain={Domain}");
if (googleResponse.IsSuccessStatusCode)
{
return await googleResponse.Content.ReadAsByteArrayAsync();
}
HttpResponseMessage domainResponse = await client.GetAsync(Domain);
var html = await domainResponse.Content.ReadAsStringAsync();
Regex regex = IconRegex();
Expand Down

0 comments on commit 2a6b61d

Please sign in to comment.