ShareX/ShareX.NativeMessagingHost/Program.cs

95 lines
3.2 KiB
C#
Raw Normal View History

2016-08-27 10:15:39 +12:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2017-01-11 21:39:40 +13:00
Copyright (c) 2007-2017 ShareX Team
2016-08-27 10:15:39 +12:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using ShareX.HelpersLib;
2016-08-27 10:15:39 +12:00
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace ShareX.NativeMessagingHost
2016-08-27 10:15:39 +12:00
{
internal class Program
{
private static void Main(string[] args)
{
try
{
Run();
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "ShareX NativeMessagingHost - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
2016-08-27 10:15:39 +12:00
}
}
private static void Run()
{
string input = GetInput();
if (!string.IsNullOrEmpty(input))
{
2017-02-11 03:32:18 +13:00
NativeMessagingInput chromeInput = JsonConvert.DeserializeObject<NativeMessagingInput>(input);
2016-08-27 10:15:39 +12:00
if (chromeInput != null)
{
string argument = null;
if (!string.IsNullOrEmpty(chromeInput.URL))
{
2017-01-27 07:47:58 +13:00
argument = Helpers.EscapeCLIText(chromeInput.URL);
2016-08-27 10:15:39 +12:00
}
else if (!string.IsNullOrEmpty(chromeInput.Text))
{
2017-01-27 04:42:43 +13:00
string filepath = Helpers.GetTempPath("txt");
2016-08-27 10:15:39 +12:00
File.WriteAllText(filepath, chromeInput.Text, Encoding.UTF8);
2017-01-27 08:49:18 +13:00
argument = $"\"{filepath}\"";
2016-08-27 10:15:39 +12:00
}
if (!string.IsNullOrEmpty(argument))
{
2017-01-27 04:42:43 +13:00
string path = Helpers.GetAbsolutePath("ShareX.exe");
2017-01-27 07:47:58 +13:00
NativeMethods.CreateProcess(path, argument, CreateProcessFlags.CREATE_BREAKAWAY_FROM_JOB);
2016-08-27 10:15:39 +12:00
}
}
}
}
private static string GetInput()
{
Stream inputStream = Console.OpenStandardInput();
byte[] bytesLength = new byte[4];
inputStream.Read(bytesLength, 0, bytesLength.Length);
int inputLength = BitConverter.ToInt32(bytesLength, 0);
byte[] bytesInput = new byte[inputLength];
inputStream.Read(bytesInput, 0, bytesInput.Length);
return Encoding.UTF8.GetString(bytesInput);
}
}
}