-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.cs
246 lines (214 loc) · 8.61 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;
using Flow.Launcher.Plugin;
using System.Net.Http;
using System.Threading;
namespace Flow.Launcher.Plugin.LinkOpener
{
public class LinkOpener : IAsyncPlugin, ISettingProvider
{
private ObservableCollection<SettingItem> settingsItems;
internal PluginInitContext Context;
private const int BASE_SCORE = 1000;
private const int BULK_SCORE = 10000;
private const int ARG_MATCH_BONUS = 500;
private string SettingsFolder => Path.Combine(
Path.GetDirectoryName(Path.GetDirectoryName(Context.CurrentPluginMetadata.PluginDirectory)),
"Settings",
"Plugins",
"Flow.Launcher.Plugin.LinkOpener"
);
private string SettingsPath => Path.Combine(SettingsFolder, "Settings.json");
public async Task InitAsync(PluginInitContext context)
{
Context = context;
await LoadSettings();
}
private async Task LoadSettings()
{
if (!Directory.Exists(SettingsFolder))
{
Directory.CreateDirectory(SettingsFolder);
}
if (File.Exists(SettingsPath))
{
string jsonData = await File.ReadAllTextAsync(SettingsPath);
settingsItems = JsonSerializer.Deserialize<ObservableCollection<SettingItem>>(jsonData) ?? new ObservableCollection<SettingItem>();
}
else
{
settingsItems = new ObservableCollection<SettingItem>();
}
}
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
{
string fullSearch = query.Search.Trim().ToLower();
List<string> args = GetAndRemoveArgs(ref fullSearch);
var filteredItems = settingsItems.Where(item => MatchesSearch(item, fullSearch));
var filteredItemsToBulkOpen = filteredItems.Where(x => x.AddToBulkOpenUrls).ToList();
var results = await Task.WhenAll(filteredItems.Select(x => CreateResult(x, args)));
var filteredResults = results.Where(result => result != null).ToList();
if (args.Any())
{
foreach (var result in filteredResults)
{
result.Score = CalculateScore((string)result.ContextData, args);
}
}
if (filteredItemsToBulkOpen.Count > 1)
{
var bulkResult = CreateBulkOpenResult(fullSearch, filteredItemsToBulkOpen, args);
if (args.Any())
{
int totalPlaceholders = filteredItemsToBulkOpen.Sum(item => UrlUpdater.CountPlaceholdersUsed(item.Url));
bulkResult.Score = BULK_SCORE + (ARG_MATCH_BONUS * totalPlaceholders);
}
filteredResults.Add(bulkResult);
}
return filteredResults;
}
private int CalculateScore(string url, List<string> args)
{
int score = BASE_SCORE;
if (!args.Any())
return score;
int argsUsed = UrlUpdater.CountPlaceholdersUsed(url);
if (argsUsed > 0)
{
score += ARG_MATCH_BONUS * argsUsed;
}
return score;
}
private static bool MatchesSearch(SettingItem item, string fullSearch)
{
string searchKeyword = item.Keyword.Trim().ToLower();
if (!fullSearch.StartsWith(searchKeyword, StringComparison.OrdinalIgnoreCase))
return false;
string remainingSearch = fullSearch.Substring(searchKeyword.Length).Trim();
List<string> args = remainingSearch.Split(' ').ToList();
return string.IsNullOrEmpty(remainingSearch) || args.TrueForAll(arg => item.Title.ToLower().Contains(arg));
}
private static List<string> GetAndRemoveArgs(ref string query)
{
const string pattern = @"-\s*([^-]+)";
List<string> args = new List<string>();
TimeSpan timeout = TimeSpan.FromSeconds(3);
MatchCollection matches = Regex.Matches(query.Trim(), pattern, RegexOptions.None, timeout);
foreach (Match match in matches)
{
string arg = match.Groups[1].Value.Trim();
if (!string.IsNullOrWhiteSpace(arg))
{
args.Add(arg);
}
}
query = Regex.Replace(query, pattern, "", RegexOptions.None, timeout).Trim();
return args;
}
public async Task<Result> CreateResult(SettingItem settingItem, List<string> args)
{
Uri updatedUri = UrlUpdater.UpdateUrl(settingItem.Url, args);
if (updatedUri == null)
return null;
Uri faviconUri = new Uri($"https://www.google.com/s2/favicons?domain_url={updatedUri.Host}&sz=48");
string iconPath;
if (string.IsNullOrEmpty(settingItem.IconPath))
iconPath = await IsFaviconAccessible(faviconUri) ? faviconUri.ToString() : "Images\\app.png";
else
iconPath = settingItem.IconPath;
return new Result
{
Title = settingItem.Title,
SubTitle = updatedUri.ToString(),
Score = BASE_SCORE,
Action = e =>
{
Context.API.OpenUrl(updatedUri);
return true;
},
IcoPath = iconPath,
ContextData = settingItem.Url
};
}
private static async Task<bool> IsFaviconAccessible(Uri faviconUri)
{
try
{
using (HttpClient client = new HttpClient())
{
var response = await client.GetAsync(faviconUri);
return response.IsSuccessStatusCode;
}
}
catch
{
return false;
}
}
private Result CreateBulkOpenResult(string cleanSearchTerm, IEnumerable<SettingItem> itemsToOpen, List<string> args)
{
var items = itemsToOpen.ToList();
var argsDisplay = args.Any() ? $" with args: {string.Join(", ", args)}" : "";
return new Result
{
Title = $"Bulk Open ({items.Count} items){argsDisplay}",
SubTitle = $"Open all matching links for '{cleanSearchTerm}'",
Score = 10000,
Action = e =>
{
foreach (var item in items)
{
Uri updatedUri = UrlUpdater.UpdateUrl(item.Url, args);
if (updatedUri != null)
{
Context.API.OpenUrl(updatedUri);
}
}
return true;
},
IcoPath = "Images\\app.png"
};
}
public Control CreateSettingPanel()
{
return new LinkSettings(settingsItems, Context);
}
}
public static partial class UrlUpdater
{
[GeneratedRegex(@"\{(\d+)\}", RegexOptions.None, matchTimeoutMilliseconds: 3000)]
private static partial Regex PlaceholderRegex();
[GeneratedRegex(@"\s+", RegexOptions.None, matchTimeoutMilliseconds: 3000)]
private static partial Regex WhitespaceRegex();
[GeneratedRegex(@"\{\d+\}", RegexOptions.None, matchTimeoutMilliseconds: 3000)]
private static partial Regex PlaceholderCountRegex();
public static int CountPlaceholdersUsed(string url)
{
return PlaceholderCountRegex().Matches(url).Count;
}
public static Uri UpdateUrl(string url, List<string> args)
{
string updatedUrl = PlaceholderRegex().Replace(url, match =>
{
if (int.TryParse(match.Groups[1].Value, out int index) && index >= 0 && index < args.Count)
{
return Uri.EscapeDataString(args[index]);
}
return string.Empty;
});
updatedUrl = WhitespaceRegex().Replace(updatedUrl.Trim(), " ");
if (Uri.TryCreate(updatedUrl, UriKind.Absolute, out Uri uri))
{
return uri;
}
return null;
}
}
}