123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- using System.Diagnostics;
- using Microsoft.Web.WebView2.Core;
- using System;
- using System.Windows.Forms;
- using Microsoft.Web.WebView2.Core;
- using System.Text;
- using System.Net.Http;
- namespace EdgeVoyager
- {
- public partial class Form1 : Form
- {
- int zoom = 100;
- private HttpClient httpClient = new HttpClient();
- public Form1()
- {
- InitializeComponent();
- InitializeAsync();
- }
- private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
- {
- Form1 newForm = new Form1();
- newForm.webView21.Source = new Uri(e.Uri);
- newForm.Show();
- e.Handled = true;
- }
- async void InitializeAsync()
- {
- await webView21.EnsureCoreWebView2Async(null);
- webView21.CoreWebView2.WebResourceRequested += WebView2_WebResourceRequested;
- await webView21.CoreWebView2.ExecuteScriptAsync("window.onload=function(){window.chrome.webview.postMessage('100');};");
- webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
- webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
- if (Settings1.Default.firstwindow == true)
- {
- string htmlString = @"<!DOCTYPE html>
- <html lang=""zh-CN"">
- <head>
- <meta charset=""UTF-8"">
- <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
- <title>起始页</title>
- <style>
- body {
- margin: 0;
- padding: 0;
- background-image: url('https://bing.img.run/uhd.php');
- background-size: cover;
- background-repeat: no-repeat;
- background-attachment: fixed;
- font-family: Arial, sans-serif;
- }
- .header {
- background-color: rgba(255, 255, 255, 0.8);
- backdrop-filter: blur(10px);
- padding: 10px 20px;
- display: flex;
- align-items: center;
- position: relative;
- box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
- }
- .logo {
- width: auto;
- height: auto;
- }
- .nav-item {
- margin-left: 20px;
- color: #333;
- text-decoration: none;
- font-size: 16px;
- }
- .nav-links {
- display: flex;
- align-items: center;
- }
- .welcome-text {
- text-align: center;
- margin: 20px 0;
- font-size: 36px;
- font-weight: bold;
- color: #0078D4;
- }
- .search-container {
- display: flex;
- justify-content: center;
- align-items: center;
- margin-top: 20px;
- border-radius: 15px;
- overflow: hidden;
- }
- .search-input {
- width: 70vw;
- max-width: 1000px;
- padding: 10px;
- font-size: 16px;
- border: none;
- border-radius: 15px 0 0 15px;
- height: 20px;
- }
- .search-button {
- padding: 10px 20px;
- width: 80px;
- font-size: 16px;
- cursor: pointer;
- border: none;
- background-color: #0078D4;
- color: white;
- border-radius: 0 15px 15px 0;
- height: 40px;
- }
- .search-engine-select {
- margin-left: 0px;
- padding: 10px;
- font-size: 16px;
- border: none;
- border-radius: 0px;
- background-color: #f2f2f2;
- cursor: pointer;
- }
- .acrylic-panel {
- background-color: rgba(255, 255, 255, 0.8);
- backdrop-filter: blur(10px);
- padding: 20px;
- border-radius: 8px;
- box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
- margin: 20px 0;
- width: 90vw;
- margin-left: auto;
- margin-right: auto;
- max-width: 1200px;
- }
- @media (max-width: 600px) {
- .search-button {
- padding: 8px 16px;
- }
- }
- </style>
- </head>
- <body>
- <div class=""header"">
- <img src=""https://www.lykns.com.cn/wp-content/uploads/2025/01/pinwheel_48px.png"" alt=""Logo"" class=""logo"">
- <span style=""font-size: 24px; color: #333; margin-left: 0;"">LYKNS </span>
- <div class=""nav-links"">
- <a href=""https://www.lykns.com.cn"" class=""nav-item"">主站</a>
- <a href=""https://its.lykns.com.cn"" class=""nav-item"">云服务</a>
- <a href=""https://dev.lykns.com.cn"" class=""nav-item"">Git 服务</a>
- <a href=""https://survey.lykns.com.cn"" class=""nav-item"">问卷考试系统</a>
- </div>
- </div>
- <div class=""acrylic-panel"">
- <div style=""text-align:center;"">
- <?xml version=""1.0"" encoding=""utf-8""?>
- <svg xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 48 48"" width=""128"" height=""128"">
- <linearGradient id=""_EnvA6UXZqPqJP5nSvD1Ka"" x1=""12.209"" x2=""34.197"" y1=""42.462"" y2=""7.83"" gradientTransform=""matrix(1 0 0 -1 0 47.89)"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#737b80"" />
- <stop offset="".473"" stop-color=""#686f74"" />
- <stop offset=""1"" stop-color=""#575c61"" />
- </linearGradient>
- <path fill=""url(#_EnvA6UXZqPqJP5nSvD1Ka)"" d=""M44,24c0,11.044-8.956,20-20,20S4,35.044,4,24S12.956,4,24,4S44,12.956,44,24z"" />
- <linearGradient id=""_EnvA6UXZqPqJP5nSvD1Kb"" x1=""16.956"" x2=""31.044"" y1=""37.138"" y2=""10.642"" gradientTransform=""matrix(1 0 0 -1 0 47.89)"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#ccc"" />
- <stop offset="".758"" stop-color=""#f2f2f2"" />
- <stop offset=""1"" stop-color=""#fff"" />
- </linearGradient>
- <path fill=""url(#_EnvA6UXZqPqJP5nSvD1Kb)"" d=""M39,24c0,8.287-6.713,15-15,15S9,32.287,9,24S15.713,9,24,9S39,15.713,39,24z"" />
- <linearGradient id=""_EnvA6UXZqPqJP5nSvD1Kc"" x1=""19.774"" x2=""28.226"" y1=""31.838"" y2=""15.942"" gradientTransform=""matrix(1 0 0 -1 0 47.89)"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#21ad64"" />
- <stop offset=""1"" stop-color=""#088242"" />
- </linearGradient>
- <circle cx=""24"" cy=""24"" r=""9"" fill=""url(#_EnvA6UXZqPqJP5nSvD1Kc)"" />
- </svg>
- </div>
- <div class=""welcome-text"">
- 欢迎使用 LYKNS EdgeVoyager!
- </div>
- <div class=""search-container"">
- <input type=""text"" id=""search-input"" class=""search-input"" placeholder=""搜索..."">
- <select id=""search-engine-select"" class=""search-engine-select"">
- <option value=""bing"">Bing</option>
- <option value=""baidu"">百度</option>
- <option value=""google"">Google</option>
- </select>
- <button onclick=""search()"" class=""search-button"">搜索</button>
- </div>
- </div>
- <script>
- function search() {
- var query = document.getElementById('search-input').value;
- var engine = document.getElementById('search-engine-select').value;
- var url = '';
- if (engine === 'bing') {
- url = 'https://www.bing.com/search?q=' + encodeURIComponent(query);
- } else if (engine === 'baidu') {
- url = 'https://www.baidu.com/s?wd=' + encodeURIComponent(query);
- } else if (engine === 'google') {
- url = 'https://www.google.com/search?q=' + encodeURIComponent(query);
- }";
- webView21.CoreWebView2.NavigateToString(htmlString);
- Settings1.Default.firstwindow = false;
- Settings1.Default.Save();
- }
- }
- private void back_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoBack();
- }
- private void forward_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoForward();
- }
- private void refresh_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Reload();
- }
- private void stop_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Stop();
- }
- private void search_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text.Length > 0)
- {
- if (comboBox1.Text != "about:blank")
- {
- Form1 newForm = new Form1();
- newForm.webView21.Source = new Uri("https://cn.bing.com/search?q=" + textBox1.Text);
- newForm.Show();
- }
- else
- {
- webView21.CoreWebView2.Navigate("https://cn.bing.com/search?q=" + textBox1.Text);
- webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
- }
- }
- }
- private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
- {
- string currentUrl = webView21.Source.ToString();
- if (!comboBox1.Items.Contains(currentUrl))
- {
- comboBox1.Items.Add(currentUrl);
- }
- comboBox1.Text = currentUrl;
- progressBar.Value = 0;
- progressLabel.Text = "0%";
- StatusLabel.Text = "开始导航到:" + currentUrl;
- }
- private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
- {
- string currentUrl = webView21.Source.ToString();
- if (!comboBox1.Items.Contains(currentUrl))
- {
- comboBox1.Items.Add(currentUrl);
- }
- comboBox1.Text = currentUrl;
- progressBar.Value = 100;
- progressLabel.Text = "100%";
- StatusLabel.Text = "完成。";
- string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
- this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
- string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);");
- faviconUrl = faviconUrl.Trim('"');
- if (!string.IsNullOrEmpty(faviconUrl))
- {
- Uri uriResult;
- bool isAbsoluteUri = Uri.TryCreate(faviconUrl, UriKind.Absolute, out uriResult);
- if (isAbsoluteUri)
- {
- using (var client = new HttpClient())
- {
- var faviconData = await client.GetByteArrayAsync(uriResult);
- using (var ms = new MemoryStream(faviconData))
- {
- using (var img = Image.FromStream(ms))
- {
- if (img is Bitmap bitmap)
- {
- this.Icon = Icon.FromHandle(bitmap.GetHicon());
- }
- else
- {
- this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
- }
- }
- }
- }
- }
- }
- }
- private void comboBox1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- string url = comboBox1.Text;
- if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
- {
- webView21.CoreWebView2.Navigate(url);
- }
- else
- {
- MessageBox.Show("请输入有效的网址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Environment.Exit(0);
- }
- private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
- {
- progressBar.Value = 50;
- progressLabel.Text = "50%";
- StatusLabel.Text = "内容开始加载";
- }
- private async void SetZoomLevel(double zoomLevel)
- {
- await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
- }
- private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
- {
- SetZoomLevel(100);
- toolStripMenuItem3.Enabled = true;
- }
- private void toolStripMenuItem2_Click(object sender, EventArgs e)
- {
- zoom = zoom + 25;
- SetZoomLevel(zoom);
- toolStripMenuItem3.Enabled = true;
- }
- private void toolStripMenuItem3_Click(object sender, EventArgs e)
- {
- zoom = zoom - 25;
- SetZoomLevel(zoom);
- if (zoom == 25)
- toolStripMenuItem3.Enabled = false;
- }
- private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Reload();
- }
- private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Stop();
- }
- private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoForward();
- }
- private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoBack();
- }
- private void webView21_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
- {
- StatusLabel.Text = "源地址已更改";
- }
- private void WebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
- {
- string requestUri = e.Request.Uri;
- StatusLabel.Text = "请求资源:" + requestUri;
- }
- private void 工具栏ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
- {
- if (工具栏ToolStripMenuItem.Checked == true)
- {
- statusStrip1.Visible = true;
- webView21.Height = webView21.Height - 23;
- }
- else
- {
- statusStrip1.Visible = false;
- webView21.Height = webView21.Height + 23;
- }
- }
- private void 关于EdgeVoyagerToolStripMenuItem_Click(object sender, EventArgs e)
- {
- AboutBox1 aboutBox1 = new AboutBox1();
- aboutBox1.ShowDialog();
- }
- private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Settings1.Default.firstwindow = true;
- Settings1.Default.Save();
- Form1 newForm = new Form1();
- newForm.Show();
- }
- private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "网页文件 (*.html;*.htm)|*.html;*.htm|可缩放矢量图形文件 (*.svg)|*.svg|所有文件 (*.*)|*.*";
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- string filePath = openFileDialog.FileName;
- Form1 newForm = new Form1();
- newForm.webView21.Source = new Uri("file:///" + filePath);
- newForm.Show();
- }
- }
- private async void 保存SToolStripMenuItem_ClickAsync(object sender, EventArgs e)
- {
- }
- private async void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
- {
- string title = webView21.CoreWebView2.DocumentTitle;
- try
- {
- CoreWebView2PrintStatus printStatus = await webView21.CoreWebView2.PrintAsync(null);
- if (printStatus == CoreWebView2PrintStatus.Succeeded)
- {
- MessageBox.Show(this, "打印 " + title + " 文档到打印机成功", "打印到默认打印机");
- }
- else if (printStatus == CoreWebView2PrintStatus.PrinterUnavailable)
- {
- MessageBox.Show(this, "打印机不可用,离线或错误状态", "打印到默认打印机");
- }
- else
- {
- MessageBox.Show(this, "打印 " + title + " 文档到打印机失败", "打印到默认打印机");
- }
- }
- catch (Exception)
- {
- MessageBox.Show(this, "打印 " + title + " 文档已在进行中", "打印到默认打印机");
- }
- }
- private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.ShowPrintUI();
- }
- private void 发送SToolStripMenuItem_Click(object sender, EventArgs e)
- {
- string mailtoLink = $"mailto:?subject=共享网页&body=请查看这个网页:" + comboBox1.Text;
- Process.Start(new ProcessStartInfo
- {
- FileName = mailtoLink,
- UseShellExecute = true
- });
- }
- private async void 属性PToolStripMenuItem_Click(object sender, EventArgs e)
- {
- string title = webView21.CoreWebView2.DocumentTitle;
- string url = webView21.CoreWebView2.Source;
- // 获取网页的字符数和字节数
- string content = await GetWebPageContent(url);
- int charCount = content.Length;
- int byteCount = Encoding.UTF8.GetByteCount(content);
- // 获取网页的编码、语言、内容类型
- string encoding = "UTF-8"; // 默认值,实际需要从响应头中获取
- string language = "zh-CN"; // 默认值,实际需要从响应头中获取
- string contentType = "text/html"; // 默认值,实际需要从响应头中获取
- // 获取网页的元数据
- string author = await GetMetaData("author");
- string description = await GetMetaData("description");
- string keywords = await GetMetaData("keywords");
- string updateDate = await GetMetaData("last-modified");
- // 获取网页的安全信息
- string securityInfo = await GetSecurityInfo(url);
- string securityLevel = "High"; // 默认值,实际需要根据安全证书信息判断
- // 获取网页的其他信息
- string faviconUrl = await GetFaviconUrl();
- string encodingLanguage = "HTML, JavaScript"; // 默认值,实际需要从网页内容中解析
- // 显示网页属性
- MessageBox.Show(
- $"常规信息:\n" +
- $"标题:{title}\n" +
- $"地址(URL):{url}\n" +
- $"编码:{encoding}\n" +
- $"语言:{language}\n" +
- $"内容类型:{contentType}\n" +
- $"\n网页内容信息:\n" +
- $"字符数:{charCount}\n" +
- $"字节数:{byteCount}\n" +
- $"编码方式:HTML, XML\n" +
- $"\n网页元数据:\n" +
- $"作者:{author}\n" +
- $"描述:{description}\n" +
- $"关键词:{keywords}\n" +
- $"更新日期:{updateDate}\n" +
- $"\n网页安全信息:\n" +
- $"安全证书信息:{securityInfo}\n" +
- $"安全级别:{securityLevel}\n" +
- $"\n其他信息:\n" +
- $"收藏夹图标:{faviconUrl}\n" +
- $"编码语言:{encodingLanguage}",
- "网页属性",
- MessageBoxButtons.OK,
- MessageBoxIcon.Information
- );
- }
- private async Task<string> GetWebPageContent(string url)
- {
- try
- {
- HttpResponseMessage response = await httpClient.GetAsync(url);
- response.EnsureSuccessStatusCode();
- return await response.Content.ReadAsStringAsync();
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- private async Task<string> GetMetaData(string name)
- {
- try
- {
- string script = $"document.querySelector('meta[name=\"{name}\"]').content;";
- return await webView21.CoreWebView2.ExecuteScriptAsync(script);
- }
- catch (Exception)
- {
- return "不可用";
- }
- }
- private async Task<string> GetFaviconUrl()
- {
- try
- {
- string script = "Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);";
- string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync(script);
- return faviconUrl.Trim('"');
- }
- catch (Exception)
- {
- return "不可用";
- }
- }
- private async Task<string> GetSecurityInfo(string url)
- {
- try
- {
- HttpResponseMessage response = await httpClient.GetAsync(url);
- response.EnsureSuccessStatusCode();
- if (response.RequestMessage.RequestUri.Scheme == "https")
- {
- var headers = response.Headers;
- if (headers.Contains("Strict-Transport-Security"))
- {
- return "HSTS已开启";
- }
- }
- return "无HSTS";
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- }
- }
|