Browse Source

完善收藏栏

CaptB 5 months ago
parent
commit
81e6ddd08f
3 changed files with 43 additions and 50 deletions
  1. 43 38
      Form1.cs
  2. 0 9
      favoritesMenuItem.Designer.cs
  3. 0 3
      favoritesMenuItem.resx

+ 43 - 38
Form1.cs

@@ -29,6 +29,40 @@ namespace EdgeVoyager
         async void InitializeAsync()
         {
             await webView21.EnsureCoreWebView2Async(null);
+            string resxFilePath = "favoritesMenuItem.resx";
+            string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("var link = document.querySelector('link[rel~=\"icon\"]') || document.querySelector('link[rel~=\"shortcut icon\"]');link ? link.href : '';");
+            byte[] iconData = await DownloadFaviconAsync(faviconUrl);
+            using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
+            {
+                foreach (DictionaryEntry entry in reader)
+                {
+                    string resourceName = entry.Key.ToString();
+                    string resourceValue = entry.Value.ToString();
+                    ToolStripMenuItem menuItem = new ToolStripMenuItem
+                    {
+                        Text = resourceName,
+                        Tag = resourceValue
+                    };
+                    menuItem.Click += MenuItem_Click;
+                    string iconResxFilePath = "favoritesMenuItemIcon.resx";
+                    using (ResXResourceReader iconReader = new ResXResourceReader(iconResxFilePath))
+                    {
+                        foreach (DictionaryEntry iconEntry in iconReader)
+                        {
+                            if (iconEntry.Key.ToString() == resourceName)
+                            {
+                                using (MemoryStream ms = new MemoryStream(iconData))
+                                {
+                                    Image iconImage = Image.FromStream(ms);
+                                    menuItem.Image = iconImage;
+                                }
+                                break;
+                            }
+                        }
+                    }
+                    收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
+                }
+            }
             webView21.CoreWebView2.WebResourceRequested += WebView2_WebResourceRequested;
             await webView21.CoreWebView2.ExecuteScriptAsync("window.onload=function(){window.chrome.webview.postMessage('100');};");
             webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
@@ -527,32 +561,20 @@ background-position: right top;
         {
             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 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 securityLevel = "High";
             string faviconUrl = await GetFaviconUrl();
-            string encodingLanguage = "HTML, JavaScript"; // 默认值,实际需要从网页内容中解析
-
-            // 显示网页属性
+            string encodingLanguage = "HTML, JavaScript";
             MessageBox.Show(
                 $"常规信息:\n" +
                 $"标题:{title}\n" +
@@ -741,11 +763,11 @@ background-position: right top;
         private async void 添加到收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
         {
             string resxFilePath = "favoritesMenuItem.resx";
+            string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("var link = document.querySelector('link[rel~=\"icon\"]') || document.querySelector('link[rel~=\"shortcut icon\"]');link ? link.href : '';");
+            byte[] iconData = await DownloadFaviconAsync(faviconUrl);
             string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
             string pageTitle = title.Trim('"');
             string pageUrl = webView21.CoreWebView2.Source.ToString();
-            string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("var link = document.querySelector('link[rel~=\"icon\"]') || document.querySelector('link[rel~=\"shortcut icon\"]');link ? link.href : '';");
-            byte[] iconData = await DownloadFaviconAsync(faviconUrl);
             bool titleExists = false;
             using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
             {
@@ -785,8 +807,6 @@ background-position: right top;
                     writer.Generate();
                 }
             }
-
-            // 1. 清空“收藏夹AToolStripMenuItem”下的菜单中分割线下方的所有按钮
             ToolStripItem[] itemsToRemove = 收藏夹AToolStripMenuItem.DropDownItems.Cast<ToolStripItem>()
                 .SkipWhile(item => item != 收藏夹AToolStripMenuItem.DropDownItems["toolStripSeparator1"])
                 .ToArray();
@@ -795,26 +815,18 @@ background-position: right top;
             {
                 收藏夹AToolStripMenuItem.DropDownItems.Remove(item);
             }
-
-            // 2. 从 favoritesMenuItem.resx 文件中读取所有字符串资源
             using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
             {
                 foreach (DictionaryEntry entry in reader)
                 {
                     string resourceName = entry.Key.ToString();
                     string resourceValue = entry.Value.ToString();
-
-                    // 3. 为每个字符串资源创建一个新的 ToolStripMenuItem
                     ToolStripMenuItem menuItem = new ToolStripMenuItem
                     {
                         Text = resourceName,
-                        Tag = resourceValue  // 你可以将资源值存储在 Tag 属性中,方便后续使用
+                        Tag = resourceValue
                     };
-
-                    // 4. 为每个按钮添加点击事件处理程序
                     menuItem.Click += MenuItem_Click;
-
-                    // 5. 从 favoritesMenuItemIcon.resx 文件中读取对应的图标
                     string iconResxFilePath = "favoritesMenuItemIcon.resx";
                     using (ResXResourceReader iconReader = new ResXResourceReader(iconResxFilePath))
                     {
@@ -831,8 +843,6 @@ background-position: right top;
                             }
                         }
                     }
-
-                    // 6. 将新的 ToolStripMenuItem 添加到“收藏夹AToolStripMenuItem”下
                     收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
                 }
             }
@@ -847,19 +857,14 @@ background-position: right top;
                 }
                 catch (Exception ex)
                 {
-                    return null; // 返回 null 或默认图标数据
+                    return null;
                 }
             }
         }
         private void MenuItem_Click(object sender, EventArgs e)
         {
-            // 获取点击的按钮
             ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
-
-            // 获取按钮的 Tag 属性,即资源值(URL)
             string url = (string)menuItem.Tag;
-
-            // 令 WebView2 导航至该地址
             webView21.CoreWebView2.Navigate(url);
         }
     }

+ 0 - 9
favoritesMenuItem.Designer.cs

@@ -59,14 +59,5 @@ namespace EdgeVoyager {
                 resourceCulture = value;
             }
         }
-        
-        /// <summary>
-        ///   查找类似  的本地化字符串。
-        /// </summary>
-        internal static string String1 {
-            get {
-                return ResourceManager.GetString("String1", resourceCulture);
-            }
-        }
     }
 }

+ 0 - 3
favoritesMenuItem.resx

@@ -117,7 +117,4 @@
   <resheader name="writer">
     <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
   </resheader>
-  <data name="String1" xml:space="preserve">
-    <value />
-  </data>
 </root>