Skip to content

Commit

Permalink
Set site favicon as default when custom icon is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Exarilo committed Oct 8, 2024
1 parent 5b5d748 commit 823055b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
32 changes: 29 additions & 3 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using System.Windows.Controls;
using Flow.Launcher.Plugin;
using System.Net.Http;

namespace Flow.Launcher.Plugin.LinkOpener
{
Expand Down Expand Up @@ -56,7 +57,7 @@ public List<Result> Query(Query query)
var filteredItemsToBulkOpen = filteredItems.Where(x => x.AddToBulkOpenUrls);

var results = new List<Result>();
results.AddRange(filteredItems.Select(x => CreateResult(x, args)).Where(result => result != null));
results.AddRange(filteredItems.Select(x => CreateResult(x, args).Result).Where(result => result != null));

if (filteredItemsToBulkOpen.Count() > 1 && results.Count > 1)
{
Expand Down Expand Up @@ -96,13 +97,17 @@ private List<string> GetAndRemoveArgs(ref string query)
return args;
}

private Result CreateResult(SettingItem settingItem, List<string> args)
private async Task<Result> CreateResult(SettingItem settingItem, List<string> args)
{
string updatedUrl = UpdateUrl(settingItem.Url, args);

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;

return new Result
{
Title = settingItem.Title,
Expand All @@ -113,10 +118,31 @@ private Result CreateResult(SettingItem settingItem, List<string> args)
Context.API.OpenUrl(updatedUrl);
return true;
},
IcoPath = string.IsNullOrEmpty(settingItem.IconPath) ? "Images\\app.png" : settingItem.IconPath
IcoPath = iconPath
};
}

private async Task<string> GetFaviconUrl(Uri uri)
{
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;
}
}
}
catch {}
return null;
}

private Result CreateBulkOpenResult(string searchTerm, IEnumerable<SettingItem> itemsToOpen, List<string> args)
{
return new Result
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.8",
"Version": "1.0.9",
"Language": "csharp",
"Website": "https://github.com/Exarilo/Flow.Launcher.Plugin.LinkOpener",
"IcoPath": "Images\\app.png",
Expand Down

0 comments on commit 823055b

Please sign in to comment.