PowerToys/Plugins/Wox.Plugin.Url/Main.cs

136 lines
4.5 KiB
C#
Raw Normal View History

2019-08-01 18:52:50 +08:00
using System;
2014-05-24 16:29:56 +08:00
using System.Collections.Generic;
2014-07-19 10:12:11 +08:00
using System.Text.RegularExpressions;
using System.Windows.Controls;
using Wox.Infrastructure.Storage;
2019-08-06 19:58:46 +08:00
using Wox.Plugin.SharedCommands;
2014-05-24 16:29:56 +08:00
namespace Wox.Plugin.Url
2014-05-24 16:29:56 +08:00
{
public class Main : ISettingProvider,IPlugin, IPluginI18n, ISavable
2014-05-24 16:29:56 +08:00
{
2014-12-16 00:07:12 +08:00
//based on https://gist.github.com/dperini/729294
2015-02-07 20:17:49 +08:00
private const string urlPattern = "^" +
2014-12-16 00:07:12 +08: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*)?" +
"$";
Regex reg = new Regex(urlPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
2015-02-07 20:17:49 +08:00
private PluginInitContext context;
private readonly Settings _settings;
private readonly PluginJsonStorage<Settings> _storage;
public Main()
{
_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();
}
public void Save()
{
_storage.Save();
}
2014-07-19 10:12:11 +08:00
public List<Result> Query(Query query)
2014-05-24 16:29:56 +08:00
{
var raw = query.Search;
2014-12-16 00:07:12 +08:00
if (IsURL(raw))
2014-05-24 16:29:56 +08:00
{
return new List<Result>
{
new Result
{
Title = raw,
2015-02-07 20:17:49 +08:00
SubTitle = string.Format(context.API.GetTranslation("wox_plugin_url_open_url"),raw),
2014-07-19 10:12:11 +08:00
IcoPath = "Images/url.png",
2014-05-24 16:29:56 +08:00
Score = 8,
Action = _ =>
{
2014-07-19 10:12:11 +08:00
if (!raw.ToLower().StartsWith("http"))
{
raw = "http://" + raw;
}
try
{
2019-08-06 19:58:46 +08:00
raw.NewBrowserWindow(_settings.BrowserPath);
2014-07-19 10:12:11 +08:00
return true;
}
catch(Exception ex)
{
2015-02-07 20:17:49 +08:00
context.API.ShowMsg(string.Format(context.API.GetTranslation("wox_plugin_url_canot_open_url"), raw));
2014-07-19 10:12:11 +08:00
return false;
}
2014-05-24 16:29:56 +08:00
}
}
};
}
return new List<Result>(0);
}
public Control CreateSettingPanel()
{
return new SettingsControl(context.API,_settings);
}
2014-12-16 00:07:12 +08:00
public bool IsURL(string raw)
{
raw = raw.ToLower();
if (reg.Match(raw).Value == raw) return true;
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)
{
2015-02-07 20:17:49 +08:00
this.context = context;
}
public string GetTranslatedPluginTitle()
{
return context.API.GetTranslation("wox_plugin_url_plugin_name");
}
public string GetTranslatedPluginDescription()
{
return context.API.GetTranslation("wox_plugin_url_plugin_description");
2014-05-24 16:29:56 +08:00
}
}
}