Form1.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. using System.Diagnostics;
  2. using System.Text;
  3. using Microsoft.Web.WebView2.Core;
  4. using System;
  5. using System.Windows.Forms;
  6. using Microsoft.Web.WebView2.Core;
  7. using System.Text;
  8. using System.Net.Http;
  9. namespace EdgeVoyager
  10. {
  11. public partial class Form1 : Form
  12. {
  13. int zoom = 100;
  14. private HttpClient httpClient = new HttpClient();
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. InitializeAsync();
  19. }
  20. private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
  21. {
  22. Form1 newForm = new Form1();
  23. newForm.webView21.Source = new Uri(e.Uri);
  24. newForm.Show();
  25. e.Handled = true;
  26. }
  27. async void InitializeAsync()
  28. {
  29. await webView21.EnsureCoreWebView2Async(null);
  30. webView21.CoreWebView2.WebResourceRequested += WebView2_WebResourceRequested;
  31. await webView21.CoreWebView2.ExecuteScriptAsync("window.onload=function(){window.chrome.webview.postMessage('100');};");
  32. webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
  33. webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
  34. if (Settings1.Default.firstwindow == true)
  35. {
  36. string htmlString = @"<!DOCTYPE html>
  37. <html lang=""zh-CN"">
  38. <head>
  39. <meta charset=""UTF-8"">
  40. <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
  41. <title>起始页</title>
  42. <style>
  43. body {
  44. margin: 0;
  45. padding: 0;
  46. background-image: url('https://bing.img.run/uhd.php');
  47. background-size: cover;
  48. background-repeat: no-repeat;
  49. background-attachment: fixed;
  50. font-family: Arial, sans-serif;
  51. }
  52. .header {
  53. background-color: rgba(255, 255, 255, 0.8);
  54. backdrop-filter: blur(10px);
  55. padding: 10px 20px;
  56. display: flex;
  57. align-items: center;
  58. position: relative;
  59. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  60. }
  61. .logo {
  62. width: auto;
  63. height: auto;
  64. }
  65. .nav-item {
  66. margin-left: 20px;
  67. color: #333;
  68. text-decoration: none;
  69. font-size: 16px;
  70. }
  71. .nav-links {
  72. display: flex;
  73. align-items: center;
  74. }
  75. .welcome-text {
  76. text-align: center;
  77. margin: 20px 0;
  78. font-size: 36px;
  79. font-weight: bold;
  80. color: #0078D4;
  81. }
  82. .search-container {
  83. display: flex;
  84. justify-content: center;
  85. align-items: center;
  86. margin-top: 20px;
  87. border-radius: 15px;
  88. overflow: hidden;
  89. }
  90. .search-input {
  91. width: 70vw;
  92. max-width: 1000px;
  93. padding: 10px;
  94. font-size: 16px;
  95. border: none;
  96. border-radius: 15px 0 0 15px;
  97. height: 20px;
  98. }
  99. .search-button {
  100. padding: 10px 20px;
  101. width: 80px;
  102. font-size: 16px;
  103. cursor: pointer;
  104. border: none;
  105. background-color: #0078D4;
  106. color: white;
  107. border-radius: 0 15px 15px 0;
  108. height: 40px;
  109. }
  110. .search-engine-select {
  111. margin-left: 0px;
  112. padding: 10px;
  113. font-size: 16px;
  114. border: none;
  115. border-radius: 0px;
  116. background-color: #f2f2f2;
  117. cursor: pointer;
  118. }
  119. .acrylic-panel {
  120. background-color: rgba(255, 255, 255, 0.8);
  121. backdrop-filter: blur(10px);
  122. padding: 20px;
  123. border-radius: 8px;
  124. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  125. margin: 20px 0;
  126. width: 90vw;
  127. margin-left: auto;
  128. margin-right: auto;
  129. max-width: 1200px;
  130. }
  131. @media (max-width: 600px) {
  132. .search-button {
  133. padding: 8px 16px;
  134. }
  135. }
  136. </style>
  137. </head>
  138. <body>
  139. <div class=""header"">
  140. <img src=""https://www.lykns.com.cn/wp-content/uploads/2025/01/pinwheel_48px.png"" alt=""Logo"" class=""logo"">
  141. <span style=""font-size: 24px; color: #333; margin-left: 0;"">LYKNS </span>
  142. <div class=""nav-links"">
  143. <a href=""https://www.lykns.com.cn"" class=""nav-item"">主站</a>
  144. <a href=""https://its.lykns.com.cn"" class=""nav-item"">云服务</a>
  145. <a href=""https://dev.lykns.com.cn"" class=""nav-item"">Git 服务</a>
  146. <a href=""https://survey.lykns.com.cn"" class=""nav-item"">问卷考试系统</a>
  147. </div>
  148. </div>
  149. <div class=""acrylic-panel"">
  150. <div style=""text-align:center;"">
  151. <?xml version=""1.0"" encoding=""utf-8""?>
  152. <svg xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 48 48"" width=""128"" height=""128"">
  153. <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"">
  154. <stop offset=""0"" stop-color=""#737b80"" />
  155. <stop offset="".473"" stop-color=""#686f74"" />
  156. <stop offset=""1"" stop-color=""#575c61"" />
  157. </linearGradient>
  158. <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"" />
  159. <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"">
  160. <stop offset=""0"" stop-color=""#ccc"" />
  161. <stop offset="".758"" stop-color=""#f2f2f2"" />
  162. <stop offset=""1"" stop-color=""#fff"" />
  163. </linearGradient>
  164. <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"" />
  165. <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"">
  166. <stop offset=""0"" stop-color=""#21ad64"" />
  167. <stop offset=""1"" stop-color=""#088242"" />
  168. </linearGradient>
  169. <circle cx=""24"" cy=""24"" r=""9"" fill=""url(#_EnvA6UXZqPqJP5nSvD1Kc)"" />
  170. </svg>
  171. </div>
  172. <div class=""welcome-text"">
  173. 欢迎使用 LYKNS EdgeVoyager!
  174. </div>
  175. <div class=""search-container"">
  176. <input type=""text"" id=""search-input"" class=""search-input"" placeholder=""搜索..."">
  177. <select id=""search-engine-select"" class=""search-engine-select"">
  178. <option value=""bing"">Bing</option>
  179. <option value=""baidu"">百度</option>
  180. <option value=""google"">Google</option>
  181. </select>
  182. <button onclick=""search()"" class=""search-button"">搜索</button>
  183. </div>
  184. </div>
  185. <script>
  186. function search() {
  187. var query = document.getElementById('search-input').value;
  188. var engine = document.getElementById('search-engine-select').value;
  189. var url = '';
  190. if (engine === 'bing') {
  191. url = 'https://www.bing.com/search?q=' + encodeURIComponent(query);
  192. } else if (engine === 'baidu') {
  193. url = 'https://www.baidu.com/s?wd=' + encodeURIComponent(query);
  194. } else if (engine === 'google') {
  195. url = 'https://www.google.com/search?q=' + encodeURIComponent(query);
  196. }";
  197. webView21.CoreWebView2.NavigateToString(htmlString);
  198. Settings1.Default.firstwindow = false;
  199. Settings1.Default.Save();
  200. }
  201. }
  202. private void back_button_Click(object sender, EventArgs e)
  203. {
  204. webView21.CoreWebView2.GoBack();
  205. }
  206. private void forward_button_Click(object sender, EventArgs e)
  207. {
  208. webView21.CoreWebView2.GoForward();
  209. }
  210. private void refresh_button_Click(object sender, EventArgs e)
  211. {
  212. webView21.CoreWebView2.Reload();
  213. }
  214. private void stop_button_Click(object sender, EventArgs e)
  215. {
  216. webView21.CoreWebView2.Stop();
  217. }
  218. private void search_button_Click(object sender, EventArgs e)
  219. {
  220. if (textBox1.Text.Length > 0)
  221. {
  222. if (comboBox1.Text != "about:blank")
  223. {
  224. Form1 newForm = new Form1();
  225. newForm.webView21.Source = new Uri("https://cn.bing.com/search?q=" + textBox1.Text);
  226. newForm.Show();
  227. }
  228. else
  229. {
  230. webView21.CoreWebView2.Navigate("https://cn.bing.com/search?q=" + textBox1.Text);
  231. webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
  232. }
  233. }
  234. }
  235. private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
  236. {
  237. string currentUrl = webView21.Source.ToString();
  238. if (!comboBox1.Items.Contains(currentUrl))
  239. {
  240. comboBox1.Items.Add(currentUrl);
  241. }
  242. comboBox1.Text = currentUrl;
  243. progressBar.Value = 0;
  244. progressLabel.Text = "0%";
  245. StatusLabel.Text = "开始导航到:" + currentUrl;
  246. }
  247. private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
  248. {
  249. string currentUrl = webView21.Source.ToString();
  250. if (!comboBox1.Items.Contains(currentUrl))
  251. {
  252. comboBox1.Items.Add(currentUrl);
  253. }
  254. comboBox1.Text = currentUrl;
  255. progressBar.Value = 100;
  256. progressLabel.Text = "100%";
  257. StatusLabel.Text = "完成。";
  258. string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
  259. this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
  260. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);");
  261. faviconUrl = faviconUrl.Trim('"');
  262. if (!string.IsNullOrEmpty(faviconUrl))
  263. {
  264. Uri uriResult;
  265. bool isAbsoluteUri = Uri.TryCreate(faviconUrl, UriKind.Absolute, out uriResult);
  266. if (isAbsoluteUri)
  267. {
  268. using (var client = new HttpClient())
  269. {
  270. var faviconData = await client.GetByteArrayAsync(uriResult);
  271. using (var ms = new MemoryStream(faviconData))
  272. {
  273. using (var img = Image.FromStream(ms))
  274. {
  275. if (img is Bitmap bitmap)
  276. {
  277. this.Icon = Icon.FromHandle(bitmap.GetHicon());
  278. }
  279. else
  280. {
  281. this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. }
  289. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  290. {
  291. if (e.KeyCode == Keys.Enter)
  292. {
  293. string url = comboBox1.Text;
  294. if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
  295. {
  296. webView21.CoreWebView2.Navigate(url);
  297. }
  298. else
  299. {
  300. MessageBox.Show("请输入有效的网址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  301. }
  302. }
  303. }
  304. private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
  305. {
  306. Environment.Exit(0);
  307. }
  308. private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
  309. {
  310. progressBar.Value = 50;
  311. progressLabel.Text = "50%";
  312. StatusLabel.Text = "内容开始加载";
  313. }
  314. private async void SetZoomLevel(double zoomLevel)
  315. {
  316. await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
  317. }
  318. private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
  319. {
  320. SetZoomLevel(100);
  321. toolStripMenuItem3.Enabled = true;
  322. }
  323. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  324. {
  325. zoom = zoom + 25;
  326. SetZoomLevel(zoom);
  327. toolStripMenuItem3.Enabled = true;
  328. }
  329. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  330. {
  331. zoom = zoom - 25;
  332. SetZoomLevel(zoom);
  333. if (zoom == 25)
  334. toolStripMenuItem3.Enabled = false;
  335. }
  336. private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
  337. {
  338. webView21.CoreWebView2.Reload();
  339. }
  340. private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
  341. {
  342. webView21.CoreWebView2.Stop();
  343. }
  344. private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
  345. {
  346. webView21.CoreWebView2.GoForward();
  347. }
  348. private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
  349. {
  350. webView21.CoreWebView2.GoBack();
  351. }
  352. private void webView21_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
  353. {
  354. StatusLabel.Text = "源地址已更改";
  355. }
  356. private void WebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
  357. {
  358. string requestUri = e.Request.Uri;
  359. StatusLabel.Text = "请求资源:" + requestUri;
  360. }
  361. private void 工具栏ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
  362. {
  363. if (工具栏ToolStripMenuItem.Checked == true)
  364. {
  365. statusStrip1.Visible = true;
  366. webView21.Height = webView21.Height - 23;
  367. }
  368. else
  369. {
  370. statusStrip1.Visible = false;
  371. webView21.Height = webView21.Height + 23;
  372. }
  373. }
  374. private void 关于EdgeVoyagerToolStripMenuItem_Click(object sender, EventArgs e)
  375. {
  376. AboutBox1 aboutBox1 = new AboutBox1();
  377. aboutBox1.ShowDialog();
  378. }
  379. private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
  380. {
  381. Settings1.Default.firstwindow = true;
  382. Settings1.Default.Save();
  383. Form1 newForm = new Form1();
  384. newForm.Show();
  385. }
  386. private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
  387. {
  388. OpenFileDialog openFileDialog = new OpenFileDialog();
  389. openFileDialog.Filter = "网页文件 (*.html;*.htm)|*.html;*.htm|可缩放矢量图形文件 (*.svg)|*.svg|所有文件 (*.*)|*.*";
  390. if (openFileDialog.ShowDialog() == DialogResult.OK)
  391. {
  392. string filePath = openFileDialog.FileName;
  393. Form1 newForm = new Form1();
  394. newForm.webView21.Source = new Uri("file:///" + filePath);
  395. newForm.Show();
  396. }
  397. }
  398. private async void 保存SToolStripMenuItem_ClickAsync(object sender, EventArgs e)
  399. {
  400. }
  401. private async void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
  402. {
  403. string title = webView21.CoreWebView2.DocumentTitle;
  404. try
  405. {
  406. CoreWebView2PrintStatus printStatus = await webView21.CoreWebView2.PrintAsync(null);
  407. if (printStatus == CoreWebView2PrintStatus.Succeeded)
  408. {
  409. MessageBox.Show(this, "打印 " + title + " 文档到打印机成功", "打印到默认打印机");
  410. }
  411. else if (printStatus == CoreWebView2PrintStatus.PrinterUnavailable)
  412. {
  413. MessageBox.Show(this, "打印机不可用,离线或错误状态", "打印到默认打印机");
  414. }
  415. else
  416. {
  417. MessageBox.Show(this, "打印 " + title + " 文档到打印机失败", "打印到默认打印机");
  418. }
  419. }
  420. catch (Exception)
  421. {
  422. MessageBox.Show(this, "打印 " + title + " 文档已在进行中", "打印到默认打印机");
  423. }
  424. }
  425. private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
  426. {
  427. webView21.CoreWebView2.ShowPrintUI();
  428. }
  429. private void 发送SToolStripMenuItem_Click(object sender, EventArgs e)
  430. {
  431. string mailtoLink = $"mailto:?subject=共享网页&body=请查看这个网页:" + comboBox1.Text;
  432. Process.Start(new ProcessStartInfo
  433. {
  434. FileName = mailtoLink,
  435. UseShellExecute = true
  436. });
  437. }
  438. private async void 属性PToolStripMenuItem_Click(object sender, EventArgs e)
  439. {
  440. string title = webView21.CoreWebView2.DocumentTitle;
  441. string url = webView21.CoreWebView2.Source;
  442. // 获取网页的字符数和字节数
  443. string content = await GetWebPageContent(url);
  444. int charCount = content.Length;
  445. int byteCount = Encoding.UTF8.GetByteCount(content);
  446. // 获取网页的编码、语言、内容类型
  447. string encoding = "UTF-8"; // 默认值,实际需要从响应头中获取
  448. string language = "zh-CN"; // 默认值,实际需要从响应头中获取
  449. string contentType = "text/html"; // 默认值,实际需要从响应头中获取
  450. // 获取网页的元数据
  451. string author = await GetMetaData("author");
  452. string description = await GetMetaData("description");
  453. string keywords = await GetMetaData("keywords");
  454. string updateDate = await GetMetaData("last-modified");
  455. // 获取网页的安全信息
  456. string securityInfo = await GetSecurityInfo(url);
  457. string securityLevel = "High"; // 默认值,实际需要根据安全证书信息判断
  458. // 获取网页的其他信息
  459. string faviconUrl = await GetFaviconUrl();
  460. string encodingLanguage = "HTML, JavaScript"; // 默认值,实际需要从网页内容中解析
  461. // 显示网页属性
  462. MessageBox.Show(
  463. $"常规信息:\n" +
  464. $"标题:{title}\n" +
  465. $"地址(URL):{url}\n" +
  466. $"编码:{encoding}\n" +
  467. $"语言:{language}\n" +
  468. $"内容类型:{contentType}\n" +
  469. $"\n网页内容信息:\n" +
  470. $"字符数:{charCount}\n" +
  471. $"字节数:{byteCount}\n" +
  472. $"编码方式:HTML, XML\n" +
  473. $"\n网页元数据:\n" +
  474. $"作者:{author}\n" +
  475. $"描述:{description}\n" +
  476. $"关键词:{keywords}\n" +
  477. $"更新日期:{updateDate}\n" +
  478. $"\n网页安全信息:\n" +
  479. $"安全证书信息:{securityInfo}\n" +
  480. $"安全级别:{securityLevel}\n" +
  481. $"\n其他信息:\n" +
  482. $"收藏夹图标:{faviconUrl}\n" +
  483. $"编码语言:{encodingLanguage}",
  484. "网页属性",
  485. MessageBoxButtons.OK,
  486. MessageBoxIcon.Information
  487. );
  488. }
  489. private async Task<string> GetWebPageContent(string url)
  490. {
  491. try
  492. {
  493. HttpResponseMessage response = await httpClient.GetAsync(url);
  494. response.EnsureSuccessStatusCode();
  495. return await response.Content.ReadAsStringAsync();
  496. }
  497. catch (Exception ex)
  498. {
  499. return ex.Message;
  500. }
  501. }
  502. private async Task<string> GetMetaData(string name)
  503. {
  504. try
  505. {
  506. string script = $"document.querySelector('meta[name=\"{name}\"]').content;";
  507. return await webView21.CoreWebView2.ExecuteScriptAsync(script);
  508. }
  509. catch (Exception)
  510. {
  511. return "不可用";
  512. }
  513. }
  514. private async Task<string> GetFaviconUrl()
  515. {
  516. try
  517. {
  518. string script = "Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);";
  519. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync(script);
  520. return faviconUrl.Trim('"');
  521. }
  522. catch (Exception)
  523. {
  524. return "不可用";
  525. }
  526. }
  527. private async Task<string> GetSecurityInfo(string url)
  528. {
  529. try
  530. {
  531. HttpResponseMessage response = await httpClient.GetAsync(url);
  532. response.EnsureSuccessStatusCode();
  533. if (response.RequestMessage.RequestUri.Scheme == "https")
  534. {
  535. var headers = response.Headers;
  536. if (headers.Contains("Strict-Transport-Security"))
  537. {
  538. return "HSTS已开启";
  539. }
  540. }
  541. return "无HSTS";
  542. }
  543. catch (Exception ex)
  544. {
  545. return ex.Message;
  546. }
  547. }
  548. }
  549. }