Flow.Launcher/Plugins/Flow.Launcher.Plugin.Url/Main.cs

145 lines
5.2 KiB
C#
Raw Permalink Normal View History

2025-10-14 11:35:06 +00:00
using System;
2014-05-24 08:29:56 +00:00
using System.Collections.Generic;
2014-07-19 02:12:11 +00:00
using System.Text.RegularExpressions;
2025-10-09 08:36:42 +00:00
using System.Windows.Controls;
2025-09-04 09:30:20 +00:00
using Flow.Launcher.Plugin.SharedCommands;
2014-05-24 08:29:56 +00:00
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Plugin.Url
2014-05-24 08:29:56 +00:00
{
2025-10-09 08:36:42 +00:00
public class Main : IPlugin, IPluginI18n, ISettingProvider
2014-05-24 08:29:56 +00:00
{
2014-12-15 16:07:12 +00:00
//based on https://gist.github.com/dperini/729294
2025-09-04 09:26:19 +00:00
private const string UrlPattern = "^" +
2014-12-15 16:07:12 +00:00
// protocol identifier
"(?:(?:https?|ftp)://|)" +
// user:pass authentication
"(?:\\S+(?::\\S*)?@)?" +
"(?:" +
// IP address exclusion
// private & local networks
"(?!(?:10|127)(?:\\.\\d{1,3}){3})" +
"(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" +
"(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
// IP address dotted notation octets
// excludes loopback network 0.0.0.0
// excludes reserved space >= 224.0.0.0
// excludes network & broacast addresses
// (first & last IP address of each class)
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
"|" +
// host name
"(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" +
// domain name
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" +
// TLD identifier
"(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
")" +
// port number
"(?::\\d{2,5})?" +
// resource path
"(?:/\\S*)?" +
"$";
2025-09-28 04:03:49 +00:00
private readonly Regex UrlRegex = new(UrlPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
internal static PluginInitContext Context { get; private set; }
internal static Settings Settings { get; private set; }
2025-09-04 09:24:59 +00:00
public List<Result> Query(Query query)
2014-05-24 08:29:56 +00:00
{
var raw = query.Search;
2014-12-15 16:07:12 +00:00
if (IsURL(raw))
2014-05-24 08:29:56 +00:00
{
2025-09-04 09:26:19 +00:00
return
[
new()
2014-05-24 08:29:56 +00:00
{
Title = raw,
SubTitle = Localize.flowlauncher_plugin_url_open_url(raw),
2014-07-19 02:12:11 +00:00
IcoPath = "Images/url.png",
2014-05-24 08:29:56 +00:00
Score = 8,
Action = _ =>
{
2025-10-09 08:48:58 +00:00
if (!raw.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && !raw.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
2014-07-19 02:12:11 +00:00
{
2025-10-09 08:36:42 +00:00
raw = GetHttpPreference() + "://" + raw;
2014-07-19 02:12:11 +00:00
}
try
{
if (Settings.UseCustomBrowser)
2025-09-04 09:30:20 +00:00
{
if (Settings.OpenInNewBrowserWindow)
{
SearchWeb.OpenInBrowserWindow(raw, Settings.BrowserPath, Settings.OpenInPrivateMode, Settings.PrivateModeArgument);
}
else
{
SearchWeb.OpenInBrowserTab(raw, Settings.BrowserPath, Settings.OpenInPrivateMode, Settings.PrivateModeArgument);
}
2025-09-04 09:30:20 +00:00
}
else
{
Context.API.OpenWebUrl(raw);
2025-09-04 09:30:20 +00:00
}
2014-07-19 02:12:11 +00:00
return true;
}
catch(Exception)
2014-07-19 02:12:11 +00:00
{
Context.API.ShowMsgError(Localize.flowlauncher_plugin_url_cannot_open_url(raw));
2014-07-19 02:12:11 +00:00
return false;
}
2014-05-24 08:29:56 +00:00
}
}
2025-09-04 09:26:19 +00:00
];
2014-05-24 08:29:56 +00:00
}
2025-09-04 09:26:19 +00:00
return [];
2014-05-24 08:29:56 +00:00
}
2025-10-12 05:31:24 +00:00
private static string GetHttpPreference()
2025-10-09 08:36:42 +00:00
{
2025-10-12 05:31:24 +00:00
return Settings.AlwaysOpenWithHttps ? "https" : "http";
2025-10-09 08:36:42 +00:00
}
2014-12-15 16:07:12 +00:00
public bool IsURL(string raw)
{
raw = raw.ToLower();
2025-09-04 09:26:19 +00:00
if (UrlRegex.Match(raw).Value == raw) return true;
2014-12-15 16:07:12 +00:00
if (raw == "localhost" || raw.StartsWith("localhost:") ||
raw == "http://localhost" || raw.StartsWith("http://localhost:") ||
raw == "https://localhost" || raw.StartsWith("https://localhost:")
)
{
return true;
}
return false;
}
public void Init(PluginInitContext context)
{
Context = context;
2025-09-28 04:04:32 +00:00
2025-09-04 09:24:59 +00:00
Settings = context.API.LoadSettingJsonStorage<Settings>();
2015-02-07 12:17:49 +00:00
}
public string GetTranslatedPluginTitle()
{
return Localize.flowlauncher_plugin_url_plugin_name();
2015-02-07 12:17:49 +00:00
}
public string GetTranslatedPluginDescription()
{
return Localize.flowlauncher_plugin_url_plugin_description();
2014-05-24 08:29:56 +00:00
}
2025-10-09 08:36:42 +00:00
public Control CreateSettingPanel()
{
2025-10-12 05:31:24 +00:00
return new SettingsControl();
2025-10-09 08:36:42 +00:00
}
2014-05-24 08:29:56 +00:00
}
}