Form1.cs 37 KB

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