Skip to content

Commit

Permalink
Merge pull request #20 from valnoxy/dev
Browse files Browse the repository at this point in the history
Version 1.3.1
  • Loading branch information
valnoxy authored Jun 3, 2024
2 parents e5f2795 + 4a7b12b commit 6254808
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 90 deletions.
148 changes: 87 additions & 61 deletions GoAwayEdge/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

using GoAwayEdge.Common;
using System.Diagnostics;
using System.IO;
using System.Security.Principal;
using System.Text;
using System.Web;
Expand All @@ -25,9 +26,13 @@ namespace GoAwayEdge
public partial class App
{
private static string? _url;
public static bool Debug = false;

public void Application_Startup(object sender, StartupEventArgs e)
{
#if DEBUG
Debug = true;
#endif
// Load Language
LocalizationManager.LoadLanguage();

Expand Down Expand Up @@ -59,6 +64,8 @@ public void Application_Startup(object sender, StartupEventArgs e)
{
if (args.Contains("-ToastActivated")) // Clicked on notification, ignore it.
Environment.Exit(0);
if (args.Contains("-debug"))
Debug = true;
if (args.Contains("-s")) // Silent Installation
{
foreach (var arg in args)
Expand Down Expand Up @@ -211,70 +218,101 @@ public void Application_Startup(object sender, StartupEventArgs e)
public static void RunParser(string[] args)
{
var argumentJoin = string.Join(" ", args);
#if DEBUG
var w = new MessageUi("GoAwayEdge",
$"The following args are redirected (CTRL+C to copy):\n\n{argumentJoin}", "OK", null, true);
w.ShowDialog();
#endif
Console.WriteLine("Command line args:\n\n" + argumentJoin + "\n", ConsoleColor.Gray);
var isFile = false;
var isCopilot = false;
var parsedData = false;
var ignoreStartup = false;
var p = new Process();
p.StartInfo.UseShellExecute = true;
p.StartInfo.RedirectStandardOutput = false;

if (Debug)
{
var w = new MessageUi("GoAwayEdge",
$"The following args are redirected (CTRL+C to copy):\n\n{argumentJoin}", "OK", null, true);
w.ShowDialog();
}

// Filter command line args
foreach (var arg in args)
{
if (arg.Contains("microsoft-edge:"))
// Check for Copilot
if (arg.Contains("microsoft-edge://?ux=copilot&tcp=1&source=taskbar")
|| arg.Contains("microsoft-edge:///?ux=copilot&tcp=1&source=taskbar"))
{
_url = arg;
isCopilot = true;
break;
}
if (!args.Contains("--profile-directory") && !ContainsParsedData(args) && args.Length != 1) continue; // Start Edge (default browser on this system)

#if DEBUG
var messageUi = new MessageUi("GoAwayEdge",
"Microsoft Edge will now start normally via IFEO application.", "OK", null, true);
messageUi.ShowDialog();
#endif
// User want to parse data
if (arg.Contains("microsoft-edge:"))
{
_url = ParseUrl(arg);
parsedData = true;
break;
}

// Check if the argument contains a file (like PDF)
if (arg.Contains("msedge.exe"))
isFile = false;
else if (File.Exists(arg))
isFile = true;

// Check for blacklisted arguments
if (arg.Contains("--no-startup-window")
|| arg.Contains("--profile-directory"))
ignoreStartup = true;
}

// Open Edge normally
if ((!parsedData || isCopilot || isFile || args.Contains("--profile-directory")) && !ignoreStartup)
{
if (Debug)
{
if (isCopilot)
{
var copilotMessageUi = new MessageUi("GoAwayEdge",
$"Opening Windows Copilot with following url:\n{_url}", "OK", null, true);
copilotMessageUi.ShowDialog();
}
else
{
var messageUi = new MessageUi("GoAwayEdge",
"Microsoft Edge will now start normally via IFEO application.", "OK", null, true);
messageUi.ShowDialog();
}
}
var parsedArgs = args.Skip(2);
var p = new Process();
p.StartInfo.FileName = FileConfiguration.NonIfeoPath;
p.StartInfo.Arguments = string.Join(" ", parsedArgs);
p.StartInfo.UseShellExecute = true;
p.StartInfo.RedirectStandardOutput = false;
p.Start();
Environment.Exit(0);
}

// Open URL in default browser
if (_url != null)
// Open default Browser with parsed data
if (parsedData)
{
var p = new Process();

// Windows Copilot
if (_url.Contains("microsoft-edge://?ux=copilot&tcp=1&source=taskbar")
|| _url.Contains("microsoft-edge:///?ux=copilot&tcp=1&source=taskbar"))
if (Debug)
{
p.StartInfo.FileName = FileConfiguration.NonIfeoPath;
p.StartInfo.Arguments = _url;
Console.WriteLine($"Opening Windows Copilot with following url:\n{_url}", ConsoleColor.Gray);
#if DEBUG
var copilotMessageUi = new MessageUi("GoAwayEdge",
$"Opening Windows Copilot with following url:\n{_url}", "OK", null, true);
copilotMessageUi.ShowDialog();
#endif
var defaultUrlMessageUi = new MessageUi("GoAwayEdge",
"Opening URL in default browser:\n\n" + _url + "\n", "OK", null, true);
defaultUrlMessageUi.ShowDialog();
}
else
p.StartInfo.FileName = _url;
p.StartInfo.Arguments = "";
p.Start();
Environment.Exit(0);
}

if (ignoreStartup)
{
if (Debug)
{
var parsed = ParseUrl(_url);
Console.WriteLine("Opening URL in default browser:\n\n" + parsed + "\n", ConsoleColor.Gray);
#if DEBUG
var defaultUrlMessageUi = new MessageUi("GoAwayEdge",
"Opening URL in default browser:\n\n" + parsed + "\n", "OK", null, true);
"Edge was called with a blacklisted argument. Edge won't be launched.", "OK", null, true);
defaultUrlMessageUi.ShowDialog();
#endif
p.StartInfo.FileName = parsed;
p.StartInfo.Arguments = "";
}
p.StartInfo.UseShellExecute = true;
p.StartInfo.RedirectStandardOutput = false;
p.Start();
Environment.Exit(0);
}
}

Expand Down Expand Up @@ -308,19 +346,6 @@ private static SearchEngine ParseSearchEngine(string argument)
};
}

private static bool ContainsParsedData(IEnumerable<string> args)
{
var contains = false;
var engineUrl = DefineEngine(Configuration.Search);

foreach (var arg in args)
{
if (arg.Contains(engineUrl))
contains = true;
}
return contains;
}

private static string ParseUrl(string encodedUrl)
{
// Remove URI handler with url argument prefix
Expand All @@ -343,11 +368,12 @@ private static string ParseUrl(string encodedUrl)
// Replace Search Engine
encodedUrl = encodedUrl.Replace("https://www.bing.com/search?q=", DefineEngine(Configuration.Search));

#if DEBUG
var uriMessageUi = new MessageUi("GoAwayEdge",
"New Uri: " + encodedUrl, "OK", null, true);
uriMessageUi.ShowDialog();
#endif
if (Debug)
{
var uriMessageUi = new MessageUi("GoAwayEdge",
"New Uri: " + encodedUrl, "OK", null, true);
uriMessageUi.ShowDialog();
}
var uri = new Uri(encodedUrl);
return uri.ToString();
}
Expand Down
66 changes: 48 additions & 18 deletions GoAwayEdge/Common/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,24 @@ public class RegistryConfig
private const string Company = "valnoxy";
private const string Product = "GoAwayEdge";
private const string RegistryPath = @$"SOFTWARE\{Company}\{Product}";
public const string UninstallGuid = "{DC021E0D-1809-4102-8888-506D3121F1E9}";
private const string UninstallRegistryPath = @$"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{UninstallGuid}";

/// <summary>
/// Create a Key in the Registry
/// </summary>
/// <param name="option">Key Name</param>
/// <param name="value">Key Value</param>
public static void SetKey(string option, object value)
/// <param name="valueKind">Type of value</param>
/// <param name="isUninstall">Use the Uninstall Registry key instead.</param>
public static void SetKey(string option, object value, RegistryValueKind valueKind = RegistryValueKind.String, bool isUninstall = false)
{
try
{
using var key = Registry.LocalMachine.CreateSubKey(RegistryPath, RegistryKeyPermissionCheck.ReadWriteSubTree);
key.SetValue(option, value);
using var key = isUninstall
? Registry.LocalMachine.CreateSubKey(UninstallRegistryPath, RegistryKeyPermissionCheck.ReadWriteSubTree)
: Registry.LocalMachine.CreateSubKey(RegistryPath, RegistryKeyPermissionCheck.ReadWriteSubTree);
key.SetValue(option, value, valueKind);
}
catch (Exception ex)
{
Expand All @@ -112,30 +118,27 @@ public static void SetKey(string option, object value)
/// Retrieves the value of a key from the Registry.
/// </summary>
/// <param name="option">Name of the key.</param>
/// <param name="isUninstall">Use the Uninstall Registry key instead.</param>
/// <returns>The value of the key if it exists, otherwise null.</returns>
public static string GetKey(string option)
public static string GetKey(string option, bool isUninstall = false)
{
try
{
using var key = Registry.LocalMachine.OpenSubKey(RegistryPath);
using var key = isUninstall
? Registry.LocalMachine.OpenSubKey(UninstallRegistryPath)
: Registry.LocalMachine.OpenSubKey(RegistryPath);
if (key != null)
{
var value = key.GetValue(option);
if (value != null)
{
return value.ToString()!;
}
else
{
Console.WriteLine($"Value for key '{option}' not found in the registry.");
return "";
}
}
else
{
Console.WriteLine($"Registry key '{RegistryPath}' not found.");
Console.WriteLine($"Value for key '{option}' not found in the registry.");
return "";
}
Console.WriteLine($"Registry key '{RegistryPath}' not found.");
return "";
}
catch (Exception ex)
{
Expand All @@ -145,14 +148,17 @@ public static string GetKey(string option)
}

/// <summary>
/// Create a Key in the Registry
/// Removes a Key in the Registry
/// </summary>
/// <param name="option">Key Name</param>
public static bool RemoveKey(string option)
/// <param name="isUninstall">Use the Uninstall Registry key instead.</param>
public static bool RemoveKey(string option, bool isUninstall = false)
{
try
{
using var key = Registry.LocalMachine.OpenSubKey(RegistryPath, RegistryKeyPermissionCheck.ReadWriteSubTree);
using var key = isUninstall
? Registry.LocalMachine.OpenSubKey(UninstallRegistryPath)
: Registry.LocalMachine.OpenSubKey(RegistryPath);
var value = key?.GetValue(option);
if (value != null)
{
Expand All @@ -163,7 +169,31 @@ public static bool RemoveKey(string option)
}
catch (Exception ex)
{
Console.WriteLine("An error has occurred while reading the registry: " + ex.Message);
Console.WriteLine("An error has occurred while removing a key from the registry: " + ex.Message);
}

return false;
}

/// <summary>
/// Removes a SubKey in the Registry
/// </summary>
/// <param name="option">SubKey Name</param>
/// <param name="isUninstall">Use the Uninstall Registry key instead.</param>
public static bool RemoveSubKey(string option, bool isUninstall = false)
{
try
{
using var key = isUninstall
? Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", true)
: Registry.LocalMachine.OpenSubKey(RegistryPath, true);
key?.DeleteSubKey(option);
key?.Close();
return true;
}
catch (Exception ex)
{
Console.WriteLine("An error has occurred while removing a subkey from the registry: " + ex.Message);
}

return false;
Expand Down
Loading

0 comments on commit 6254808

Please sign in to comment.