-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrowserForm.cs
199 lines (179 loc) · 6.11 KB
/
BrowserForm.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;
using System.IO;
using Microsoft.Web.WebView2.WinForms;
using System.Threading;
using System.Collections.Generic;
namespace WebView2WindowsFormsBrowser
{
public partial class BrowserForm : Form
{
public BrowserForm()
{
InitializeComponent();
InitializeBrowser();
AttachControlEventHandlers();
HandleResize();
}
private void InitializeBrowser()
{
var options = new CoreWebView2EnvironmentOptions
{
AllowSingleSignOnUsingOSPrimaryAccount = true,
Language = $"{Globals.APP_REQUEST_LANG}",
};
if (!Directory.Exists(Globals.USER_DATA_FOLDER))
{
Directory.CreateDirectory(Globals.USER_DATA_FOLDER);
}
var webView2Environment = CoreWebView2Environment.CreateAsync(null, Globals.USER_DATA_FOLDER, options).Result;
_ = this.webView2Control.EnsureCoreWebView2Async(webView2Environment);
if (Environment.GetCommandLineArgs().Length > 1)
{
var outString = BrowserForm.RemoveSpecialChars(Environment.GetCommandLineArgs()[1]);
//Add code to check for gpu pram
if (outString.StartsWith("gpu"))
{
this.webView2Control.Source = new Uri($"edge://gpu", UriKind.Absolute);
}
else
{
this.webView2Control.Source = new Uri($"{Globals.BASE_URL}{outString}", UriKind.Absolute);
}
}
else
{
this.webView2Control.Source = new Uri($"{Globals.BASE_URL}", UriKind.Absolute);
}
}
#region Event Handlers
private void OnChanged(object sender, FileSystemEventArgs e)
{
var milliseconds = 100;
Thread.Sleep(milliseconds);
if (e.ChangeType != WatcherChangeTypes.Changed)
{
return;
}
string filePath = @Globals.USER_DATA_FOLDER + @"\temp.txt";
using (StreamReader inputFile = new StreamReader(filePath))
{
if (RemoveSpecialChars(inputFile.ReadToEnd()).StartsWith("gpu"))
{
webView2Control.Source = new Uri($"edge://gpu", UriKind.Absolute);
}
else
{
webView2Control.Source = new Uri($"{Globals.BASE_URL}" + RemoveSpecialChars(inputFile.ReadToEnd()));
}
}
this.Focus();
this.Activate();
}
void AttachControlEventHandlers()
{
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher($"{Globals.USER_DATA_FOLDER}");
var watcher = fileSystemWatcher;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Changed += OnChanged;
watcher.Filter = "temp.txt";
watcher.IncludeSubdirectories = false;
watcher.EnableRaisingEvents = true;
watcher.SynchronizingObject = this;
}
#endregion
#region UI event handlers
private void Form_Resize(object sender, EventArgs e)
{
HandleResize();
}
#endregion
private void HandleResize()
{
// Resize the webview
webView2Control.Size = this.ClientSize - new Size(webView2Control.Location);
}
private void Form1_Closing(object sender, FormClosingEventArgs e)
{
webView2Control.CoreWebView2.CallDevToolsProtocolMethodAsync("Network.clearBrowserCache", "{}");
}
private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
e.NewWindow = webView2Control.CoreWebView2;
}
private void WebView2Control_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
this.webView2Control.CoreWebView2.Settings.AreDefaultContextMenusEnabled = true;
this.webView2Control.CoreWebView2.Settings.AreDevToolsEnabled = false;
this.webView2Control.CoreWebView2.Settings.IsStatusBarEnabled = false;
this.webView2Control.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
this.webView2Control.CoreWebView2.Settings.AreHostObjectsAllowed = false;
this.webView2Control.CoreWebView2.Settings.IsWebMessageEnabled = false;
this.webView2Control.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
this.webView2Control.CoreWebView2.Settings.IsGeneralAutofillEnabled = false;
this.webView2Control.CoreWebView2.Settings.IsPasswordAutosaveEnabled = false;
this.webView2Control.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
this.webView2Control.CoreWebView2.ContextMenuRequested += menurequested;
}
public static string RemoveSpecialChars(string str)
{
str = str.Replace($"{Globals.URI_SCHEMA}", "");
// Create a string array and add the special characters you want to remove
string[] chars = new string[] { "~", "`", "!", "@", "#", "$", "%", "^", "*", "(", ")", "_", "+", "}", "{", "]", "[", "|", "\"", ":", "'", ":", ">", "<", "/", ".", ",", "\\" };
//Iterate the number of times based on the String array length.
for (int i = 0; i < chars.Length; i++)
{
if (str.Contains(chars[i]))
{
str = str.Replace(chars[i], "");
}
}
return str;
}
private void menurequested(object sender, CoreWebView2ContextMenuRequestedEventArgs args)
{
IList<CoreWebView2ContextMenuItem> menuList = args.MenuItems;
CoreWebView2ContextMenuTargetKind context = args.ContextMenuTarget.Kind;
if (context == CoreWebView2ContextMenuTargetKind.Audio)
{
for (int index = menuList.Count - 1; index >= 0; index--)
{
menuList.RemoveAt(index);
}
}
if (context == CoreWebView2ContextMenuTargetKind.Image)
{
for (int index = menuList.Count - 1; index >= 0; index--)
{
menuList.RemoveAt(index);
}
}
if (context == CoreWebView2ContextMenuTargetKind.Page)
{
for (int index = menuList.Count - 1; index >= 0; index--)
{
if (menuList[index].Name != "reload") { menuList.RemoveAt(index); }
}
}
if (context == CoreWebView2ContextMenuTargetKind.SelectedText)
{
for (int index = menuList.Count - 1; index >= 0; index--)
{
if (menuList[index].Name != "copy" && menuList[index].Name != "paste" && menuList[index].Name != "cut") { menuList.RemoveAt(index); }
}
}
if (context == CoreWebView2ContextMenuTargetKind.Video)
{
for (int index = menuList.Count - 1; index >= 0; index--)
{
menuList.RemoveAt(index);
}
}
}
}
}