Flow.Launcher/Flow.Launcher.Plugin/SharedCommands/SearchWeb.cs

130 lines
4.3 KiB
C#
Raw Permalink Normal View History

2025-06-04 14:21:08 +00:00
using System;
using System.ComponentModel;
2019-08-06 11:27:54 +00:00
using System.Diagnostics;
using System.IO;
using System.Linq;
2025-06-04 14:21:08 +00:00
using Microsoft.Win32;
2019-08-06 11:27:54 +00:00
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Plugin.SharedCommands
2019-08-06 11:27:54 +00:00
{
2025-02-24 07:37:13 +00:00
/// <summary>
/// Contains methods to open a search in a new browser window or tab.
/// </summary>
2019-08-06 11:58:46 +00:00
public static class SearchWeb
2019-08-06 11:27:54 +00:00
{
2020-11-20 14:26:29 +00:00
private static string GetDefaultBrowserPath()
{
2025-06-04 14:21:08 +00:00
var name = string.Empty;
2020-11-20 14:26:29 +00:00
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"))
2025-06-04 14:21:08 +00:00
name = name[..(name.LastIndexOf(".exe") + 4)];
2020-11-20 14:26:29 +00:00
}
catch
{
return string.Empty;
}
return name;
}
/// <summary>
/// Opens search in a new browser. If no browser path is passed in then Chrome is used.
2019-08-06 11:27:54 +00:00
/// Leave browser path blank to use Chrome.
/// </summary>
public static void OpenInBrowserWindow(this string url, string browserPath = "", bool inPrivate = false, string privateArg = "")
2019-08-06 11:27:54 +00:00
{
2020-11-20 14:26:29 +00:00
browserPath = string.IsNullOrEmpty(browserPath) ? GetDefaultBrowserPath() : browserPath;
2019-08-06 11:27:54 +00:00
var browserExecutableName = browserPath?
2021-12-05 21:06:10 +00:00
.Split(new[]
{
Path.DirectorySeparatorChar
}, StringSplitOptions.None)
.Last();
2019-08-06 11:27:54 +00:00
var browser = string.IsNullOrEmpty(browserExecutableName) ? "chrome" : browserPath;
2019-08-06 11:27:54 +00:00
// Internet Explorer will open url in new browser window, and does not take the --new-window parameter
var browserArguements = (browserExecutableName == "iexplore.exe" ? "" : "--new-window ") + (inPrivate ? $"{privateArg} " : "") + url;
2019-08-06 11:27:54 +00:00
var psi = new ProcessStartInfo
{
FileName = browser,
Arguments = browserArguements,
UseShellExecute = true
};
try
{
2022-01-09 18:30:57 +00:00
Process.Start(psi)?.Dispose();
}
2025-06-04 14:21:08 +00:00
// This error may be thrown if browser path is incorrect
catch (Win32Exception)
{
try
2021-12-05 21:06:10 +00:00
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
catch
{
throw; // Re-throw the exception if we cannot open the URL in the default browser
}
}
}
/// <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 = "")
{
2020-11-20 14:26:29 +00:00
browserPath = string.IsNullOrEmpty(browserPath) ? GetDefaultBrowserPath() : browserPath;
2021-12-05 21:06:10 +00:00
var psi = new ProcessStartInfo()
{
UseShellExecute = true
};
2019-08-20 22:30:18 +00:00
try
{
if (!string.IsNullOrEmpty(browserPath))
{
psi.FileName = browserPath;
psi.Arguments = (inPrivate ? $"{privateArg} " : "") + url;
}
else
{
psi.FileName = url;
}
2022-01-09 18:30:57 +00:00
Process.Start(psi)?.Dispose();
2019-08-20 22:30:18 +00:00
}
// This error may be thrown if browser path is incorrect
2025-06-04 14:21:08 +00:00
catch (Win32Exception)
2019-08-20 22:30:18 +00:00
{
try
2021-12-05 21:06:10 +00:00
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
catch
{
throw; // Re-throw the exception if we cannot open the URL in the default browser
}
2019-08-20 22:30:18 +00:00
}
2019-08-06 11:27:54 +00:00
}
}
2025-02-24 07:37:13 +00:00
}