Form1.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. var faviconData = await client.GetByteArrayAsync(uriResult);
  357. using (var ms = new MemoryStream(faviconData))
  358. {
  359. using (var img = Image.FromStream(ms))
  360. {
  361. if (img is Bitmap bitmap)
  362. {
  363. this.Icon = Icon.FromHandle(bitmap.GetHicon());
  364. }
  365. else
  366. {
  367. this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
  368. }
  369. }
  370. }
  371. }
  372. }
  373. }
  374. }
  375. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  376. {
  377. if (e.KeyCode == Keys.Enter)
  378. {
  379. string input = comboBox1.Text;
  380. if (!input.StartsWith("http://") && !input.StartsWith("https://"))
  381. {
  382. comboBox1.Text = "https://" + input;
  383. comboBox1.SelectionStart = comboBox1.Text.Length;
  384. }
  385. string url = comboBox1.Text;
  386. if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
  387. {
  388. webView21.CoreWebView2.Navigate(url);
  389. }
  390. }
  391. }
  392. private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
  393. {
  394. Environment.Exit(0);
  395. }
  396. private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
  397. {
  398. progressBar.Value = 50;
  399. progressLabel.Text = "50%";
  400. StatusLabel.Text = "内容开始加载";
  401. }
  402. private async void SetZoomLevel(double zoomLevel)
  403. {
  404. await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
  405. }
  406. private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
  407. {
  408. SetZoomLevel(100);
  409. toolStripMenuItem3.Enabled = true;
  410. }
  411. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  412. {
  413. zoom = zoom + 25;
  414. SetZoomLevel(zoom);
  415. toolStripMenuItem3.Enabled = true;
  416. }
  417. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  418. {
  419. zoom = zoom - 25;
  420. SetZoomLevel(zoom);
  421. if (zoom == 25)
  422. toolStripMenuItem3.Enabled = false;
  423. }
  424. private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
  425. {
  426. webView21.CoreWebView2.Reload();
  427. }
  428. private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
  429. {
  430. webView21.CoreWebView2.Stop();
  431. }
  432. private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
  433. {
  434. webView21.CoreWebView2.GoForward();
  435. }
  436. private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
  437. {
  438. webView21.CoreWebView2.GoBack();
  439. }
  440. private void webView21_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
  441. {
  442. StatusLabel.Text = "源地址已更改";
  443. }
  444. private void WebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
  445. {
  446. string requestUri = e.Request.Uri;
  447. StatusLabel.Text = "请求资源:" + requestUri;
  448. }
  449. private void 工具栏ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
  450. {
  451. if (工具栏ToolStripMenuItem.Checked == true)
  452. {
  453. statusStrip1.Visible = true;
  454. webView21.Height = webView21.Height - 23;
  455. }
  456. else
  457. {
  458. statusStrip1.Visible = false;
  459. webView21.Height = webView21.Height + 23;
  460. }
  461. }
  462. private void 关于EdgeVoyagerToolStripMenuItem_Click(object sender, EventArgs e)
  463. {
  464. AboutBox1 aboutBox1 = new AboutBox1();
  465. aboutBox1.ShowDialog();
  466. }
  467. private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
  468. {
  469. Settings1.Default.firstwindow = true;
  470. Settings1.Default.Save();
  471. Form1 newForm = new Form1();
  472. newForm.Show();
  473. }
  474. private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
  475. {
  476. OpenFileDialog openFileDialog = new OpenFileDialog();
  477. openFileDialog.Filter = "网页文件 (*.html;*.htm)|*.html;*.htm|可缩放矢量图形文件 (*.svg)|*.svg|所有文件 (*.*)|*.*";
  478. if (openFileDialog.ShowDialog() == DialogResult.OK)
  479. {
  480. string filePath = openFileDialog.FileName;
  481. Form1 newForm = new Form1();
  482. newForm.webView21.Source = new Uri("file:///" + filePath);
  483. newForm.Show();
  484. }
  485. }
  486. private async void 保存SToolStripMenuItem_ClickAsync(object sender, EventArgs e)
  487. {
  488. }
  489. private async void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
  490. {
  491. string title = webView21.CoreWebView2.DocumentTitle;
  492. try
  493. {
  494. CoreWebView2PrintStatus printStatus = await webView21.CoreWebView2.PrintAsync(null);
  495. if (printStatus == CoreWebView2PrintStatus.Succeeded)
  496. {
  497. MessageBox.Show(this, "打印 " + title + " 文档到打印机成功", "打印到默认打印机");
  498. }
  499. else if (printStatus == CoreWebView2PrintStatus.PrinterUnavailable)
  500. {
  501. MessageBox.Show(this, "打印机不可用,离线或错误状态", "打印到默认打印机");
  502. }
  503. else
  504. {
  505. MessageBox.Show(this, "打印 " + title + " 文档到打印机失败", "打印到默认打印机");
  506. }
  507. }
  508. catch (Exception)
  509. {
  510. MessageBox.Show(this, "打印 " + title + " 文档已在进行中", "打印到默认打印机");
  511. }
  512. }
  513. private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
  514. {
  515. webView21.CoreWebView2.ShowPrintUI();
  516. }
  517. private void 发送SToolStripMenuItem_Click(object sender, EventArgs e)
  518. {
  519. string mailtoLink = $"mailto:?subject=共享网页&body=请查看这个网页:" + comboBox1.Text;
  520. Process.Start(new ProcessStartInfo
  521. {
  522. FileName = mailtoLink,
  523. UseShellExecute = true
  524. });
  525. }
  526. private async void 属性PToolStripMenuItem_Click(object sender, EventArgs e)
  527. {
  528. string title = webView21.CoreWebView2.DocumentTitle;
  529. string url = webView21.CoreWebView2.Source;
  530. string content = await GetWebPageContent(url);
  531. int charCount = content.Length;
  532. int byteCount = Encoding.UTF8.GetByteCount(content);
  533. string encoding = "UTF-8";
  534. string language = "zh-CN";
  535. string contentType = "text/html";
  536. string author = await GetMetaData("author");
  537. string description = await GetMetaData("description");
  538. string keywords = await GetMetaData("keywords");
  539. string updateDate = await GetMetaData("last-modified");
  540. string securityInfo = await GetSecurityInfo(url);
  541. string securityLevel = "High";
  542. string faviconUrl = await GetFaviconUrl();
  543. string encodingLanguage = "HTML, JavaScript";
  544. MessageBox.Show(
  545. $"常规信息:\n" +
  546. $"标题:{title}\n" +
  547. $"地址(URL):{url}\n" +
  548. $"编码:{encoding}\n" +
  549. $"语言:{language}\n" +
  550. $"内容类型:{contentType}\n" +
  551. $"\n网页内容信息:\n" +
  552. $"字符数:{charCount}\n" +
  553. $"字节数:{byteCount}\n" +
  554. $"编码方式:HTML, XML\n" +
  555. $"\n网页元数据:\n" +
  556. $"作者:{author}\n" +
  557. $"描述:{description}\n" +
  558. $"关键词:{keywords}\n" +
  559. $"更新日期:{updateDate}\n" +
  560. $"\n网页安全信息:\n" +
  561. $"安全证书信息:{securityInfo}\n" +
  562. $"安全级别:{securityLevel}\n" +
  563. $"\n其他信息:\n" +
  564. $"收藏夹图标:{faviconUrl}\n" +
  565. $"编码语言:{encodingLanguage}",
  566. "网页属性",
  567. MessageBoxButtons.OK,
  568. MessageBoxIcon.Information
  569. );
  570. }
  571. private async Task<string> GetWebPageContent(string url)
  572. {
  573. try
  574. {
  575. HttpResponseMessage response = await httpClient.GetAsync(url);
  576. response.EnsureSuccessStatusCode();
  577. return await response.Content.ReadAsStringAsync();
  578. }
  579. catch (Exception ex)
  580. {
  581. return ex.Message;
  582. }
  583. }
  584. private async Task<string> GetMetaData(string name)
  585. {
  586. try
  587. {
  588. string script = $"document.querySelector('meta[name=\"{name}\"]').content;";
  589. return await webView21.CoreWebView2.ExecuteScriptAsync(script);
  590. }
  591. catch (Exception)
  592. {
  593. return "不可用";
  594. }
  595. }
  596. private async Task<string> GetFaviconUrl()
  597. {
  598. try
  599. {
  600. string script = "Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);";
  601. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync(script);
  602. return faviconUrl.Trim('"');
  603. }
  604. catch (Exception)
  605. {
  606. return "不可用";
  607. }
  608. }
  609. private async Task<string> GetSecurityInfo(string url)
  610. {
  611. try
  612. {
  613. HttpResponseMessage response = await httpClient.GetAsync(url);
  614. response.EnsureSuccessStatusCode();
  615. if (response.RequestMessage.RequestUri.Scheme == "https")
  616. {
  617. var headers = response.Headers;
  618. if (headers.Contains("Strict-Transport-Security"))
  619. {
  620. return "HSTS已开启";
  621. }
  622. }
  623. return "无HSTS";
  624. }
  625. catch (Exception ex)
  626. {
  627. return ex.Message;
  628. }
  629. }
  630. private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
  631. {
  632. if (comboBox1.Focused)
  633. {
  634. if (comboBox1.SelectedText != "")
  635. {
  636. Clipboard.SetText(comboBox1.SelectedText);
  637. comboBox1.Text = comboBox1.Text.Remove(comboBox1.SelectionStart, comboBox1.SelectionLength);
  638. comboBox1.Focus();
  639. }
  640. }
  641. else if (textBox1.Focused)
  642. {
  643. if (textBox1.SelectedText != "")
  644. {
  645. Clipboard.SetText(textBox1.SelectedText);
  646. textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart, textBox1.SelectionLength);
  647. }
  648. }
  649. }
  650. private async void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
  651. {
  652. if (comboBox1.Focused)
  653. {
  654. if (comboBox1.SelectedText != "")
  655. {
  656. Clipboard.SetText(comboBox1.SelectedText);
  657. }
  658. }
  659. else if (textBox1.Focused)
  660. {
  661. if (textBox1.SelectedText != "")
  662. {
  663. Clipboard.SetText(textBox1.SelectedText);
  664. }
  665. }
  666. else if (isWebView2Focused)
  667. {
  668. string script = "window.getSelection().toString();";
  669. string selectedText = await webView21.CoreWebView2.ExecuteScriptAsync(script);
  670. selectedText = selectedText.Trim('"');
  671. if (!string.IsNullOrEmpty(selectedText))
  672. {
  673. Clipboard.SetText(selectedText);
  674. }
  675. }
  676. }
  677. private async void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
  678. {
  679. if (comboBox1.Focused)
  680. {
  681. if (comboBox1.SelectionLength > 0)
  682. {
  683. string clipboardText = Clipboard.GetText();
  684. comboBox1.Text = comboBox1.Text.Remove(comboBox1.SelectionStart, comboBox1.SelectionLength) + clipboardText;
  685. comboBox1.SelectionStart = comboBox1.Text.Length;
  686. }
  687. else
  688. {
  689. string clipboardText = Clipboard.GetText();
  690. comboBox1.Text += clipboardText;
  691. comboBox1.SelectionStart = comboBox1.Text.Length;
  692. }
  693. }
  694. else if (textBox1.Focused)
  695. {
  696. textBox1.Paste();
  697. }
  698. }
  699. private async void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
  700. {
  701. if (comboBox1.Focused)
  702. {
  703. comboBox1.SelectAll();
  704. }
  705. else if (textBox1.Focused)
  706. {
  707. textBox1.SelectAll();
  708. }
  709. else
  710. {
  711. string script = "document.execCommand('selectAll', false, null);";
  712. await webView21.CoreWebView2.ExecuteScriptAsync(script);
  713. }
  714. }
  715. private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
  716. {
  717. }
  718. private async void 添加到收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
  719. {
  720. string resxFilePath = "favoritesMenuItem.resx";
  721. string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
  722. string pageTitle = title.Trim('"');
  723. string pageUrl = webView21.CoreWebView2.Source.ToString();
  724. bool titleExists = false;
  725. using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
  726. {
  727. foreach (DictionaryEntry entry in reader)
  728. {
  729. if (entry.Key.ToString() == pageTitle)
  730. {
  731. titleExists = true;
  732. break;
  733. }
  734. }
  735. }
  736. if (!titleExists)
  737. {
  738. ResXResourceReader reader = new ResXResourceReader(resxFilePath);
  739. Hashtable resources = new Hashtable();
  740. foreach (DictionaryEntry entry in reader)
  741. {
  742. resources.Add(entry.Key, entry.Value);
  743. }
  744. reader.Close();
  745. // 添加新的资源项
  746. resources.Add(pageTitle, pageUrl);
  747. // 写入到资源文件
  748. using (ResXResourceWriter writer = new ResXResourceWriter(resxFilePath))
  749. {
  750. foreach (DictionaryEntry entry in resources)
  751. {
  752. writer.AddResource(entry.Key.ToString(), entry.Value);
  753. }
  754. writer.Generate();
  755. }
  756. for (int i = 0; i < 收藏夹AToolStripMenuItem.DropDownItems.Count; i++)
  757. {
  758. // 如果找到了toolStripSeparator10
  759. if (收藏夹AToolStripMenuItem.DropDownItems[i] is ToolStripSeparator &&
  760. 收藏夹AToolStripMenuItem.DropDownItems[i].Name == "toolStripSeparator10")
  761. {
  762. // 从toolStripSeparator10下方开始,删除所有按钮
  763. for (int j = 收藏夹AToolStripMenuItem.DropDownItems.Count - 1; j > i; j--)
  764. {
  765. 收藏夹AToolStripMenuItem.DropDownItems.RemoveAt(j);
  766. }
  767. break; // 找到并处理完毕后退出循环
  768. }
  769. }
  770. using (ResXResourceReader reader1 = new ResXResourceReader(resxFilePath))
  771. {
  772. foreach (DictionaryEntry entry in reader1)
  773. {
  774. string resourceName = entry.Key.ToString();
  775. string resourceValue = entry.Value.ToString();
  776. ToolStripMenuItem menuItem = new ToolStripMenuItem
  777. {
  778. Text = resourceName,
  779. Tag = resourceValue
  780. };
  781. menuItem.Click += MenuItem_Click;
  782. 收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
  783. }
  784. }
  785. }
  786. }
  787. private async Task<byte[]> DownloadFaviconAsync(string faviconUrl)
  788. {
  789. using (HttpClient client = new HttpClient())
  790. {
  791. try
  792. {
  793. return await client.GetByteArrayAsync(faviconUrl);
  794. }
  795. catch (Exception ex)
  796. {
  797. return null;
  798. }
  799. }
  800. }
  801. private void MenuItem_Click(object sender, EventArgs e)
  802. {
  803. ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
  804. string url = (string)menuItem.Tag;
  805. webView21.CoreWebView2.Navigate(url);
  806. }
  807. private void 整理收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
  808. {
  809. Form2 form2 = new Form2();
  810. form2.ShowDialog();
  811. string resxFilePath = "favoritesMenuItem.resx";
  812. for (int i = 0; i < 收藏夹AToolStripMenuItem.DropDownItems.Count; i++)
  813. {
  814. // 如果找到了toolStripSeparator10
  815. if (收藏夹AToolStripMenuItem.DropDownItems[i] is ToolStripSeparator &&
  816. 收藏夹AToolStripMenuItem.DropDownItems[i].Name == "toolStripSeparator10")
  817. {
  818. // 从toolStripSeparator10下方开始,删除所有按钮
  819. for (int j = 收藏夹AToolStripMenuItem.DropDownItems.Count - 1; j > i; j--)
  820. {
  821. 收藏夹AToolStripMenuItem.DropDownItems.RemoveAt(j);
  822. }
  823. break; // 找到并处理完毕后退出循环
  824. }
  825. }
  826. using (ResXResourceReader reader1 = new ResXResourceReader(resxFilePath))
  827. {
  828. foreach (DictionaryEntry entry in reader1)
  829. {
  830. string resourceName = entry.Key.ToString();
  831. string resourceValue = entry.Value.ToString();
  832. ToolStripMenuItem menuItem = new ToolStripMenuItem
  833. {
  834. Text = resourceName,
  835. Tag = resourceValue
  836. };
  837. menuItem.Click += MenuItem_Click;
  838. 收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
  839. }
  840. }
  841. }
  842. private void Form1_Activated(object sender, EventArgs e)
  843. {
  844. string resxFilePath = "favoritesMenuItem.resx";
  845. for (int i = 0; i < 收藏夹AToolStripMenuItem.DropDownItems.Count; i++)
  846. {
  847. // 如果找到了toolStripSeparator10
  848. if (收藏夹AToolStripMenuItem.DropDownItems[i] is ToolStripSeparator &&
  849. 收藏夹AToolStripMenuItem.DropDownItems[i].Name == "toolStripSeparator10")
  850. {
  851. // 从toolStripSeparator10下方开始,删除所有按钮
  852. for (int j = 收藏夹AToolStripMenuItem.DropDownItems.Count - 1; j > i; j--)
  853. {
  854. 收藏夹AToolStripMenuItem.DropDownItems.RemoveAt(j);
  855. }
  856. break; // 找到并处理完毕后退出循环
  857. }
  858. }
  859. using (ResXResourceReader reader1 = new ResXResourceReader(resxFilePath))
  860. {
  861. foreach (DictionaryEntry entry in reader1)
  862. {
  863. string resourceName = entry.Key.ToString();
  864. string resourceValue = entry.Value.ToString();
  865. ToolStripMenuItem menuItem = new ToolStripMenuItem
  866. {
  867. Text = resourceName,
  868. Tag = resourceValue
  869. };
  870. menuItem.Click += MenuItem_Click;
  871. 收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
  872. }
  873. }
  874. }
  875. }
  876. }