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