-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
132 lines (117 loc) · 4.96 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Win32;
// ReSharper disable CommentTypo
namespace iexplore {
public partial class MainWindow/* : Window*/ {
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool FreeConsole();
public bool extoff; //ie: -extoff | chrome: --disable-extensions | means: No extensions
public bool prv; //ie: -private | chrome: --incognito | means: Incognito mode
public bool kiosk; //ie: -k | chrome: --enable-kiosk-mode & --start-fullscreen | means: Fullscreen mode
public List<string> urls;
public List<string> args;
public List<string> log;
public bool logging;
public MainWindow() {
Hide();
FreeConsole();
urls = new List<string>();
args = new List<string>();
log = new List<string>();
args = Environment.GetCommandLineArgs().ToList();
args.RemoveAt(0);
if(File.Exists(GetSelf() + "log.txt")) {
logging = true;
log.Add(">>File: \"" + GetSelf() + "log.txt\" found, logging enabled<<");
}
Start();
//InitializeComponent(); //Starts the window
}
public void Start() {
if (args != null && args.Count >= 1) {
foreach (string arg in args) {
Debug.WriteLine("Got Arg: " + arg);
ParseArg(arg);
}
}
RunBrowser(); //Currently chrome only
if (logging) { SaveLog(); }
Environment.Exit(0);
}
public string GetSelf() => Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\";
public void ParseArg(string arg) {
switch (arg) {
case @"-extoff":
extoff = true;
log.Add("Arg: \"" + arg + "\" | Response: extoff = true");
Debug.WriteLine("Arg: \"" + arg + "\" | Response: extoff = true");
break;
case "-private":
prv = true;
log.Add("Arg: \"" + arg + "\" | Response: prv = true");
Debug.WriteLine("Arg: \"" + arg + "\" | Response: prv = true");
break;
case "-k":
kiosk = true;
log.Add("Arg: \"" + arg + "\" | Response: kiosk = true");
Debug.WriteLine("Arg: \"" + arg + "\" | Response: kiosk = true");
break;
default:
if (arg.Contains("http") || arg.Contains("//") || arg.Contains("www.")) {
urls.Add(arg);
log.Add("<>Found Url: \"" + arg + "\"<>");
} else {
log.Add("<<Unknown Arg: \"" + arg + "\">>");
}
break;
}
}
public string GetChromePath() {
try {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe")) {
return key?.GetValue(null).ToString();
}
} catch (Exception e) {
log.Add("<<Caught exception when reading registry: [[" + e + "]]>>");
Debug.WriteLine("<<Caught exception when reading registry: \n" + e + ">>");
}
return null;
}
public string GetExt() => (extoff ? "--disable-extensions " : "") + (prv ? "--incognito " : "") + (kiosk ? @"--start-maximized" : "");
public void RunBrowser() {
if (urls == null || urls.Count < 1) {
log.Add("<<No URL was found! Opening blank session>>");
Process p = new Process {
StartInfo = {
FileName = GetChromePath(),
Arguments = GetExt()
}
};
p.Start();
} else {
log.Add(">>Running: " + GetChromePath() + " " + GetExt() + " \"" + urls[0] + "\"<<");
try {
Process p = new Process {
StartInfo = {
FileName = GetChromePath(),
Arguments = GetExt() + " \"" + urls[0] + "\""
}
};
p.Start();
} catch (Exception e) {
log.Add("<<Caught exception when loading chrome: [[" + e + "]]>>");
Debug.WriteLine("<<Caught exception when loading chrome: \n" + e + ">>");
}
}
}
public void SaveLog() {
log.Add("<>Saving Log<>");
File.WriteAllLines(GetSelf() + "log.txt", log);
}
}
}