[PT Run] Web search should use user's default browser (#17304)

* Web search does not use my default browser #16549
updated DefaultBrowserInfo logic to get program location from shell open command not icon location

* Refactored DefaultBrowserInfo to start default browser with arguments specified in shell/open/command value for the browser.
Added fallback to Microsoft Edge if no browser information available.
#16549 Web search does not use my default browser

* fixed comment
#16549 Web search does not use my default browser
This commit is contained in:
lncubus
2022-04-12 17:10:05 +02:00
committed by GitHub
parent f778d010e5
commit 86783e9815
3 changed files with 85 additions and 151 deletions

View File

@@ -63,7 +63,7 @@ namespace Community.PowerToys.Run.Plugin.WebSearch
// empty non-global query: // empty non-global query:
if (!AreResultsGlobal() && query.ActionKeyword == query.RawQuery) if (!AreResultsGlobal() && query.ActionKeyword == query.RawQuery)
{ {
string arguments = "\"? \""; string arguments = "? ";
results.Add(new Result results.Add(new Result
{ {
Title = Properties.Resources.plugin_description.Remove(Description.Length - 1, 1), Title = Properties.Resources.plugin_description.Remove(Description.Length - 1, 1),
@@ -73,7 +73,7 @@ namespace Community.PowerToys.Run.Plugin.WebSearch
ProgramArguments = arguments, ProgramArguments = arguments,
Action = action => Action = action =>
{ {
if (!Helper.OpenInShell(BrowserInfo.Path ?? BrowserInfo.MSEdgePath, arguments)) if (!Helper.OpenCommandInShell(BrowserInfo.Path, BrowserInfo.ArgumentsPattern, arguments))
{ {
onPluginError(); onPluginError();
return false; return false;
@@ -105,37 +105,19 @@ namespace Community.PowerToys.Run.Plugin.WebSearch
IcoPath = _iconPath, IcoPath = _iconPath,
}; };
if (BrowserInfo.SupportsWebSearchByCmdLineArgument) string arguments = $"? {searchTerm}";
result.ProgramArguments = arguments;
result.Action = action =>
{ {
string arguments = $"\"? {searchTerm}\""; if (!Helper.OpenCommandInShell(BrowserInfo.Path, BrowserInfo.ArgumentsPattern, arguments))
result.ProgramArguments = arguments;
result.Action = action =>
{ {
if (!Helper.OpenInShell(BrowserInfo.Path ?? BrowserInfo.MSEdgePath, arguments)) onPluginError();
{ return false;
onPluginError(); }
return false;
}
return true; return true;
}; };
}
else
{
string url = string.Format(CultureInfo.InvariantCulture, BrowserInfo.SearchEngineUrl, searchTerm);
result.Action = action =>
{
if (!Helper.OpenInShell(url))
{
onPluginError();
return false;
}
return true;
};
}
results.Add(result); results.Add(result);
} }

View File

@@ -148,6 +148,16 @@ namespace Wox.Infrastructure
return Process.Start(processStartInfo); return Process.Start(processStartInfo);
} }
public static bool OpenCommandInShell(string path, string pattern, string arguments, string workingDir = null, ShellRunAsType runAs = ShellRunAsType.None, bool runWithHiddenWindow = false)
{
if (pattern.Contains("%1", StringComparison.Ordinal))
{
arguments = pattern.Replace("%1", arguments);
}
return OpenInShell(path, arguments, workingDir, runAs, runWithHiddenWindow);
}
public static bool OpenInShell(string path, string arguments = null, string workingDir = null, ShellRunAsType runAs = ShellRunAsType.None, bool runWithHiddenWindow = false) public static bool OpenInShell(string path, string arguments = null, string workingDir = null, ShellRunAsType runAs = ShellRunAsType.None, bool runWithHiddenWindow = false)
{ {
using (var process = new Process()) using (var process = new Process())

View File

@@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System; using System;
using System.IO.Abstractions;
using System.Text; using System.Text;
using Wox.Plugin.Common.Win32; using Wox.Plugin.Common.Win32;
using Wox.Plugin.Logger; using Wox.Plugin.Logger;
@@ -18,40 +17,40 @@ namespace Wox.Plugin.Common
private static readonly object _updateLock = new object(); private static readonly object _updateLock = new object();
private static int _lastUpdateTickCount = -1; private static int _lastUpdateTickCount = -1;
public static readonly string MSEdgePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\Microsoft\Edge\Application\msedge.exe"; /// <summary>Gets the path to the MS Edge browser executable.</summary>
public const string MSEdgeName = "Microsoft Edge"; public static string MSEdgePath =>
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) +
@"\Microsoft\Edge\Application\msedge.exe";
private static readonly IPath FilePath = new FileSystem().Path; /// <summary>Gets the command line pattern of the MS Edge.</summary>
public static string MSEdgeArgumentsPattern => "--single-argument %1";
public const string MSEdgeName = "Microsoft Edge";
/// <summary>Gets the path to default browser's executable.</summary> /// <summary>Gets the path to default browser's executable.</summary>
public static string Path { get; private set; } public static string Path { get; private set; }
/// <summary>Gets <see cref="Path" /> since the icon is embedded in the executable.</summary> /// <summary>Gets <see cref="Path"/> since the icon is embedded in the executable.</summary>
public static string IconPath { get => Path; } public static string IconPath => Path;
/// <summary>Gets the user-friendly name of the default browser.</summary>
public static string Name { get; private set; } public static string Name { get; private set; }
/// <summary>Gets a value indicating whether the browser supports querying a web search via command line argument (`? <term>`).</summary> /// <summary>Gets the command line pattern of the default browser.</summary>
public static bool SupportsWebSearchByCmdLineArgument { get => SearchEngineUrl is null; } public static string ArgumentsPattern { get; private set; }
public static bool IsDefaultBrowserSet { get => !string.IsNullOrEmpty(Path); } public static bool IsDefaultBrowserSet { get => !string.IsNullOrEmpty(Path); }
/// <summary> public const int UpdateTimeout = 300;
/// Gets the browser's default search engine's url ready to be formatted with a single value (like `https://www.bing.com/search?q={0}`)
/// </summary>
#pragma warning disable CA1056 // URI-like properties should not be strings
public static string SearchEngineUrl { get; private set; }
#pragma warning restore CA1056 // URI-like properties should not be strings
/// <summary> /// <summary>
/// Updates only if at least more than 300ms has passed since the last update, to avoid multiple calls to <see cref="Update"/>. /// Updates only if at least more than 300ms has passed since the last update, to avoid multiple calls to <see cref="Update"/>.
/// (because of multiple plugins calling update at the same time.) /// (because of multiple plugins calling update at the same time.)
/// </summary> /// </summary>
/// <param name="defaultToEdgeOnFail">If true, If <see cref="Update"/> fails, for any reason, the browser will be set to Microsoft Edge.</param>
public static void UpdateIfTimePassed() public static void UpdateIfTimePassed()
{ {
int curTickCount = Environment.TickCount; int curTickCount = Environment.TickCount;
if (curTickCount - _lastUpdateTickCount > 300) if (curTickCount - _lastUpdateTickCount > UpdateTimeout)
{ {
_lastUpdateTickCount = curTickCount; _lastUpdateTickCount = curTickCount;
Update(); Update();
@@ -72,131 +71,74 @@ namespace Wox.Plugin.Common
string progId = GetRegistryValue( string progId = GetRegistryValue(
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice", @"HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice",
"ProgId"); "ProgId");
string appName = GetRegistryValue($@"HKEY_CLASSES_ROOT\{progId}\Application", "ApplicationName")
?? GetRegistryValue($@"HKEY_CLASSES_ROOT\{progId}", "FriendlyTypeName");
// The `?` argument doesn't work on opera, so we get the user's default search engine: if (appName != null)
if (progId.StartsWith("Opera", StringComparison.OrdinalIgnoreCase))
{ {
// Opera user preferences file: // Handle indirect strings:
string prefFile; if (appName.StartsWith("@", StringComparison.Ordinal))
if (progId.Contains("GX", StringComparison.OrdinalIgnoreCase))
{ {
Name = "Opera GX"; appName = GetIndirectString(appName);
prefFile = System.IO.File.ReadAllText($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\Opera Software\\Opera GX Stable\\Preferences");
}
else
{
Name = "Opera";
prefFile = System.IO.File.ReadAllText($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\Opera Software\\Opera Stable\\Preferences");
} }
// "default_search_provider_data" doesn't exist if the user hasn't searched for the first time, appName = appName
// therefore we set `url` to opera's default search engine: .Replace("URL", null, StringComparison.OrdinalIgnoreCase)
string url = "https://www.google.com/search?client=opera&q={0}&sourceid=opera"; .Replace("HTML", null, StringComparison.OrdinalIgnoreCase)
.Replace("Document", null, StringComparison.OrdinalIgnoreCase)
.Replace("Web", null, StringComparison.OrdinalIgnoreCase)
.TrimEnd();
}
using (System.Text.Json.JsonDocument doc = System.Text.Json.JsonDocument.Parse(prefFile)) Name = appName;
string commandPattern = GetRegistryValue($@"HKEY_CLASSES_ROOT\{progId}\shell\open\command", null);
if (string.IsNullOrEmpty(commandPattern))
{
throw new ArgumentOutOfRangeException(
nameof(commandPattern),
"Default browser program command is not specified.");
}
if (commandPattern.StartsWith('@'))
{
commandPattern = GetIndirectString(commandPattern);
}
if (commandPattern.StartsWith('\"'))
{
var endQuoteIndex = commandPattern.IndexOf('\"', 1);
if (endQuoteIndex != -1)
{ {
if (doc.RootElement.TryGetProperty("default_search_provider_data", out var element)) Path = commandPattern.Substring(1, endQuoteIndex - 1);
{ ArgumentsPattern = commandPattern.Substring(endQuoteIndex + 1).Trim();
if (element.TryGetProperty("template_url_data", out element))
{
if (element.TryGetProperty("url", out element))
{
url = element.GetString();
}
}
}
} }
url = url
.Replace("{searchTerms}", "{0}", StringComparison.Ordinal)
.Replace("{inputEncoding}", "UTF-8", StringComparison.Ordinal)
.Replace("{outputEncoding}", "UTF-8", StringComparison.Ordinal);
int startIndex = url.IndexOf('}', StringComparison.Ordinal) + 1;
// In case there are other url parameters (e.g. `&foo={bar}`), remove them:
for (int i = url.IndexOf("}", startIndex, StringComparison.Ordinal);
i != -1;
i = url.IndexOf("}", startIndex, StringComparison.Ordinal))
{
for (int j = i - 1; j > 0; --j)
{
if (url[j] == '&')
{
url = url.Remove(j, i - j + 1);
break;
}
}
}
SearchEngineUrl = url;
} }
else else
{ {
string appName = GetRegistryValue($@"HKEY_CLASSES_ROOT\{progId}\Application", "ApplicationName") var spaceIndex = commandPattern.IndexOf(' ');
?? GetRegistryValue($@"HKEY_CLASSES_ROOT\{progId}", "FriendlyTypeName"); if (spaceIndex != -1)
if (appName != null)
{ {
// Handle indirect strings: Path = commandPattern.Substring(0, spaceIndex);
if (appName.StartsWith("@", StringComparison.Ordinal)) ArgumentsPattern = commandPattern.Substring(spaceIndex + 1).Trim();
{
appName = GetIndirectString(appName);
}
appName = appName
.Replace("URL", null, StringComparison.OrdinalIgnoreCase)
.Replace("HTML", null, StringComparison.OrdinalIgnoreCase)
.Replace("Document", null, StringComparison.OrdinalIgnoreCase)
.TrimEnd();
} }
Name = appName;
SearchEngineUrl = null;
}
string programLocation =
// Resolve App Icon (UWP)
GetRegistryValue(
$@"HKEY_CLASSES_ROOT\{progId}\Application",
"ApplicationIcon")
// Resolves default file association icon (UWP + Normal)
?? GetRegistryValue($@"HKEY_CLASSES_ROOT\{progId}\DefaultIcon", null);
// "Handles 'Indirect Strings' (UWP programs)"
// Using Ordinal since this is internal and used with a symbol
if (programLocation.StartsWith("@", StringComparison.Ordinal))
{
string directProgramLocation = GetIndirectString(programLocation);
Path = string.Equals(FilePath.GetExtension(directProgramLocation), ".exe", StringComparison.Ordinal)
? directProgramLocation
: null;
}
else
{
// Using Ordinal since this is internal and used with a symbol
var indexOfComma = programLocation.IndexOf(',', StringComparison.Ordinal);
Path = indexOfComma > 0
? programLocation.Substring(0, indexOfComma)
: programLocation;
} }
if (string.IsNullOrEmpty(Path)) if (string.IsNullOrEmpty(Path))
{ {
throw new ArgumentOutOfRangeException(nameof(Path), "Browser path is null or empty."); throw new ArgumentOutOfRangeException(
nameof(Path),
"Default browser program path could not be determined.");
} }
} }
catch (Exception e) catch (Exception e)
{ {
Path = null; // fallback to MS Edge
Name = null; Path = MSEdgePath;
SearchEngineUrl = null; Name = MSEdgeName;
ArgumentsPattern = MSEdgeArgumentsPattern;
Log.Exception("Exception when retrieving browser path/name. Path and Name are null.", e, typeof(DefaultBrowserInfo)); Log.Exception("Exception when retrieving browser path/name. Path and Name are set to use Microsoft Edge.", e, typeof(DefaultBrowserInfo));
} }
string GetRegistryValue(string registryLocation, string valueName) string GetRegistryValue(string registryLocation, string valueName)
@@ -217,7 +159,7 @@ namespace Wox.Plugin.Common
return stringBuilder.ToString(); return stringBuilder.ToString();
} }
throw new ArgumentNullException(nameof(Path), "Could not load indirect string."); throw new ArgumentNullException(nameof(str), "Could not load indirect string.");
} }
} }
} }