Form1.cs 42 KB

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