forked from ibebbs/ScriptVsNewWindow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
235 lines (197 loc) · 8.74 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace ScriptVsNewWindow
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly ObservableCollection<string> log = new();
private readonly CoreWebView2CreationProperties webView2CreationProperties;
private bool enableLog = true;
public event PropertyChangedEventHandler? PropertyChanged;
public MainWindow()
{
InitializeComponent();
var executablePath = GetCanaryWebViewPathIfAvailable();
this.webView2CreationProperties = new CoreWebView2CreationProperties()
{
AdditionalBrowserArguments = "--auto-open-devtools-for-tabs",
};
if (executablePath != null)
{
this.webView2CreationProperties.BrowserExecutableFolder = executablePath;
}
_ = InitializeAsync();
}
private string? GetCanaryWebViewPathIfAvailable() =>
Directory
.EnumerateDirectories(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\Edge SxS\Application"), "*", SearchOption.TopDirectoryOnly)
.Where(path => Version.TryParse(Path.GetFileName(path), out _))
.FirstOrDefault();
public ObservableCollection<string> Log => this.log;
private async Task InitializeAsync()
{
this.WebView.CreationProperties = this.webView2CreationProperties;
await this.WebView.EnsureCoreWebView2Async();
await this.WebView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("console.log('script injected')");
this.WebView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
this.WebView.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
this.WebView.NavigateToString(HTML.OpenWindow);
LogEvent($"Using WebView2 version {this.WebView.CoreWebView2.Environment.BrowserVersionString}");
}
private void LogEvent(string @event)
{
if (this.enableLog)
{
this.log.Add($"[{DateTime.Now.ToString("hh:mm:ss.ffff")}] {@event}");
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Log)));
}
}
private void CoreWebView2_NavigationStarting(object? sender, CoreWebView2NavigationStartingEventArgs e)
{
if (sender is CoreWebView2 webView)
{
LogEvent($"NavigationStarting - '{webView.Source}'");
}
}
private void CoreWebView2_ContentLoading(object? sender, CoreWebView2ContentLoadingEventArgs e)
{
if (sender is CoreWebView2 webView)
{
LogEvent($"ContentLoading - '{webView.Source}'");
}
}
private void CoreWebView2_NavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs e)
{
this.enableLog = true;
}
private async void CoreWebView2_NewWindowRequested(object? sender, Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs e)
{
if (e.Uri == "https://random.test.url/")
{
e.Handled = true;
_ = Task.Factory.StartNew(() => StartPostNavigationTest(), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
return;
}
var _deferral = e.GetDeferral();
if (this.ScheduleNewWindow.IsChecked == true)
{
_ = Dispatcher.InvokeAsync(() => OpenNewWindowAsync(e, _deferral));
}
else
{
await OpenNewWindowAsync(e, _deferral);
}
}
private void StartPostNavigationTest()
{
this.WebView.CoreWebView2.DOMContentLoaded += CoreWebView2_DOMContentLoaded;
this.WebView.CoreWebView2.Navigate("https://lite.duckduckgo.com/lite");
void CoreWebView2_DOMContentLoaded(object? sender, CoreWebView2DOMContentLoadedEventArgs e)
{
this.WebView.CoreWebView2.DOMContentLoaded -= CoreWebView2_DOMContentLoaded;
this.WebView.CoreWebView2.ExecuteScriptAsync("""
if (window.location.href === 'https://lite.duckduckgo.com/lite') {
const form = document.getElementsByTagName('form')[0]
form.setAttribute('target', '_blank')
form.setAttribute('rel', 'opener')
document.querySelectorAll('input[class="query"]')[0].value = 'test'
form.submit()
}
""");
}
}
private async Task OpenNewWindowAsync(CoreWebView2NewWindowRequestedEventArgs e, CoreWebView2Deferral deferral)
{
Window window = new Window
{
Width = Width,
Height = Height,
Left = Left + 100,
Top = Top + 100
};
var newWebView = new WebView2();
newWebView.CreationProperties = this.webView2CreationProperties;
window.Content = newWebView;
window.Show();
await newWebView.EnsureCoreWebView2Async();
LogEvent($"Creating new WebView2 version {newWebView.CoreWebView2.Environment.BrowserVersionString}");
newWebView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
newWebView.CoreWebView2.NavigationStarting += CoreWebView2_NavigationStarting;
newWebView.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
newWebView.CoreWebView2.ContentLoading += CoreWebView2_ContentLoading;
if (this.SetScripts.SelectedIndex == 1)
{
LogEvent($"Start Loading Scripts");
await newWebView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("console.log('script injected - before setting NewWindow')");
if (this.Delay.SelectedIndex > 0)
{
await Task.Delay(TimeSpan.FromMilliseconds(1000 * this.Delay.SelectedIndex));
}
LogEvent($"Completed Loading Scripts");
}
if (this.SetNewWindow.IsChecked == true)
{
LogEvent($"Assigning NewWindow");
e.NewWindow = newWebView.CoreWebView2;
LogEvent($"Assigned NewWindow");
}
if (this.SetScripts.SelectedIndex == 0)
{
LogEvent($"Start Loading Scripts");
await newWebView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("console.log('script injected - after setting NewWindow')");
if (this.Delay.SelectedIndex > 0)
{
await Task.Delay(TimeSpan.FromMilliseconds(1000 * this.Delay.SelectedIndex));
}
LogEvent($"Completed Loading Scripts");
}
if (this.SetSource.IsChecked == true)
{
LogEvent($"Setting Source - '{e.Uri}'");
newWebView.Source = new Uri(e.Uri);
LogEvent($"Set Source - '{e.Uri}'");
}
e.Handled = true;
deferral.Complete();
}
private void CopyLog_Click(object sender, RoutedEventArgs e)
{
var builder = new StringBuilder();
foreach (var @event in this.log)
{
builder.AppendLine(@event);
}
Clipboard.SetText(builder.ToString());
}
private void ClearLog_Click(object sender, RoutedEventArgs e)
{
this.log.Clear();
}
private void TextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter && sender is TextBox textBox &&
(Uri.TryCreate(textBox.Text, UriKind.Absolute, out var uri) || Uri.TryCreate($"https://{textBox.Text}", UriKind.Absolute, out uri)))
{
this.WebView.CoreWebView2.Navigate(uri.AbsoluteUri);
}
}
private void OpenDefaultHomepage(object sender, RoutedEventArgs e)
{
this.WebView.NavigateToString(HTML.OpenWindow);
}
}
}