Skip to content

Commit

Permalink
Improve favicon resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Exarilo committed Oct 8, 2024
1 parent 823055b commit d312f21
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
32 changes: 17 additions & 15 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,16 @@ private async Task<Result> CreateResult(SettingItem settingItem, List<string> ar
if (!Uri.TryCreate(updatedUrl, UriKind.Absolute, out Uri uri))
return null;

string iconPath = string.IsNullOrEmpty(settingItem.IconPath)
? await GetFaviconUrl(uri) ?? "Images\\app.png"
: settingItem.IconPath;
string iconPath = settingItem.IconPath;

if (string.IsNullOrEmpty(iconPath))
{
string faviconUrl = $"https://www.google.com/s2/favicons?domain_url={uri.Host}&sz=48";
if (await IsFaviconAccessible(faviconUrl))
iconPath = faviconUrl;
else
iconPath = "Images\\app.png";
}

return new Result
{
Expand All @@ -122,25 +129,20 @@ private async Task<Result> CreateResult(SettingItem settingItem, List<string> ar
};
}

private async Task<string> GetFaviconUrl(Uri uri)
private async Task<bool> IsFaviconAccessible(string faviconUrl)
{
try
{
string faviconUrl = $"{uri.Scheme}://{uri.Host}/favicon.ico";

using (HttpClient client = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Head, faviconUrl);
var response = await client.SendAsync(request);

if (response.IsSuccessStatusCode)
{
return faviconUrl;
}
var response = await client.GetAsync(faviconUrl);
return response.IsSuccessStatusCode;
}
}
catch {}
return null;
catch
{
return false;
}
}

private Result CreateBulkOpenResult(string searchTerm, IEnumerable<SettingItem> itemsToOpen, List<string> args)
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Name": "LinkOpener",
"Description": "Allows you to open URLs using keywords set in the settings",
"Author": "Exarilo",
"Version": "1.0.9",
"Version": "1.1.0",
"Language": "csharp",
"Website": "https://github.com/Exarilo/Flow.Launcher.Plugin.LinkOpener",
"IcoPath": "Images\\app.png",
Expand Down

0 comments on commit d312f21

Please sign in to comment.