Form1.cs 22 KB

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