|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Windows.Forms; |
| 4 | +using Microsoft.Web.WebView2.Core; |
| 5 | +using Microsoft.Web.WebView2.WinForms; |
| 6 | +using Newtonsoft.Json; |
| 7 | + |
| 8 | +namespace Proxmox_Desktop_Client.Classes.consoles |
| 9 | +{ |
| 10 | + public partial class WebGUI : Form |
| 11 | + { |
| 12 | + private WebView2 webView; |
| 13 | + |
| 14 | + public WebGUI() |
| 15 | + { |
| 16 | + InitializeComponent(); |
| 17 | + CenterToScreen(); |
| 18 | + InitializeWebView(); |
| 19 | + this.WindowState = FormWindowState.Maximized; |
| 20 | + } |
| 21 | + |
| 22 | + private async void InitializeWebView() |
| 23 | + { |
| 24 | + try |
| 25 | + { |
| 26 | + webView = new WebView2 |
| 27 | + { |
| 28 | + Dock = DockStyle.Fill |
| 29 | + }; |
| 30 | + this.Controls.Add(webView); |
| 31 | + |
| 32 | + string userDataFolder = Path.Combine(Program._Config.AppDataFolder, "WebView2Data"); |
| 33 | + var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder, new CoreWebView2EnvironmentOptions("--ignore-certificate-errors")); |
| 34 | + await webView.EnsureCoreWebView2Async(env); |
| 35 | + |
| 36 | + if (webView.CoreWebView2 == null) |
| 37 | + { |
| 38 | + Program.DebugPoint("WebView2 initialization failed."); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + // Enable JavaScript |
| 43 | + webView.CoreWebView2.Settings.IsScriptEnabled = true; |
| 44 | + |
| 45 | + // Handle navigation events |
| 46 | + webView.CoreWebView2.NavigationCompleted += WebView_NavigationCompleted; |
| 47 | + |
| 48 | + string ticketValue = Program._Api.DataTicket.ticket; |
| 49 | + |
| 50 | + var cookie = webView.CoreWebView2.CookieManager.CreateCookie( |
| 51 | + "PVEAuthCookie", |
| 52 | + ticketValue, |
| 53 | + Program._Api.DataServerInfo.server, |
| 54 | + "/" |
| 55 | + ); |
| 56 | + |
| 57 | + cookie.IsHttpOnly = false; |
| 58 | + cookie.SameSite = CoreWebView2CookieSameSiteKind.Lax; |
| 59 | + |
| 60 | + webView.CoreWebView2.CookieManager.AddOrUpdateCookie(cookie); |
| 61 | + |
| 62 | + string WebURL = $"https://{Program._Api.DataServerInfo.server}:{Program._Api.DataServerInfo.port}/#v1:0:18:4:::::::"; |
| 63 | + |
| 64 | + webView.CoreWebView2.Navigate(WebURL); |
| 65 | + |
| 66 | + } |
| 67 | + catch (Exception ex) |
| 68 | + { |
| 69 | + Program.DebugPoint(JsonConvert.SerializeObject(ex)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private void WebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e) |
| 74 | + { |
| 75 | + if (e.IsSuccess) |
| 76 | + { |
| 77 | + Program.DebugPoint("Navigation completed successfully."); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + Program.DebugPoint($"Navigation failed with error: {e.WebErrorStatus}"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + protected override void OnClosed(EventArgs e) |
| 86 | + { |
| 87 | + base.OnClosed(e); |
| 88 | + webView.Dispose(); |
| 89 | + Dispose(); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments