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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-10 08:12:43 +00:00
|
|
|
|
/// <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>
|
2021-12-08 10:49:14 +00:00
|
|
|
|
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
|
|
|
|
|
2019-11-10 09:12:45 +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
|
2021-12-09 03:06:52 +00:00
|
|
|
|
var browserArguements = (browserExecutableName == "iexplore.exe" ? "" : "--new-window ") + (inPrivate ? $"{privateArg} " : "") + url;
|
2019-08-06 11:27:54 +00:00
|
|
|
|
|
2020-04-17 10:47:56 +00:00
|
|
|
|
var psi = new ProcessStartInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
FileName = browser,
|
|
|
|
|
|
Arguments = browserArguements,
|
|
|
|
|
|
UseShellExecute = true
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2019-11-10 09:12:45 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-01-09 18:30:57 +00:00
|
|
|
|
Process.Start(psi)?.Dispose();
|
2019-11-10 09:12:45 +00:00
|
|
|
|
}
|
2025-06-04 14:21:08 +00:00
|
|
|
|
// This error may be thrown if browser path is incorrect
|
|
|
|
|
|
catch (Win32Exception)
|
2019-11-10 09:12:45 +00:00
|
|
|
|
{
|
2025-06-04 14:22:02 +00:00
|
|
|
|
try
|
2021-12-05 21:06:10 +00:00
|
|
|
|
{
|
2025-06-04 14:22:02 +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-11-10 09:12:45 +00:00
|
|
|
|
}
|
2019-11-10 08:12:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Opens search as a tab in the default browser chosen in Windows settings.
|
|
|
|
|
|
/// </summary>
|
2021-12-08 10:49:14 +00:00
|
|
|
|
public static void OpenInBrowserTab(this string url, string browserPath = "", bool inPrivate = false, string privateArg = "")
|
2019-11-10 08:12:43 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
|
{
|
2019-11-10 09:15:14 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(browserPath))
|
|
|
|
|
|
{
|
2020-04-17 10:47:56 +00:00
|
|
|
|
psi.FileName = browserPath;
|
2021-12-09 03:06:52 +00:00
|
|
|
|
psi.Arguments = (inPrivate ? $"{privateArg} " : "") + url;
|
2019-11-10 09:15:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-04-17 10:47:56 +00:00
|
|
|
|
psi.FileName = url;
|
2019-11-10 09:15:14 +00:00
|
|
|
|
}
|
2020-04-17 10:47:56 +00:00
|
|
|
|
|
2022-01-09 18:30:57 +00:00
|
|
|
|
Process.Start(psi)?.Dispose();
|
2019-08-20 22:30:18 +00:00
|
|
|
|
}
|
2020-04-17 10:47:56 +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
|
|
|
|
{
|
2025-06-04 14:22:02 +00:00
|
|
|
|
try
|
2021-12-05 21:06:10 +00:00
|
|
|
|
{
|
2025-06-04 14:22:02 +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
|
|
|
|
}
|