Form1.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. using System.Collections;
  2. using System.Diagnostics;
  3. using System.Resources;
  4. using System.Text;
  5. using Microsoft.Web.WebView2.Core;
  6. namespace EdgeVoyager
  7. {
  8. public partial class Form1 : Form
  9. {
  10. int zoom = 100;
  11. private bool isWebView2Focused = false;
  12. private HttpClient httpClient = new HttpClient();
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. InitializeAsync();
  17. }
  18. private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
  19. {
  20. Form1 newForm = new Form1();
  21. newForm.webView21.Source = new Uri(e.Uri);
  22. newForm.Show();
  23. e.Handled = true;
  24. }
  25. async void InitializeAsync()
  26. {
  27. await webView21.EnsureCoreWebView2Async(null);
  28. string resxFilePath = "favoritesMenuItem.resx";
  29. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("var link = document.querySelector('link[rel~=\"icon\"]') || document.querySelector('link[rel~=\"shortcut icon\"]');link ? link.href : '';");
  30. byte[] iconData = await DownloadFaviconAsync(faviconUrl);
  31. using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
  32. {
  33. foreach (DictionaryEntry entry in reader)
  34. {
  35. string resourceName = entry.Key.ToString();
  36. string resourceValue = entry.Value.ToString();
  37. ToolStripMenuItem menuItem = new ToolStripMenuItem
  38. {
  39. Text = resourceName,
  40. Tag = resourceValue
  41. };
  42. menuItem.Click += MenuItem_Click;
  43. string iconResxFilePath = "favoritesMenuItemIcon.resx";
  44. using (ResXResourceReader iconReader = new ResXResourceReader(iconResxFilePath))
  45. {
  46. foreach (DictionaryEntry iconEntry in iconReader)
  47. {
  48. if (iconEntry.Key.ToString() == resourceName)
  49. {
  50. using (MemoryStream ms = new MemoryStream(iconData))
  51. {
  52. Image iconImage = Image.FromStream(ms);
  53. menuItem.Image = iconImage;
  54. }
  55. break;
  56. }
  57. }
  58. }
  59. 收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
  60. }
  61. }
  62. webView21.CoreWebView2.WebResourceRequested += WebView2_WebResourceRequested;
  63. await webView21.CoreWebView2.ExecuteScriptAsync("window.onload=function(){window.chrome.webview.postMessage('100');};");
  64. webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
  65. webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
  66. webView21.GotFocus += (s, e) => isWebView2Focused = true;
  67. webView21.LostFocus += (s, e) => isWebView2Focused = false;
  68. if (Settings1.Default.firstwindow == true)
  69. {
  70. string htmlString = @"<!DOCTYPE html>
  71. <html lang=""zh-CN"">
  72. <head>
  73. <meta charset=""UTF-8"">
  74. <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
  75. <title>起始页</title>
  76. <style>
  77. body {
  78. margin: 0;
  79. padding: 0;
  80. background-image: url('https://www.beijing.gov.cn/images/index_top_ch_20190924.jpg');
  81. background-size: cover;
  82. background-repeat: no-repeat;
  83. background-position: right top;
  84. background-attachment: fixed;
  85. font-family: Arial, sans-serif;
  86. }
  87. .header {
  88. background-color: rgba(255, 255, 255, 0.8);
  89. backdrop-filter: blur(10px);
  90. padding: 10px 20px;
  91. display: flex;
  92. align-items: center;
  93. position: relative;
  94. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  95. }
  96. .logo {
  97. width: 48px;
  98. height: 48px;
  99. }
  100. .nav-item {
  101. margin-left: 20px;
  102. color: #333;
  103. text-decoration: none;
  104. font-size: 16px;
  105. }
  106. .nav-links {
  107. display: flex;
  108. align-items: center;
  109. }
  110. .welcome-text {
  111. text-align: center;
  112. margin: 20px 0;
  113. font-size: 36px;
  114. font-weight: bold;
  115. color: #0078D4;
  116. }
  117. .search-container {
  118. display: flex;
  119. justify-content: center;
  120. align-items: center;
  121. margin-top: 20px;
  122. border-radius: 15px;
  123. overflow: hidden;
  124. }
  125. .search-input {
  126. width: 70vw;
  127. max-width: 1000px;
  128. padding: 10px;
  129. font-size: 16px;
  130. border: none;
  131. border-radius: 15px 0 0 15px;
  132. height: 20px;
  133. }
  134. .search-button {
  135. padding: 10px 20px;
  136. width: 80px;
  137. font-size: 16px;
  138. cursor: pointer;
  139. border: none;
  140. background-color: #0078D4;
  141. color: white;
  142. border-radius: 0 15px 15px 0;
  143. height: 40px;
  144. }
  145. .search-engine-select {
  146. margin-left: 0px;
  147. padding: 10px;
  148. font-size: 16px;
  149. border: none;
  150. border-radius: 0px;
  151. background-color: #f2f2f2;
  152. cursor: pointer;
  153. }
  154. .acrylic-panel {
  155. background-color: rgba(255, 255, 255, 0.8);
  156. backdrop-filter: blur(10px);
  157. padding: 20px;
  158. border-radius: 8px;
  159. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  160. margin: 20px 0;
  161. width: 90vw;
  162. margin-left: auto;
  163. margin-right: auto;
  164. max-width: 1200px;
  165. }
  166. @media (max-width: 600px) {
  167. .search-button {
  168. padding: 8px 16px;
  169. }
  170. }
  171. </style>
  172. </head>
  173. <body>
  174. <div class=""header"">
  175. <svg class=""logo"" xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 48 48"">
  176. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHna"" x1=""27.702"" x2=""35.722"" y1=""18.41"" y2=""26.008"" gradientUnits=""userSpaceOnUse"">
  177. <stop offset=""0"" stop-color=""#0e5fa4""/>
  178. <stop offset=""1"" stop-color=""#1595df""/>
  179. </linearGradient>
  180. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHna)"" d=""M24,6l18,16v17.586c0,0.891-1.077,1.337-1.707,0.707L24,24V6z""/>
  181. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnb"" x1=""16.667"" x2=""22.667"" y1=""27"" y2=""27"" gradientTransform=""matrix(-3 0 0 -1 92 42)"" gradientUnits=""userSpaceOnUse"">
  182. <stop offset=""0"" stop-color=""#50e6ff""/>
  183. <stop offset=""1"" stop-color=""#32bdef""/>
  184. </linearGradient>
  185. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnb)"" d=""M24,6l18,16c-7.249-2.341-14-2-18,2V6z""/>
  186. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnc"" x1=""18.483"" x2=""27.736"" y1=""19.517"" y2=""10.264"" gradientUnits=""userSpaceOnUse"">
  187. <stop offset=""0"" stop-color=""#0e5fa4""/>
  188. <stop offset=""1"" stop-color=""#1595df""/>
  189. </linearGradient>
  190. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnc)"" d=""M6,24L22,6h17.586c0.891,0,1.337,1.077,0.707,1.707L24,24H6z""/>
  191. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnd"" x1=""3"" x2=""9"" y1=""24"" y2=""24"" gradientTransform=""matrix(0 3 -1 0 39 -3)"" gradientUnits=""userSpaceOnUse"">
  192. <stop offset=""0"" stop-color=""#50e6ff""/>
  193. <stop offset=""1"" stop-color=""#32bdef""/>
  194. </linearGradient>
  195. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnd)"" d=""M6,24L22,6c-2.341,7.249-2,14,2,18H6z""/>
  196. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHne"" x1=""19.748"" x2=""7.491"" y1=""29.748"" y2=""17.491"" gradientUnits=""userSpaceOnUse"">
  197. <stop offset=""0"" stop-color=""#0e5fa4""/>
  198. <stop offset=""1"" stop-color=""#1595df""/>
  199. </linearGradient>
  200. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHne)"" d=""M24,42L6,26V8.414c0-0.891,1.077-1.337,1.707-0.707L24,24V42z""/>
  201. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnf"" x1=""6"" x2=""12"" y1=""33"" y2=""33"" gradientTransform=""matrix(3 0 0 1 -12 0)"" gradientUnits=""userSpaceOnUse"">
  202. <stop offset=""0"" stop-color=""#50e6ff""/>
  203. <stop offset=""1"" stop-color=""#32bdef""/>
  204. </linearGradient>
  205. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnf)"" d=""M24,42L6,26c7.249,2.341,14,2,18-2V42z""/>
  206. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHng"" x1=""28.457"" x2=""19.623"" y1=""29.543"" y2=""38.377"" gradientUnits=""userSpaceOnUse"">
  207. <stop offset=""0"" stop-color=""#0e5fa4""/>
  208. <stop offset=""1"" stop-color=""#1595df""/>
  209. </linearGradient>
  210. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHng)"" d=""M24,24L7.707,40.293C7.077,40.923,7.523,42,8.414,42H26l7.529-8.471L24,24z""/>
  211. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnh"" x1=""24"" x2=""33.529"" y1=""33"" y2=""33"" gradientUnits=""userSpaceOnUse"">
  212. <stop offset=""0"" stop-color=""#50e6ff""/>
  213. <stop offset=""1"" stop-color=""#32bdef""/>
  214. </linearGradient>
  215. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnh)"" d=""M33.529,33.529L24,24c4,4,4.341,10.751,2,18L33.529,33.529z""/>
  216. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHni"" x1=""24.517"" x2=""23.183"" y1=""24.517"" y2=""23.183"" gradientUnits=""userSpaceOnUse"">
  217. <stop offset=""0"" stop-color=""#ffd000""/>
  218. <stop offset=""1"" stop-color=""#fede00""/>
  219. </linearGradient>
  220. <circle cx=""24"" cy=""24"" r=""2"" fill=""url(#Z3u55as5xQT0Xm3pZ~tHni)""/>
  221. </svg>
  222. <span style=""font-size: 24px; color: #333; margin-left: 10px;"">LYKNS</span>
  223. <div class=""nav-links"">
  224. <a href=""https://www.lykns.com.cn"" class=""nav-item"">主站</a>
  225. <a href=""https://its.lykns.com.cn"" class=""nav-item"">云服务</a>
  226. <a href=""https://dev.lykns.com.cn"" class=""nav-item"">Git 服务</a>
  227. <a href=""https://survey.lykns.com.cn"" class=""nav-item"">问卷考试系统</a>
  228. </div>
  229. </div>
  230. <div class=""acrylic-panel"">
  231. <div style=""text-align:center;"">
  232. <svg xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 48 48"" width=""128"" height=""128"">
  233. <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"">
  234. <stop offset=""0"" stop-color=""#737b80"" />
  235. <stop offset="".473"" stop-color=""#686f74"" />
  236. <stop offset=""1"" stop-color=""#575c61"" />
  237. </linearGradient>
  238. <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"" />
  239. <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"">
  240. <stop offset=""0"" stop-color=""#ccc"" />
  241. <stop offset="".758"" stop-color=""#f2f2f2"" />
  242. <stop offset=""1"" stop-color=""#fff"" />
  243. </linearGradient>
  244. <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"" />
  245. <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"">
  246. <stop offset=""0"" stop-color=""#21ad64"" />
  247. <stop offset=""1"" stop-color=""#088242"" />
  248. </linearGradient>
  249. <circle cx=""24"" cy=""24"" r=""9"" fill=""url(#_EnvA6UXZqPqJP5nSvD1Kc)"" />
  250. </svg>
  251. </div>
  252. <div class=""welcome-text"">
  253. 欢迎使用 LYKNS EdgeVoyager!
  254. </div>
  255. <div class=""search-container"">
  256. <input type=""text"" id=""search-input"" class=""search-input"" placeholder=""搜索..."">
  257. <select id=""search-engine-select"" class=""search-engine-select"">
  258. <option value=""bing"">Bing</option>
  259. <option value=""baidu"">百度</option>
  260. <option value=""google"">Google</option>
  261. </select>
  262. <button onclick=""search()"" class=""search-button"">搜索</button>
  263. </div>
  264. </div>
  265. <script>
  266. function search() {
  267. var query = document.getElementById('search-input').value;
  268. var engine = document.getElementById('search-engine-select').value;
  269. var url = '';
  270. if (engine === 'bing') {
  271. url = 'https://www.bing.com/search?q=' + encodeURIComponent(query);
  272. } else if (engine === 'baidu') {
  273. url = 'https://www.baidu.com/s?wd=' + encodeURIComponent(query);
  274. } else if (engine === 'google') {
  275. url = 'https://www.google.com/search?q=' + encodeURIComponent(query);
  276. }
  277. window.open(url, '_blank');
  278. }
  279. </script>
  280. </body>
  281. </html>";
  282. webView21.CoreWebView2.NavigateToString(htmlString);
  283. Settings1.Default.firstwindow = false;
  284. Settings1.Default.Save();
  285. }
  286. }
  287. private void back_button_Click(object sender, EventArgs e)
  288. {
  289. webView21.CoreWebView2.GoBack();
  290. }
  291. private void forward_button_Click(object sender, EventArgs e)
  292. {
  293. webView21.CoreWebView2.GoForward();
  294. }
  295. private void refresh_button_Click(object sender, EventArgs e)
  296. {
  297. webView21.CoreWebView2.Reload();
  298. }
  299. private void stop_button_Click(object sender, EventArgs e)
  300. {
  301. webView21.CoreWebView2.Stop();
  302. }
  303. private void search_button_Click(object sender, EventArgs e)
  304. {
  305. if (textBox1.Text.Length > 0)
  306. {
  307. if (comboBox1.Text != "about:blank")
  308. {
  309. Form1 newForm = new Form1();
  310. newForm.webView21.Source = new Uri("https://cn.bing.com/search?q=" + textBox1.Text);
  311. newForm.Show();
  312. }
  313. else
  314. {
  315. webView21.CoreWebView2.Navigate("https://cn.bing.com/search?q=" + textBox1.Text);
  316. webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
  317. }
  318. }
  319. }
  320. private bool isHttpsFallback = false;
  321. private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
  322. {
  323. string uri = e.Uri;
  324. if (uri.StartsWith("http://"))
  325. {
  326. string httpsUri = "https://" + uri.Substring("http://".Length);
  327. e.Cancel = true; // 取消当前的http导航
  328. webView21.CoreWebView2.Navigate(httpsUri); // 尝试https导航
  329. }
  330. string currentUrl = webView21.Source.ToString();
  331. if (!comboBox1.Items.Contains(currentUrl))
  332. {
  333. comboBox1.Items.Add(currentUrl);
  334. }
  335. comboBox1.Text = currentUrl;
  336. progressBar.Value = 0;
  337. progressLabel.Text = "0%";
  338. StatusLabel.Text = "开始导航到:" + currentUrl;
  339. }
  340. private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
  341. {
  342. if (!e.IsSuccess && isHttpsFallback)
  343. {
  344. isHttpsFallback = false; // 重置标志
  345. string originalUri = webView21.CoreWebView2.Source.Replace("https://", "http://");
  346. webView21.CoreWebView2.Navigate(originalUri); // 重新导航到http地址
  347. }
  348. else if (e.IsSuccess && webView21.CoreWebView2.Source.StartsWith("https://"))
  349. {
  350. isHttpsFallback = false; // 如果https导航成功,重置标志
  351. }
  352. string currentUrl = webView21.Source.ToString();
  353. if (!comboBox1.Items.Contains(currentUrl))
  354. {
  355. comboBox1.Items.Add(currentUrl);
  356. }
  357. comboBox1.Text = currentUrl;
  358. progressBar.Value = 100;
  359. progressLabel.Text = "100%";
  360. StatusLabel.Text = "完成。";
  361. string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
  362. this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
  363. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);");
  364. faviconUrl = faviconUrl.Trim('"');
  365. if (!string.IsNullOrEmpty(faviconUrl))
  366. {
  367. Uri uriResult;
  368. bool isAbsoluteUri = Uri.TryCreate(faviconUrl, UriKind.Absolute, out uriResult);
  369. if (isAbsoluteUri)
  370. {
  371. using (var client = new HttpClient())
  372. {
  373. var faviconData = await client.GetByteArrayAsync(uriResult);
  374. using (var ms = new MemoryStream(faviconData))
  375. {
  376. using (var img = Image.FromStream(ms))
  377. {
  378. if (img is Bitmap bitmap)
  379. {
  380. this.Icon = Icon.FromHandle(bitmap.GetHicon());
  381. }
  382. else
  383. {
  384. this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
  385. }
  386. }
  387. }
  388. }
  389. }
  390. }
  391. }
  392. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  393. {
  394. if (e.KeyCode == Keys.Enter)
  395. {
  396. string input = comboBox1.Text;
  397. if (!input.StartsWith("http://") && !input.StartsWith("https://"))
  398. {
  399. comboBox1.Text = "https://" + input;
  400. comboBox1.SelectionStart = comboBox1.Text.Length;
  401. }
  402. string url = comboBox1.Text;
  403. if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
  404. {
  405. webView21.CoreWebView2.Navigate(url);
  406. }
  407. }
  408. }
  409. private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
  410. {
  411. Environment.Exit(0);
  412. }
  413. private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
  414. {
  415. progressBar.Value = 50;
  416. progressLabel.Text = "50%";
  417. StatusLabel.Text = "内容开始加载";
  418. }
  419. private async void SetZoomLevel(double zoomLevel)
  420. {
  421. await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
  422. }
  423. private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
  424. {
  425. SetZoomLevel(100);
  426. toolStripMenuItem3.Enabled = true;
  427. }
  428. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  429. {
  430. zoom = zoom + 25;
  431. SetZoomLevel(zoom);
  432. toolStripMenuItem3.Enabled = true;
  433. }
  434. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  435. {
  436. zoom = zoom - 25;
  437. SetZoomLevel(zoom);
  438. if (zoom == 25)
  439. toolStripMenuItem3.Enabled = false;
  440. }
  441. private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
  442. {
  443. webView21.CoreWebView2.Reload();
  444. }
  445. private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
  446. {
  447. webView21.CoreWebView2.Stop();
  448. }
  449. private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
  450. {
  451. webView21.CoreWebView2.GoForward();
  452. }
  453. private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
  454. {
  455. webView21.CoreWebView2.GoBack();
  456. }
  457. private void webView21_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
  458. {
  459. StatusLabel.Text = "源地址已更改";
  460. }
  461. private void WebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
  462. {
  463. string requestUri = e.Request.Uri;
  464. StatusLabel.Text = "请求资源:" + requestUri;
  465. }
  466. private void 工具栏ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
  467. {
  468. if (工具栏ToolStripMenuItem.Checked == true)
  469. {
  470. statusStrip1.Visible = true;
  471. webView21.Height = webView21.Height - 23;
  472. }
  473. else
  474. {
  475. statusStrip1.Visible = false;
  476. webView21.Height = webView21.Height + 23;
  477. }
  478. }
  479. private void 关于EdgeVoyagerToolStripMenuItem_Click(object sender, EventArgs e)
  480. {
  481. AboutBox1 aboutBox1 = new AboutBox1();
  482. aboutBox1.ShowDialog();
  483. }
  484. private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
  485. {
  486. Settings1.Default.firstwindow = true;
  487. Settings1.Default.Save();
  488. Form1 newForm = new Form1();
  489. newForm.Show();
  490. }
  491. private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
  492. {
  493. OpenFileDialog openFileDialog = new OpenFileDialog();
  494. openFileDialog.Filter = "网页文件 (*.html;*.htm)|*.html;*.htm|可缩放矢量图形文件 (*.svg)|*.svg|所有文件 (*.*)|*.*";
  495. if (openFileDialog.ShowDialog() == DialogResult.OK)
  496. {
  497. string filePath = openFileDialog.FileName;
  498. Form1 newForm = new Form1();
  499. newForm.webView21.Source = new Uri("file:///" + filePath);
  500. newForm.Show();
  501. }
  502. }
  503. private async void 保存SToolStripMenuItem_ClickAsync(object sender, EventArgs e)
  504. {
  505. }
  506. private async void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
  507. {
  508. string title = webView21.CoreWebView2.DocumentTitle;
  509. try
  510. {
  511. CoreWebView2PrintStatus printStatus = await webView21.CoreWebView2.PrintAsync(null);
  512. if (printStatus == CoreWebView2PrintStatus.Succeeded)
  513. {
  514. MessageBox.Show(this, "打印 " + title + " 文档到打印机成功", "打印到默认打印机");
  515. }
  516. else if (printStatus == CoreWebView2PrintStatus.PrinterUnavailable)
  517. {
  518. MessageBox.Show(this, "打印机不可用,离线或错误状态", "打印到默认打印机");
  519. }
  520. else
  521. {
  522. MessageBox.Show(this, "打印 " + title + " 文档到打印机失败", "打印到默认打印机");
  523. }
  524. }
  525. catch (Exception)
  526. {
  527. MessageBox.Show(this, "打印 " + title + " 文档已在进行中", "打印到默认打印机");
  528. }
  529. }
  530. private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
  531. {
  532. webView21.CoreWebView2.ShowPrintUI();
  533. }
  534. private void 发送SToolStripMenuItem_Click(object sender, EventArgs e)
  535. {
  536. string mailtoLink = $"mailto:?subject=共享网页&body=请查看这个网页:" + comboBox1.Text;
  537. Process.Start(new ProcessStartInfo
  538. {
  539. FileName = mailtoLink,
  540. UseShellExecute = true
  541. });
  542. }
  543. private async void 属性PToolStripMenuItem_Click(object sender, EventArgs e)
  544. {
  545. string title = webView21.CoreWebView2.DocumentTitle;
  546. string url = webView21.CoreWebView2.Source;
  547. string content = await GetWebPageContent(url);
  548. int charCount = content.Length;
  549. int byteCount = Encoding.UTF8.GetByteCount(content);
  550. string encoding = "UTF-8";
  551. string language = "zh-CN";
  552. string contentType = "text/html";
  553. string author = await GetMetaData("author");
  554. string description = await GetMetaData("description");
  555. string keywords = await GetMetaData("keywords");
  556. string updateDate = await GetMetaData("last-modified");
  557. string securityInfo = await GetSecurityInfo(url);
  558. string securityLevel = "High";
  559. string faviconUrl = await GetFaviconUrl();
  560. string encodingLanguage = "HTML, JavaScript";
  561. MessageBox.Show(
  562. $"常规信息:\n" +
  563. $"标题:{title}\n" +
  564. $"地址(URL):{url}\n" +
  565. $"编码:{encoding}\n" +
  566. $"语言:{language}\n" +
  567. $"内容类型:{contentType}\n" +
  568. $"\n网页内容信息:\n" +
  569. $"字符数:{charCount}\n" +
  570. $"字节数:{byteCount}\n" +
  571. $"编码方式:HTML, XML\n" +
  572. $"\n网页元数据:\n" +
  573. $"作者:{author}\n" +
  574. $"描述:{description}\n" +
  575. $"关键词:{keywords}\n" +
  576. $"更新日期:{updateDate}\n" +
  577. $"\n网页安全信息:\n" +
  578. $"安全证书信息:{securityInfo}\n" +
  579. $"安全级别:{securityLevel}\n" +
  580. $"\n其他信息:\n" +
  581. $"收藏夹图标:{faviconUrl}\n" +
  582. $"编码语言:{encodingLanguage}",
  583. "网页属性",
  584. MessageBoxButtons.OK,
  585. MessageBoxIcon.Information
  586. );
  587. }
  588. private async Task<string> GetWebPageContent(string url)
  589. {
  590. try
  591. {
  592. HttpResponseMessage response = await httpClient.GetAsync(url);
  593. response.EnsureSuccessStatusCode();
  594. return await response.Content.ReadAsStringAsync();
  595. }
  596. catch (Exception ex)
  597. {
  598. return ex.Message;
  599. }
  600. }
  601. private async Task<string> GetMetaData(string name)
  602. {
  603. try
  604. {
  605. string script = $"document.querySelector('meta[name=\"{name}\"]').content;";
  606. return await webView21.CoreWebView2.ExecuteScriptAsync(script);
  607. }
  608. catch (Exception)
  609. {
  610. return "不可用";
  611. }
  612. }
  613. private async Task<string> GetFaviconUrl()
  614. {
  615. try
  616. {
  617. string script = "Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);";
  618. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync(script);
  619. return faviconUrl.Trim('"');
  620. }
  621. catch (Exception)
  622. {
  623. return "不可用";
  624. }
  625. }
  626. private async Task<string> GetSecurityInfo(string url)
  627. {
  628. try
  629. {
  630. HttpResponseMessage response = await httpClient.GetAsync(url);
  631. response.EnsureSuccessStatusCode();
  632. if (response.RequestMessage.RequestUri.Scheme == "https")
  633. {
  634. var headers = response.Headers;
  635. if (headers.Contains("Strict-Transport-Security"))
  636. {
  637. return "HSTS已开启";
  638. }
  639. }
  640. return "无HSTS";
  641. }
  642. catch (Exception ex)
  643. {
  644. return ex.Message;
  645. }
  646. }
  647. private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
  648. {
  649. if (comboBox1.Focused)
  650. {
  651. if (comboBox1.SelectedText != "")
  652. {
  653. Clipboard.SetText(comboBox1.SelectedText);
  654. comboBox1.Text = comboBox1.Text.Remove(comboBox1.SelectionStart, comboBox1.SelectionLength);
  655. comboBox1.Focus();
  656. }
  657. }
  658. else if (textBox1.Focused)
  659. {
  660. if (textBox1.SelectedText != "")
  661. {
  662. Clipboard.SetText(textBox1.SelectedText);
  663. textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart, textBox1.SelectionLength);
  664. }
  665. }
  666. }
  667. private async void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
  668. {
  669. if (comboBox1.Focused)
  670. {
  671. if (comboBox1.SelectedText != "")
  672. {
  673. Clipboard.SetText(comboBox1.SelectedText);
  674. }
  675. }
  676. else if (textBox1.Focused)
  677. {
  678. if (textBox1.SelectedText != "")
  679. {
  680. Clipboard.SetText(textBox1.SelectedText);
  681. }
  682. }
  683. else if (isWebView2Focused)
  684. {
  685. string script = "window.getSelection().toString();";
  686. string selectedText = await webView21.CoreWebView2.ExecuteScriptAsync(script);
  687. selectedText = selectedText.Trim('"');
  688. if (!string.IsNullOrEmpty(selectedText))
  689. {
  690. Clipboard.SetText(selectedText);
  691. }
  692. }
  693. }
  694. private async void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
  695. {
  696. if (comboBox1.Focused)
  697. {
  698. if (comboBox1.SelectionLength > 0)
  699. {
  700. string clipboardText = Clipboard.GetText();
  701. comboBox1.Text = comboBox1.Text.Remove(comboBox1.SelectionStart, comboBox1.SelectionLength) + clipboardText;
  702. comboBox1.SelectionStart = comboBox1.Text.Length;
  703. }
  704. else
  705. {
  706. string clipboardText = Clipboard.GetText();
  707. comboBox1.Text += clipboardText;
  708. comboBox1.SelectionStart = comboBox1.Text.Length;
  709. }
  710. }
  711. else if (textBox1.Focused)
  712. {
  713. textBox1.Paste();
  714. }
  715. }
  716. private async void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
  717. {
  718. if (comboBox1.Focused)
  719. {
  720. comboBox1.SelectAll();
  721. }
  722. else if (textBox1.Focused)
  723. {
  724. textBox1.SelectAll();
  725. }
  726. else
  727. {
  728. string script = "document.execCommand('selectAll', false, null);";
  729. await webView21.CoreWebView2.ExecuteScriptAsync(script);
  730. }
  731. }
  732. private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
  733. {
  734. }
  735. private async void 添加到收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
  736. {
  737. string resxFilePath = "favoritesMenuItem.resx";
  738. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("var link = document.querySelector('link[rel~=\"icon\"]') || document.querySelector('link[rel~=\"shortcut icon\"]');link ? link.href : '';");
  739. byte[] iconData = await DownloadFaviconAsync(faviconUrl);
  740. string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
  741. string pageTitle = title.Trim('"');
  742. string pageUrl = webView21.CoreWebView2.Source.ToString();
  743. bool titleExists = false;
  744. using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
  745. {
  746. foreach (DictionaryEntry entry in reader)
  747. {
  748. if (entry.Key.ToString() == pageTitle)
  749. {
  750. titleExists = true;
  751. break;
  752. }
  753. }
  754. }
  755. if (!titleExists)
  756. {
  757. using (ResXResourceWriter writer = new ResXResourceWriter(resxFilePath))
  758. {
  759. writer.AddResource(pageTitle, pageUrl);
  760. writer.Generate();
  761. }
  762. }
  763. using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
  764. {
  765. foreach (DictionaryEntry entry in reader)
  766. {
  767. if (entry.Key.ToString() == pageTitle)
  768. {
  769. titleExists = true;
  770. break;
  771. }
  772. }
  773. }
  774. if (!titleExists)
  775. {
  776. using (ResXResourceWriter writer = new ResXResourceWriter(resxFilePath))
  777. {
  778. writer.AddResource(pageTitle, iconData);
  779. writer.Generate();
  780. }
  781. }
  782. ToolStripItem[] itemsToRemove = 收藏夹AToolStripMenuItem.DropDownItems.Cast<ToolStripItem>()
  783. .SkipWhile(item => item != 收藏夹AToolStripMenuItem.DropDownItems["toolStripSeparator1"])
  784. .ToArray();
  785. foreach (var item in itemsToRemove)
  786. {
  787. 收藏夹AToolStripMenuItem.DropDownItems.Remove(item);
  788. }
  789. using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
  790. {
  791. foreach (DictionaryEntry entry in reader)
  792. {
  793. string resourceName = entry.Key.ToString();
  794. string resourceValue = entry.Value.ToString();
  795. ToolStripMenuItem menuItem = new ToolStripMenuItem
  796. {
  797. Text = resourceName,
  798. Tag = resourceValue
  799. };
  800. menuItem.Click += MenuItem_Click;
  801. string iconResxFilePath = "favoritesMenuItemIcon.resx";
  802. using (ResXResourceReader iconReader = new ResXResourceReader(iconResxFilePath))
  803. {
  804. foreach (DictionaryEntry iconEntry in iconReader)
  805. {
  806. if (iconEntry.Key.ToString() == resourceName)
  807. {
  808. using (MemoryStream ms = new MemoryStream(iconData))
  809. {
  810. Image iconImage = Image.FromStream(ms);
  811. menuItem.Image = iconImage;
  812. }
  813. break;
  814. }
  815. }
  816. }
  817. 收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
  818. }
  819. }
  820. }
  821. private async Task<byte[]> DownloadFaviconAsync(string faviconUrl)
  822. {
  823. using (HttpClient client = new HttpClient())
  824. {
  825. try
  826. {
  827. return await client.GetByteArrayAsync(faviconUrl);
  828. }
  829. catch (Exception ex)
  830. {
  831. return null;
  832. }
  833. }
  834. }
  835. private void MenuItem_Click(object sender, EventArgs e)
  836. {
  837. ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
  838. string url = (string)menuItem.Tag;
  839. webView21.CoreWebView2.Navigate(url);
  840. }
  841. private void 整理收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
  842. {
  843. Form2 form2 = new Form2();
  844. form2.ShowDialog();
  845. }
  846. private async void Form1_Enter(object sender, EventArgs e)
  847. {
  848. string resxFilePath = "favoritesMenuItem.resx";
  849. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("var link = document.querySelector('link[rel~=\"icon\"]') || document.querySelector('link[rel~=\"shortcut icon\"]');link ? link.href : '';");
  850. byte[] iconData = await DownloadFaviconAsync(faviconUrl);
  851. ToolStripItem[] itemsToRemove = 收藏夹AToolStripMenuItem.DropDownItems.Cast<ToolStripItem>()
  852. .SkipWhile(item => item != 收藏夹AToolStripMenuItem.DropDownItems["toolStripSeparator1"])
  853. .ToArray();
  854. foreach (var item in itemsToRemove)
  855. {
  856. 收藏夹AToolStripMenuItem.DropDownItems.Remove(item);
  857. }
  858. using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
  859. {
  860. foreach (DictionaryEntry entry in reader)
  861. {
  862. string resourceName = entry.Key.ToString();
  863. string resourceValue = entry.Value.ToString();
  864. ToolStripMenuItem menuItem = new ToolStripMenuItem
  865. {
  866. Text = resourceName,
  867. Tag = resourceValue
  868. };
  869. menuItem.Click += MenuItem_Click;
  870. string iconResxFilePath = "favoritesMenuItemIcon.resx";
  871. using (ResXResourceReader iconReader = new ResXResourceReader(iconResxFilePath))
  872. {
  873. foreach (DictionaryEntry iconEntry in iconReader)
  874. {
  875. if (iconEntry.Key.ToString() == resourceName)
  876. {
  877. using (MemoryStream ms = new MemoryStream(iconData))
  878. {
  879. Image iconImage = Image.FromStream(ms);
  880. menuItem.Image = iconImage;
  881. }
  882. break;
  883. }
  884. }
  885. }
  886. 收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
  887. }
  888. }
  889. }
  890. }
  891. }