Browse Source

完成页面属性显示

CaptB 4 months ago
parent
commit
c477a74199
2 changed files with 129 additions and 1 deletions
  1. 1 0
      Form1.Designer.cs
  2. 128 1
      Form1.cs

+ 1 - 0
Form1.Designer.cs

@@ -322,6 +322,7 @@
             属性PToolStripMenuItem.Name = "属性PToolStripMenuItem";
             属性PToolStripMenuItem.Size = new Size(180, 22);
             属性PToolStripMenuItem.Text = "属性(&P)";
+            属性PToolStripMenuItem.Click += 属性PToolStripMenuItem_Click;
             // 
             // toolStripSeparator2
             // 

+ 128 - 1
Form1.cs

@@ -1,11 +1,17 @@
 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;
+        int zoom = 100; 
+        private HttpClient httpClient = new HttpClient();
         public Form1()
         {
             InitializeComponent();
@@ -463,5 +469,126 @@ namespace EdgeVoyager
                 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;
+            }
+        }
     }
 }