-
-
Notifications
You must be signed in to change notification settings - Fork 350
/
Copy pathSearchWeb.cs
123 lines (108 loc) · 4.56 KB
/
SearchWeb.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
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace Flow.Launcher.Plugin.SharedCommands
{
public static class SearchWeb
{
private static string GetDefaultBrowserPath()
{
string name = string.Empty;
try
{
using var regDefault = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice", false);
var stringDefault = regDefault.GetValue("ProgId");
using var regKey = Registry.ClassesRoot.OpenSubKey(stringDefault + "\\shell\\open\\command", false);
name = regKey.GetValue(null).ToString().ToLower().Replace("\"", "");
if (!name.EndsWith("exe"))
name = name.Substring(0, name.LastIndexOf(".exe") + 4);
}
catch
{
return string.Empty;
}
return name;
}
/// <summary>
/// Opens search in a new browser. If no browser path is passed in then Chrome is used.
/// Leave browser path blank to use Chrome.
/// </summary>
public static void OpenInBrowserWindow(this string url, string browserPath = "", bool inPrivate = false, string privateArg = "", string additionalArgs = "")
{
browserPath = string.IsNullOrEmpty(browserPath) ? GetDefaultBrowserPath() : browserPath;
var browserExecutableName = browserPath?
.Split(new[]
{
Path.DirectorySeparatorChar
}, StringSplitOptions.None)
.Last();
var browser = string.IsNullOrEmpty(browserExecutableName) ? "chrome" : browserPath;
// Internet Explorer will open url in new browser window, and does not take the --new-window parameter
var newWindowArg = browserExecutableName == "iexplore.exe" ? "" : "--new-window ";
privateArg = inPrivate && !string.IsNullOrEmpty(privateArg) ? $"{privateArg} " : "";
var browserArguements = $"{newWindowArg}{privateArg}{additionalArgs} {url}";
var psi = new ProcessStartInfo
{
FileName = browser,
Arguments = browserArguements,
UseShellExecute = true
};
try
{
Process.Start(psi)?.Dispose();
}
catch (System.ComponentModel.Win32Exception)
{
Process.Start(new ProcessStartInfo
{
FileName = url, UseShellExecute = true
});
}
}
[Obsolete("This is provided for backwards compatibility after 1.9.0 release, e.g. GitHub plugin. Use the new method instead")]
public static void NewBrowserWindow(this string url, string browserPath = "")
{
OpenInBrowserWindow(url, browserPath);
}
/// <summary>
/// Opens search as a tab in the default browser chosen in Windows settings.
/// </summary>
public static void OpenInBrowserTab(this string url, string browserPath = "", bool inPrivate = false, string privateArg = "", string additionalArgs = "")
{
browserPath = string.IsNullOrEmpty(browserPath) ? GetDefaultBrowserPath() : browserPath;
var psi = new ProcessStartInfo()
{
UseShellExecute = true
};
try
{
if (!string.IsNullOrEmpty(browserPath))
{
psi.FileName = browserPath;
privateArg = inPrivate && !string.IsNullOrEmpty(privateArg) ? $"{privateArg} " : "";
psi.Arguments = $"{privateArg}{additionalArgs} {url}";
}
else
{
psi.FileName = url;
}
Process.Start(psi)?.Dispose();
}
// This error may be thrown if browser path is incorrect
catch (System.ComponentModel.Win32Exception)
{
Process.Start(new ProcessStartInfo
{
FileName = url, UseShellExecute = true
});
}
}
[Obsolete("This is provided for backwards compatibility after 1.9.0 release, e.g. GitHub plugin. Use the new method instead")]
public static void NewTabInBrowser(this string url, string browserPath = "")
{
OpenInBrowserTab(url, browserPath);
}
}
}