Form1.cs 22 KB

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