Skip to content

Commit

Permalink
added url patten matching for issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
zumoshi committed Jun 8, 2016
1 parent 3ddf55c commit 7d8d8d6
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 25 deletions.
4 changes: 4 additions & 0 deletions BrowserSelect/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public override string ToString()
{
return name;
}
public static implicit operator Browser(System.String s)
{
return BrowserFinder.find().Where(b => b.name == s).First();
}
}
static class BrowserFinder {
public static List<Browser> find() {
Expand Down
2 changes: 1 addition & 1 deletion BrowserSelect/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void browser_click(object sender, EventArgs e) {
open_url(uc.browser);
}

private void open_url(Browser b) {
public static void open_url(Browser b) {
if (b.exec == "edge"){
//edge is a universal app , which means we can't just run it like other browsers
Process.Start("microsoft-edge:"+Program.url
Expand Down
62 changes: 60 additions & 2 deletions BrowserSelect/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using BrowserSelect.Properties;

Expand All @@ -14,11 +15,27 @@ static class Program {
/// </summary>
[STAThread]
static void Main(string[] args) {
if (Settings.Default.HideBrowsers == null)
Settings.Default.HideBrowsers = new StringCollection();
if (Settings.Default.AutoBrowser == null)
Settings.Default.AutoBrowser = new StringCollection();

if (args.Length > 0)
{
url = args[0];
foreach (var rule in Settings.Default.AutoBrowser)
{
var sr=rule.Split(new[] {"[#!][$~][?_]"}, StringSplitOptions.None);
var pattern = sr[0];
var browser = sr[1];

if(Settings.Default.HideBrowsers == null)
Settings.Default.HideBrowsers = new StringCollection();
if (IsDomainValid(new Uri(url).Host, pattern))
{
Form1.open_url((Browser)browser);
return;
}
}
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Expand All @@ -36,5 +53,46 @@ public static string ProgramFilesx86()

return Environment.GetEnvironmentVariable("ProgramFiles");
}


/// <summary>
/// Checks if a wildcard string matches a domain
/// taken from http://madskristensen.net/post/wildcard-search-for-domains-in-c
/// </summary>
public static bool IsDomainValid(string domain, string domainToCheck)
{
if (domainToCheck.Contains("*"))
{
string checkDomain = domainToCheck;
if (checkDomain.StartsWith("*."))
checkDomain = "*" + checkDomain.Substring(2, checkDomain.Length - 2);
return DoesWildcardMatch(domain, checkDomain);
}
else
{
return domainToCheck.Equals(domain, StringComparison.OrdinalIgnoreCase);
}
}
/// <summary>
/// Performs a wildcard (*) search on any string.
/// </summary>
public static bool DoesWildcardMatch(string originalString, string searchString)
{
if (!searchString.StartsWith("*"))
{
int stop = searchString.IndexOf('*');
if (!originalString.StartsWith(searchString.Substring(0, stop)))
return false;
}
if (!searchString.EndsWith("*"))
{
int start = searchString.LastIndexOf('*') + 1;
if (!originalString.EndsWith(searchString.Substring(start, searchString.Length - start)))
return false;
}
Regex regex = new Regex(searchString.Replace(@".", @"\.").Replace(@"*", @".*"));
return regex.IsMatch(originalString);
}

}
}
4 changes: 2 additions & 2 deletions BrowserSelect/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
11 changes: 11 additions & 0 deletions BrowserSelect/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions BrowserSelect/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
<Setting Name="HideBrowsers" Type="System.Collections.Specialized.StringCollection" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="AutoBrowser" Type="System.Collections.Specialized.StringCollection" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
122 changes: 104 additions & 18 deletions BrowserSelect/frm_settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7d8d8d6

Please sign in to comment.