Added -NativeMessagingInput CLI argument which is now used by NativeMessagingHost

This commit is contained in:
Jaex 2021-12-20 09:03:18 +03:00
parent ae39dbd947
commit 4057ec27cb
7 changed files with 62 additions and 46 deletions

View file

@ -1232,7 +1232,7 @@ public static string GetAbsolutePath(string path)
return Path.GetFullPath(path); return Path.GetFullPath(path);
} }
public static string GetTempPath(string extension) public static string GetTempFilePath(string extension)
{ {
string path = Path.GetTempFileName(); string path = Path.GetTempFileName();
return Path.ChangeExtension(path, extension); return Path.ChangeExtension(path, extension);
@ -1391,7 +1391,8 @@ public static Cursor CreateCursor(byte[] data)
public static string EscapeCLIText(string text) public static string EscapeCLIText(string text)
{ {
return string.Format("\"{0}\"", text.Replace("\\", "\\\\").Replace("\"", "\\\"")); string escapedText = text.Replace("\\", "\\\\").Replace("\"", "\\\"");
return $"\"{escapedText}\"";
} }
public static string BytesToHex(byte[] bytes) public static string BytesToHex(byte[] bytes)

View file

@ -23,7 +23,6 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3) #endregion License Information (GPL v3)
using Newtonsoft.Json;
using ShareX.HelpersLib; using ShareX.HelpersLib;
using System; using System;
using System.IO; using System.IO;
@ -40,7 +39,16 @@ private static void Main(string[] args)
{ {
try try
{ {
Run(); string input = GetNativeMessagingInput();
if (!string.IsNullOrEmpty(input))
{
string filePath = Helpers.GetAbsolutePath("ShareX.exe");
string tempFilePath = Helpers.GetTempFilePath("json");
File.WriteAllText(tempFilePath, input, Encoding.UTF8);
string argument = $"-NativeMessagingInput \"{tempFilePath}\"";
NativeMethods.CreateProcess(filePath, argument, CreateProcessFlags.CREATE_BREAKAWAY_FROM_JOB);
}
} }
catch (Exception e) catch (Exception e)
{ {
@ -54,40 +62,7 @@ private static void Main(string[] args)
} }
} }
private static void Run() private static string GetNativeMessagingInput()
{
string input = GetInput();
if (!string.IsNullOrEmpty(input))
{
NativeMessagingInput nativeMessagingInput = JsonConvert.DeserializeObject<NativeMessagingInput>(input);
if (nativeMessagingInput != null)
{
string argument = null;
if (!string.IsNullOrEmpty(nativeMessagingInput.URL))
{
argument = Helpers.EscapeCLIText(nativeMessagingInput.URL);
}
else if (!string.IsNullOrEmpty(nativeMessagingInput.Text))
{
string filepath = Helpers.GetTempPath("txt");
File.WriteAllText(filepath, nativeMessagingInput.Text, Encoding.UTF8);
argument = $"\"{filepath}\"";
}
if (!string.IsNullOrEmpty(argument))
{
string path = Helpers.GetAbsolutePath("ShareX.exe");
NativeMethods.CreateProcess(path, argument, CreateProcessFlags.CREATE_BREAKAWAY_FROM_JOB);
}
}
}
}
private static string GetInput()
{ {
Stream inputStream = Console.OpenStandardInput(); Stream inputStream = Console.OpenStandardInput();

View file

@ -48,7 +48,6 @@
<Compile Include="..\SharedAssemblyInfo.cs"> <Compile Include="..\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link> <Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile> </Compile>
<Compile Include="NativeMessageInput.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
@ -61,11 +60,6 @@
<ItemGroup> <ItemGroup>
<None Include="app.config" /> <None Include="app.config" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json">
<Version>13.0.1</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View file

@ -23,7 +23,7 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3) #endregion License Information (GPL v3)
namespace ShareX.NativeMessagingHost namespace ShareX
{ {
public class NativeMessagingInput public class NativeMessagingInput
{ {

View file

@ -219,6 +219,7 @@
<Compile Include="Forms\TextUploadForm.Designer.cs"> <Compile Include="Forms\TextUploadForm.Designer.cs">
<DependentUpon>TextUploadForm.cs</DependentUpon> <DependentUpon>TextUploadForm.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="NativeMessageInput.cs" />
<Compile Include="NewsItem.cs" /> <Compile Include="NewsItem.cs" />
<Compile Include="NewsManager.cs" /> <Compile Include="NewsManager.cs" />
<Compile Include="..\SharedAssemblyInfo.cs"> <Compile Include="..\SharedAssemblyInfo.cs">

View file

@ -51,7 +51,8 @@ public void UseCommandLineArgs(List<CLICommand> commands)
if (command.IsCommand) if (command.IsCommand)
{ {
if (CheckCustomUploader(command) || CheckImageEffect(command) || CheckCLIHotkey(command) || CheckCLIWorkflow(command)) if (CheckCustomUploader(command) || CheckImageEffect(command) || CheckCLIHotkey(command) || CheckCLIWorkflow(command) ||
CheckNativeMessagingInput(command))
{ {
} }
@ -153,5 +154,20 @@ private bool CheckCLIWorkflow(CLICommand command)
return false; return false;
} }
private bool CheckNativeMessagingInput(CLICommand command)
{
if (command.Command.Equals("NativeMessagingInput", StringComparison.InvariantCultureIgnoreCase))
{
if (!string.IsNullOrEmpty(command.Parameter) && command.Parameter.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
{
TaskHelpers.HandleNativeMessagingInput(command.Parameter);
}
return true;
}
return false;
}
} }
} }

View file

@ -1727,6 +1727,35 @@ public static void ImportImageEffect(string filePath)
} }
} }
public static void HandleNativeMessagingInput(string filePath)
{
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
NativeMessagingInput nativeMessagingInput = null;
try
{
nativeMessagingInput = JsonHelpers.DeserializeFromFile<NativeMessagingInput>(filePath);
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
if (nativeMessagingInput != null)
{
if (!string.IsNullOrEmpty(nativeMessagingInput.URL) && URLHelpers.IsValidURL(nativeMessagingInput.URL))
{
UploadManager.DownloadAndUploadFile(nativeMessagingInput.URL);
}
else if (!string.IsNullOrEmpty(nativeMessagingInput.Text))
{
UploadManager.UploadText(nativeMessagingInput.Text);
}
}
}
}
public static void OpenActionsToolbar() public static void OpenActionsToolbar()
{ {
ActionsToolbarForm.Instance.ForceActivate(); ActionsToolbarForm.Instance.ForceActivate();