Form1.cs 42 KB

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