-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWebpagePanel.cs
More file actions
377 lines (342 loc) · 15.8 KB
/
WebpagePanel.cs
File metadata and controls
377 lines (342 loc) · 15.8 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright © 2022 Frank Becker
using System;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using Newtonsoft.Json;
using QuickLook.Common.Helpers;
using UtfUnknown;
// Note: see webview2 sample app:
// https://github.com/MicrosoftEdge/WebView2Samples/blob/main/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs
public class FileData
{
public string fileName { get; set; }
public long fileSize { get; set; }
public bool isBinary { get; set; }
public string textContent { get; set; }
}
public class CommandMessage
{
public string command { get; set; }
public string[] data { get; set; }
public bool boolValue;
}
public class InitData
{
public string langCode { get; set; }
public bool detectEncoding { get; set; }
public bool showTrayIcon { get; set; }
public bool useTransparency { get; set; }
}
namespace QuickLook.Plugin.WebViewPlus
{
public class WebpagePanel : UserControl
{
public static readonly string DefaultExtensions =
"html,htm,mht,mhtml,pdf,epub,csv,xlsx,svg,md,markdown,gltf,glb,c++,h++,bat,c,cmake,cpp,cs,css,d,go,h,hpp,java,js,json,jsx,kt,lua,m,mm,makefile,pas,perl,php,pl,ps1,psm1,py,r,rb,rs,sass,scala,scss,sh,sql,swift,tex,ts,tsx,txt,webp,xml,yaml,yml";
public static string[] Extensions = SettingHelper.Get("ExtensionList", WebpagePanel.DefaultExtensions, "QuickLook.Plugin.WebViewPlus").Split(',');
// These should match the ones in the web app openFile.ts:BINARY_EXTENSIONS
private static readonly string[] _binExtensions = "pdf,epub,xlsx,xls,ods,gltf,glb,fbx,obj,webp,jpg,jpeg,png,apng,gif,bmp,avif,ttf,otf,woff,woff2".Split(',');
private Uri _currentUri;
private WebView2 _webView;
private bool _webAppReady = false;
private CoreWebView2SharedBuffer _sharedBuffer = null;
private FileInfo _activeFileInfo = null;
private CoreWebView2Environment _webViewEnvironment;
private bool DetectEncoding = false;
private static bool firstPreview = true;
public WebpagePanel()
{
DetectEncoding = SettingHelper.Get<bool>("DetectEncoding", false, "QuickLook.Plugin.WebViewPlus");
var unavailReason = WebpagePanel.WebView2UnavailableReason();
if (unavailReason != null)
{
Content = CreateDownloadButton(unavailReason);
}
else
{
_webView = new WebView2
{
CreationProperties = new CoreWebView2CreationProperties
{
// Note: using a different user data folder than the built-in HTmlViewer plugin
// This plugin starts the webview once and keeps it running and would effectively
// block the HTmlViewer if it used the same folder.
// See "Process model for WebView2 apps"
UserDataFolder = Path.Combine(SettingHelper.LocalDataPath, @"WebViewPlus_Data\\"),
Language = CultureInfo.CurrentUICulture.Name
},
// Match background color in theme.tsx in WebViewPlus
// Note: this will not adjust if dark/light is swapped via the in-app yingyang toggle
DefaultBackgroundColor = OSThemeHelper.AppsUseDarkTheme() ? Color.FromArgb(0x21, 0x21, 0x21) : Color.FromArgb(0xee, 0xee, 0xee),
};
_webView.NavigationStarting += NavigationStarting_CancelNavigation;
_webView.CoreWebView2InitializationCompleted += CoreWebView2InitializationCompleted;
_webView.EnsureCoreWebView2Async();
Content = _webView;
}
}
public static string WebView2UnavailableReason()
{
try
{
var version = CoreWebView2Environment.GetAvailableBrowserVersionString();
// verion format example: "110.0.1556.0 dev"
// This plugin uses an experimental feature introduced in 1466
// https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes?tabs=dotnetcsharp#101466-prerelease
var buildNum = Int32.Parse(version.Split('.')[2]);
if (buildNum < 1466)
{
var reason = $"QuickLook.Plugin.WebViewPlus found incompatible webview2: {version} - 1466 or higher needed";
return reason;
}
// TODO: validate api availability.
return null;
}
catch (Exception)
{
return "Viewing this file requires Microsoft Edge WebView2 (build 1466 or higher) to be installed.";
}
}
CoreWebView2Environment WebViewEnvironment
{
get
{
if (_webViewEnvironment == null && _webView?.CoreWebView2 != null)
{
_webViewEnvironment = _webView.CoreWebView2.Environment;
}
return _webViewEnvironment;
}
}
public void NavigateToFile(string path)
{
_activeFileInfo = new FileInfo(path);
if (_webAppReady)
{
sendFileData();
}
}
void sendFileData()
{
if (_sharedBuffer != null)
{
_sharedBuffer.Dispose();
_sharedBuffer = null;
}
var extension = _activeFileInfo.Extension.ToLower().Substring(1);
var isBinary = _binExtensions.Any(extension.Equals);
var textContent = "";
if (isBinary)
{
_sharedBuffer = WebViewEnvironment.CreateSharedBuffer((ulong)_activeFileInfo.Length);
using (BinaryWriter writer = new BinaryWriter(_sharedBuffer.OpenStream()))
{
using (var br = new BinaryReader(new FileStream(_activeFileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
writer.Write(br.ReadBytes((int)br.BaseStream.Length));
}
}
}
else
{
_sharedBuffer = WebViewEnvironment.CreateSharedBuffer(1);
var encoding = Encoding.UTF8;
if (DetectEncoding)
{
encoding = CharsetDetector.DetectFromFile(_activeFileInfo).Detected?.Encoding ?? encoding;
}
using (var sr = new StreamReader(new FileStream(_activeFileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), encoding))
{
textContent = sr.ReadToEnd();
}
}
var json = JsonConvert.SerializeObject(
new FileData
{
fileName = _activeFileInfo.Name,
fileSize = _activeFileInfo.Length,
isBinary = isBinary,
textContent = textContent
}
);
// read-write access needed for epub/jszip. ArrayReader writes to itself.
// See https://github.com/Stuk/jszip/blob/v3.10.1/lib/reader/ArrayReader.js#L8
_webView.CoreWebView2.PostSharedBufferToScript(_sharedBuffer, CoreWebView2SharedBufferAccess.ReadWrite, json);
}
void WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs args)
{
var msg = JsonConvert.DeserializeObject<CommandMessage>(args.WebMessageAsJson);
switch(msg.command)
{
case "AppReadyForData":
_webAppReady = true;
// If the user profile was previously generated (WebView2_Data), it doesn't seem to change the navigator.language setting
// when a webview is created with a different language in 'new WebView2' above.
// You'd have to delete the WebView2_Data folder first.
// Oh, well. Explicitly sending current language to web app, instead.
var json = JsonConvert.SerializeObject(
new InitData
{
langCode = CultureInfo.CurrentUICulture.Name,
detectEncoding = SettingHelper.Get("DetectEncoding", false, "QuickLook.Plugin.WebViewPlus"),
showTrayIcon = SettingHelper.Get("ShowTrayIcon", true),
useTransparency = SettingHelper.Get("UseTransparency", true)
}
);
_webView.CoreWebView2.PostWebMessageAsString($"initData:{json}");
sendFileData();
break;
case "Extensions":
SettingHelper.Set("ExtensionList", String.Join(",", msg.data), "QuickLook.Plugin.WebViewPlus");
Extensions = SettingHelper.Get("ExtensionList", WebpagePanel.DefaultExtensions, "QuickLook.Plugin.WebViewPlus").Split(',');
break;
case "DetectEncoding":
SettingHelper.Set("DetectEncoding", msg.boolValue, "QuickLook.Plugin.WebViewPlus");
DetectEncoding = SettingHelper.Get("DetectEncoding", false, "QuickLook.Plugin.WebViewPlus");
break;
case "ShowTrayIcon":
SettingHelper.Set("ShowTrayIcon", msg.boolValue);
break;
case "UseTransparency":
SettingHelper.Set("UseTransparency", msg.boolValue);
break;
case "Restart":
RestartQuicklook();
break;
default:
break;
}
}
private void RestartQuicklook()
{
// Restart QL. Clunky way to delay startup to pass already running check.
ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C ping 127.0.0.1 -n 2 && \"" + string.Join(" ", Environment.GetCommandLineArgs()) + "\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
Process.GetCurrentProcess().Kill();
}
private void CoreWebView2InitializationCompleted(object sender, EventArgs e)
{
_webView.CoreWebView2.WebMessageReceived += WebMessageReceived;
_webView.CoreWebView2.NewWindowRequested += NewWindowRequested;
_webView.CoreWebView2.FrameNavigationStarting += FrameNavigationStarting;
_webView.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
_webView.CoreWebView2.Settings.AreHostObjectsAllowed = false;
// 3 places to get web app:
// 1. a url via WebAppUrl (e.g. locally hosted vite dev version of app)
// 2. in SettingHelper.LocalDataPath (same place as config files)
// 3. bundled with plugin
var webAppInConfigFolder = Path.Combine(SettingHelper.LocalDataPath, "webviewplus");
var webAppInBundledFolder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "webviewplus");
var uri = new Uri("https://webviewplus.mooflu.com/index.html");
var webAppUrl = SettingHelper.Get<string>("WebAppUrl", null, "QuickLook.Plugin.WebViewPlus");
if (webAppUrl != null)
{
// ProcessHelper.WriteLog($"QuickLook.Plugin.WebViewPlus using app via custom url: {webAppUrl}");
uri = new Uri(webAppUrl);
}
else
{
// map webviewplus folder to a private virtual hostname
if (File.Exists(Path.Combine(webAppInConfigFolder, "index.html")))
{
// ProcessHelper.WriteLog("QuickLook.Plugin.WebViewPlus using app from config");
_webView.CoreWebView2.SetVirtualHostNameToFolderMapping("webviewplus.mooflu.com", webAppInConfigFolder, CoreWebView2HostResourceAccessKind.Allow);
}
else if(File.Exists(Path.Combine(webAppInBundledFolder, "index.html")))
{
// ProcessHelper.WriteLog("QuickLook.Plugin.WebViewPlus using app from plugin");
_webView.CoreWebView2.SetVirtualHostNameToFolderMapping("webviewplus.mooflu.com", webAppInBundledFolder, CoreWebView2HostResourceAccessKind.Allow);
}
}
_webView.Source = uri;
_currentUri = _webView.Source;
}
private void NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
// Prevent new windows being opened (e.g. ctrl-click link)
e.Handled = true;
if (_webAppReady)
{
_webView.CoreWebView2.PostWebMessageAsString("newWindowRejected");
}
}
private void NavigationStarting_CancelNavigation(object sender, CoreWebView2NavigationStartingEventArgs e)
{
var newUri = new Uri(e.Uri);
if (newUri != _currentUri) e.Cancel = true;
}
private void FrameNavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
{
if (!e.Uri.StartsWith("blob:") && !e.Uri.StartsWith("about:"))
{
e.Cancel = true;
if (_webAppReady)
{
_webView.CoreWebView2.PostWebMessageAsString("frameNavigationRejected");
}
}
}
// No longer used - Changes in webview2 caused crashes due to
// "WebView2 controller will be auto closed if the parent hwnd is destroyed"
public void UnloadData()
{
_activeFileInfo = null;
_sharedBuffer?.Dispose();
_sharedBuffer = null;
if (_webAppReady)
{
_webView.CoreWebView2.PostWebMessageAsString("unload");
}
}
public void Dispose()
{
if (firstPreview)
{
firstPreview = false;
_webView.CoreWebView2.Profile.ClearBrowsingDataAsync(
CoreWebView2BrowsingDataKinds.FileSystems |
CoreWebView2BrowsingDataKinds.IndexedDb |
// keep local storage
CoreWebView2BrowsingDataKinds.WebSql |
CoreWebView2BrowsingDataKinds.CacheStorage |
CoreWebView2BrowsingDataKinds.Cookies |
CoreWebView2BrowsingDataKinds.DiskCache |
CoreWebView2BrowsingDataKinds.DownloadHistory |
CoreWebView2BrowsingDataKinds.GeneralAutofill |
CoreWebView2BrowsingDataKinds.PasswordAutosave |
CoreWebView2BrowsingDataKinds.Settings
);
}
_activeFileInfo = null;
_sharedBuffer?.Dispose();
_sharedBuffer = null;
_webView?.Dispose();
_webView = null;
}
private object CreateDownloadButton(string reason)
{
var button = new Button
{
Content = reason,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Padding = new Thickness(20, 6, 20, 6)
};
button.Click += (sender, e) => Process.Start("https://go.microsoft.com/fwlink/p/?LinkId=2124703");
return button;
}
}
}