123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974 |
- using System.Collections;
- using System.Diagnostics;
- using System.Resources;
- using System.Text;
- using Microsoft.Web.WebView2.Core;
- namespace EdgeVoyager
- {
- public partial class Form1 : Form
- {
- int zoom = 100;
- private bool isWebView2Focused = false;
- private HttpClient httpClient = new HttpClient();
- public Form1()
- {
- InitializeComponent();
- InitializeAsync();
- string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- string filePath = Path.Combine(docPath, @"EdgeVoyager\favoritesMenuItem.resx");
- string edgeVoyagerPath = Path.Combine(docPath, "EdgeVoyager");
- if (!Directory.Exists(edgeVoyagerPath))
- Directory.CreateDirectory(edgeVoyagerPath);
- if (!File.Exists(filePath))
- {
- using (StreamWriter sw = File.CreateText(filePath))
- {
- sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
- sw.WriteLine("<root>");
- sw.WriteLine(" <!-- ");
- sw.WriteLine(" Microsoft ResX Schema ");
- sw.WriteLine(" ");
- sw.WriteLine(" Version 2.0");
- sw.WriteLine(" ");
- sw.WriteLine(" The primary goals of this format is to allow a simple XML info path, ");
- sw.WriteLine(" yet also allow others to add their own info beyond the main info ");
- sw.WriteLine(" needed for a form without breaking any existing code. ");
- sw.WriteLine(" ");
- sw.WriteLine(" -->");
- sw.WriteLine(" <xsd:schema id=\"root\" xmlns=\"\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">");
- sw.WriteLine(" <xsd:import namespace=\"http://www.w3.org/XML/1998/namespace\" />");
- sw.WriteLine(" <xsd:element name=\"root\" msdata:IsDataSet=\"true\">");
- sw.WriteLine(" <xsd:complexType>");
- sw.WriteLine(" <xsd:choice maxOccurs=\"unbounded\">");
- sw.WriteLine(" <xsd:element name=\"metadata\">");
- sw.WriteLine(" <xsd:complexType>");
- sw.WriteLine(" <xsd:attribute name=\"name\" type=\"xsd:string\" />");
- sw.WriteLine(" <xsd:attribute name=\"value\" type=\"xsd:string\" />");
- sw.WriteLine(" </xsd:complexType>");
- sw.WriteLine(" </xsd:element>");
- sw.WriteLine(" <xsd:element name=\"assembly\">");
- sw.WriteLine(" <xsd:complexType>");
- sw.WriteLine(" <xsd:attribute name=\"alias\" type=\"xsd:string\" />");
- sw.WriteLine(" <xsd:attribute name=\"name\" type=\"xsd:string\" />");
- sw.WriteLine(" </xsd:complexType>");
- sw.WriteLine(" </xsd:element>");
- sw.WriteLine(" <xsd:element name=\"data\">");
- sw.WriteLine(" <xsd:complexType>");
- sw.WriteLine(" <xsd:attribute name=\"name\" type=\"xsd:string\" />");
- sw.WriteLine(" <xsd:attribute name=\"type\" type=\"xsd:string\" />");
- sw.WriteLine(" <xsd:attribute name=\"mimetype\" type=\"xsd:string\" />");
- sw.WriteLine(" <xsd:attribute name=\"xml:space\" type=\"xsd:string\" />");
- sw.WriteLine(" </xsd:complexType>");
- sw.WriteLine(" </xsd:element>");
- sw.WriteLine(" <xsd:element name=\"resheader\">");
- sw.WriteLine(" <xsd:complexType>");
- sw.WriteLine(" <xsd:attribute name=\"name\" type=\"xsd:string\" />");
- sw.WriteLine(" </xsd:complexType>");
- sw.WriteLine(" </xsd:element>");
- sw.WriteLine(" </xsd:choice>");
- sw.WriteLine(" </xsd:complexType>");
- sw.WriteLine(" </xsd:element>");
- sw.WriteLine(" </xsd:schema>");
- sw.WriteLine(" <resheader name=\"resmimetype\">");
- sw.WriteLine(" <value>text/microsoft-resx</value>");
- sw.WriteLine(" </resheader>");
- sw.WriteLine(" <resheader name=\"version\">");
- sw.WriteLine(" <value>2.0</value>");
- sw.WriteLine(" </resheader>");
- sw.WriteLine(" <resheader name=\"reader\">");
- sw.WriteLine(" <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>");
- sw.WriteLine(" </resheader>");
- sw.WriteLine(" <resheader name=\"writer\">");
- sw.WriteLine(" <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>");
- sw.WriteLine(" </resheader>");
- sw.WriteLine("</root>");
- }
- }
- }
- private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
- {
- Form1 newForm = new Form1();
- newForm.webView21.Source = new Uri(e.Uri);
- newForm.Show();
- e.Handled = true;
- }
- async void InitializeAsync()
- {
- string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- string edgeVoyagerPath = Path.Combine(docPath, "EdgeVoyager");
- var environment = await CoreWebView2Environment.CreateAsync(null, edgeVoyagerPath);
- await webView21.EnsureCoreWebView2Async(environment);
- string resxFilePath = Path.Combine(docPath, @"EdgeVoyager\favoritesMenuItem.resx");
- using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
- {
- foreach (DictionaryEntry entry in reader)
- {
- string resourceName = entry.Key.ToString();
- string resourceValue = entry.Value.ToString();
- ToolStripMenuItem menuItem = new ToolStripMenuItem
- {
- Text = resourceName,
- Tag = resourceValue
- };
- menuItem.Click += MenuItem_Click;
- }
- }
- webView21.CoreWebView2.WebResourceRequested += WebView2_WebResourceRequested;
- await webView21.CoreWebView2.ExecuteScriptAsync("window.onload=function(){window.chrome.webview.postMessage('100');};");
- webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
- webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
- webView21.GotFocus += (s, e) => isWebView2Focused = true;
- webView21.LostFocus += (s, e) => isWebView2Focused = false;
- if (Settings1.Default.firstwindow == true)
- {
- string htmlString = @"<!DOCTYPE html>
- <html lang=""zh-CN"">
- <head>
- <meta charset=""UTF-8"">
- <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
- <title>起始页</title>
- <style>
- body {
- margin: 0;
- padding: 0;
- background-image: url('https://www.beijing.gov.cn/images/index_top_ch_20190924.jpg');
- background-size: cover;
- background-repeat: no-repeat;
- background-position: right top;
- background-attachment: fixed;
- font-family: Arial, sans-serif;
- }
- .header {
- background-color: rgba(255, 255, 255, 0.8);
- backdrop-filter: blur(10px);
- padding: 10px 20px;
- display: flex;
- align-items: center;
- position: relative;
- box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
- }
- .logo {
- width: 48px;
- height: 48px;
- }
- .nav-item {
- margin-left: 20px;
- color: #333;
- text-decoration: none;
- font-size: 16px;
- }
- .nav-links {
- display: flex;
- align-items: center;
- }
- .welcome-text {
- text-align: center;
- margin: 20px 0;
- font-size: 36px;
- font-weight: bold;
- color: #0078D4;
- }
- .search-container {
- display: flex;
- justify-content: center;
- align-items: center;
- margin-top: 20px;
- border-radius: 15px;
- overflow: hidden;
- }
- .search-input {
- width: 70vw;
- max-width: 1000px;
- padding: 10px;
- font-size: 16px;
- border: none;
- border-radius: 15px 0 0 15px;
- height: 20px;
- }
- .search-button {
- padding: 10px 20px;
- width: 80px;
- font-size: 16px;
- cursor: pointer;
- border: none;
- background-color: #0078D4;
- color: white;
- border-radius: 0 15px 15px 0;
- height: 40px;
- }
- .search-engine-select {
- margin-left: 0px;
- padding: 10px;
- font-size: 16px;
- border: none;
- border-radius: 0px;
- background-color: #f2f2f2;
- cursor: pointer;
- }
- .acrylic-panel {
- background-color: rgba(255, 255, 255, 0.8);
- backdrop-filter: blur(10px);
- padding: 20px;
- border-radius: 8px;
- box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
- margin: 20px 0;
- width: 90vw;
- margin-left: auto;
- margin-right: auto;
- max-width: 1200px;
- }
- @media (max-width: 600px) {
- .search-button {
- padding: 8px 16px;
- }
- }
- </style>
- </head>
- <body>
- <div class=""header"">
- <svg class=""logo"" xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 48 48"">
- <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHna"" x1=""27.702"" x2=""35.722"" y1=""18.41"" y2=""26.008"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#0e5fa4""/>
- <stop offset=""1"" stop-color=""#1595df""/>
- </linearGradient>
- <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHna)"" d=""M24,6l18,16v17.586c0,0.891-1.077,1.337-1.707,0.707L24,24V6z""/>
- <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnb"" x1=""16.667"" x2=""22.667"" y1=""27"" y2=""27"" gradientTransform=""matrix(-3 0 0 -1 92 42)"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#50e6ff""/>
- <stop offset=""1"" stop-color=""#32bdef""/>
- </linearGradient>
- <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnb)"" d=""M24,6l18,16c-7.249-2.341-14-2-18,2V6z""/>
- <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnc"" x1=""18.483"" x2=""27.736"" y1=""19.517"" y2=""10.264"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#0e5fa4""/>
- <stop offset=""1"" stop-color=""#1595df""/>
- </linearGradient>
- <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnc)"" d=""M6,24L22,6h17.586c0.891,0,1.337,1.077,0.707,1.707L24,24H6z""/>
- <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnd"" x1=""3"" x2=""9"" y1=""24"" y2=""24"" gradientTransform=""matrix(0 3 -1 0 39 -3)"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#50e6ff""/>
- <stop offset=""1"" stop-color=""#32bdef""/>
- </linearGradient>
- <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnd)"" d=""M6,24L22,6c-2.341,7.249-2,14,2,18H6z""/>
- <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHne"" x1=""19.748"" x2=""7.491"" y1=""29.748"" y2=""17.491"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#0e5fa4""/>
- <stop offset=""1"" stop-color=""#1595df""/>
- </linearGradient>
- <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHne)"" d=""M24,42L6,26V8.414c0-0.891,1.077-1.337,1.707-0.707L24,24V42z""/>
- <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnf"" x1=""6"" x2=""12"" y1=""33"" y2=""33"" gradientTransform=""matrix(3 0 0 1 -12 0)"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#50e6ff""/>
- <stop offset=""1"" stop-color=""#32bdef""/>
- </linearGradient>
- <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnf)"" d=""M24,42L6,26c7.249,2.341,14,2,18-2V42z""/>
- <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHng"" x1=""28.457"" x2=""19.623"" y1=""29.543"" y2=""38.377"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#0e5fa4""/>
- <stop offset=""1"" stop-color=""#1595df""/>
- </linearGradient>
- <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHng)"" d=""M24,24L7.707,40.293C7.077,40.923,7.523,42,8.414,42H26l7.529-8.471L24,24z""/>
- <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnh"" x1=""24"" x2=""33.529"" y1=""33"" y2=""33"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#50e6ff""/>
- <stop offset=""1"" stop-color=""#32bdef""/>
- </linearGradient>
- <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnh)"" d=""M33.529,33.529L24,24c4,4,4.341,10.751,2,18L33.529,33.529z""/>
- <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHni"" x1=""24.517"" x2=""23.183"" y1=""24.517"" y2=""23.183"" gradientUnits=""userSpaceOnUse"">
- <stop offset=""0"" stop-color=""#ffd000""/>
- <stop offset=""1"" stop-color=""#fede00""/>
- </linearGradient>
- <circle cx=""24"" cy=""24"" r=""2"" fill=""url(#Z3u55as5xQT0Xm3pZ~tHni)""/>
- </svg>
- <span style=""font-size: 24px; color: #333; margin-left: 10px;"">LYKNS</span>
- <div class=""nav-links"">
- <a href=""https://www.lykns.com.cn"" class=""nav-item"">主站</a>
- <a href=""https://its.lykns.com.cn"" class=""nav-item"">云服务</a>
- <a href=""https://dev.lykns.com.cn"" class=""nav-item"">Git 服务</a>
- <a href=""https://survey.lykns.com.cn"" class=""nav-item"">问卷考试系统</a>
- </div>
- </div>
- <div class=""acrylic-panel"">
- <div style=""text-align:center;"">
- <svg xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 48 48"" width=""128"" height=""128"">
- <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"">
- <stop offset=""0"" stop-color=""#737b80"" />
- <stop offset="".473"" stop-color=""#686f74"" />
- <stop offset=""1"" stop-color=""#575c61"" />
- </linearGradient>
- <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"" />
- <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"">
- <stop offset=""0"" stop-color=""#ccc"" />
- <stop offset="".758"" stop-color=""#f2f2f2"" />
- <stop offset=""1"" stop-color=""#fff"" />
- </linearGradient>
- <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"" />
- <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"">
- <stop offset=""0"" stop-color=""#21ad64"" />
- <stop offset=""1"" stop-color=""#088242"" />
- </linearGradient>
- <circle cx=""24"" cy=""24"" r=""9"" fill=""url(#_EnvA6UXZqPqJP5nSvD1Kc)"" />
- </svg>
- </div>
- <div class=""welcome-text"">
- 欢迎使用 LYKNS EdgeVoyager!
- </div>
- <div class=""search-container"">
- <input type=""text"" id=""search-input"" class=""search-input"" placeholder=""搜索..."">
- <select id=""search-engine-select"" class=""search-engine-select"">
- <option value=""bing"">Bing</option>
- <option value=""baidu"">百度</option>
- <option value=""google"">Google</option>
- </select>
- <button onclick=""search()"" class=""search-button"">搜索</button>
- </div>
- </div>
- <script>
- function search() {
- var query = document.getElementById('search-input').value;
- var engine = document.getElementById('search-engine-select').value;
- var url = '';
- if (engine === 'bing') {
- url = 'https://www.bing.com/search?q=' + encodeURIComponent(query);
- } else if (engine === 'baidu') {
- url = 'https://www.baidu.com/s?wd=' + encodeURIComponent(query);
- } else if (engine === 'google') {
- url = 'https://www.google.com/search?q=' + encodeURIComponent(query);
- }
- window.open(url, '_blank');
- }
- </script>
- </body>
- </html>";
- webView21.CoreWebView2.NavigateToString(htmlString);
- Settings1.Default.firstwindow = false;
- Settings1.Default.Save();
- }
- }
- private void back_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoBack();
- }
- private void forward_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoForward();
- }
- private void refresh_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Reload();
- }
- private void stop_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Stop();
- }
- private void search_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text.Length > 0)
- {
- if (comboBox1.Text != "about:blank")
- {
- Form1 newForm = new Form1();
- newForm.webView21.Source = new Uri("https://cn.bing.com/search?q=" + textBox1.Text);
- newForm.Show();
- }
- else
- {
- webView21.CoreWebView2.Navigate("https://cn.bing.com/search?q=" + textBox1.Text);
- webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
- }
- }
- }
- private bool isHttpsFallback = false;
- private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
- {
- string uri = e.Uri;
- if (uri.StartsWith("http://"))
- {
- string httpsUri = "https://" + uri.Substring("http://".Length);
- e.Cancel = true;
- webView21.CoreWebView2.Navigate(httpsUri);
- }
- string currentUrl = webView21.Source.ToString();
- if (!comboBox1.Items.Contains(currentUrl))
- comboBox1.Items.Add(currentUrl);
- comboBox1.Text = currentUrl;
- progressBar.Value = 0;
- progressLabel.Text = "0%";
- StatusLabel.Text = "开始导航到:" + currentUrl;
- }
- private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
- {
- if (!e.IsSuccess && isHttpsFallback)
- {
- isHttpsFallback = false;
- string originalUri = webView21.CoreWebView2.Source.Replace("https://", "http://");
- webView21.CoreWebView2.Navigate(originalUri);
- }
- else if (e.IsSuccess && webView21.CoreWebView2.Source.StartsWith("https://"))
- isHttpsFallback = false;
- string currentUrl = webView21.Source.ToString();
- if (!comboBox1.Items.Contains(currentUrl))
- comboBox1.Items.Add(currentUrl);
- comboBox1.Text = currentUrl;
- progressBar.Value = 100;
- progressLabel.Text = "100%";
- StatusLabel.Text = "完成。";
- string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
- this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
- string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);");
- faviconUrl = faviconUrl.Trim('"');
- if (!string.IsNullOrEmpty(faviconUrl))
- {
- Uri uriResult;
- bool isAbsoluteUri = Uri.TryCreate(faviconUrl, UriKind.Absolute, out uriResult);
- if (isAbsoluteUri)
- {
- using (var client = new HttpClient())
- {
- try
- {
- var faviconData = await client.GetByteArrayAsync(uriResult);
- using (var ms = new MemoryStream(faviconData))
- {
- using (var img = Image.FromStream(ms))
- {
- if (img is Bitmap bitmap)
- Icon = Icon.FromHandle(bitmap.GetHicon());
- else
- Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
- }
- }
- }
- catch (Exception ex)
- {
- }
- }
- }
- }
- }
- private void comboBox1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- string input = comboBox1.Text;
- if (!input.StartsWith("http://") && !input.StartsWith("https://"))
- {
- comboBox1.Text = "https://" + input;
- comboBox1.SelectionStart = comboBox1.Text.Length;
- }
- string url = comboBox1.Text;
- if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
- webView21.CoreWebView2.Navigate(url);
- }
- }
- private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show("确认关闭 LYKNS EdgeVoyager 所有窗口?", "退出确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- Environment.Exit(0);
- }
- private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
- {
- progressBar.Value = 50;
- progressLabel.Text = "50%";
- StatusLabel.Text = "内容开始加载";
- }
- private async void SetZoomLevel(double zoomLevel)
- {
- await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
- }
- private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
- {
- SetZoomLevel(100);
- toolStripMenuItem3.Enabled = true;
- }
- private void toolStripMenuItem2_Click(object sender, EventArgs e)
- {
- zoom = zoom + 25;
- SetZoomLevel(zoom);
- toolStripMenuItem3.Enabled = true;
- }
- private void toolStripMenuItem3_Click(object sender, EventArgs e)
- {
- zoom = zoom - 25;
- SetZoomLevel(zoom);
- if (zoom == 25)
- toolStripMenuItem3.Enabled = false;
- }
- private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Reload();
- }
- private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Stop();
- }
- private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoForward();
- }
- private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoBack();
- }
- private void webView21_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
- {
- StatusLabel.Text = "源地址已更改";
- }
- private void WebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
- {
- string requestUri = e.Request.Uri;
- StatusLabel.Text = "请求资源:" + requestUri;
- }
- private void 工具栏ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
- {
- if (工具栏ToolStripMenuItem.Checked == true)
- {
- statusStrip1.Visible = true;
- webView21.Height = webView21.Height - 23;
- }
- else
- {
- statusStrip1.Visible = false;
- webView21.Height = webView21.Height + 23;
- }
- }
- private void 关于EdgeVoyagerToolStripMenuItem_Click(object sender, EventArgs e)
- {
- AboutBox1 aboutBox1 = new AboutBox1();
- aboutBox1.ShowDialog();
- }
- private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Settings1.Default.firstwindow = true;
- Settings1.Default.Save();
- Form1 newForm = new Form1();
- newForm.Show();
- }
- private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "网页文件 (*.html;*.htm)|*.html;*.htm|可缩放矢量图形文件 (*.svg)|*.svg|所有文件 (*.*)|*.*";
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- string filePath = openFileDialog.FileName;
- Form1 newForm = new Form1();
- newForm.webView21.Source = new Uri("file:///" + filePath);
- newForm.Show();
- }
- }
- private async void 保存SToolStripMenuItem_ClickAsync(object sender, EventArgs e)
- {
- }
- private async void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
- {
- string title = webView21.CoreWebView2.DocumentTitle;
- try
- {
- CoreWebView2PrintStatus printStatus = await webView21.CoreWebView2.PrintAsync(null);
- if (printStatus == CoreWebView2PrintStatus.Succeeded)
- MessageBox.Show(this, "打印 " + title + " 文档到打印机成功", "打印到默认打印机");
- else if (printStatus == CoreWebView2PrintStatus.PrinterUnavailable)
- MessageBox.Show(this, "打印机不可用,离线或错误状态", "打印到默认打印机");
- else
- MessageBox.Show(this, "打印 " + title + " 文档到打印机失败", "打印到默认打印机");
- }
- catch (Exception)
- {
- MessageBox.Show(this, "打印 " + title + " 文档已在进行中", "打印到默认打印机");
- }
- }
- private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.ShowPrintUI();
- }
- private void 发送SToolStripMenuItem_Click(object sender, EventArgs e)
- {
- string mailtoLink = $"mailto:?subject=共享网页&body=请查看这个网页:" + comboBox1.Text;
- Process.Start(new ProcessStartInfo
- {
- FileName = mailtoLink,
- UseShellExecute = true
- });
- }
- private async void 属性PToolStripMenuItem_Click(object sender, EventArgs e)
- {
- string title = webView21.CoreWebView2.DocumentTitle;
- string url = webView21.CoreWebView2.Source;
- string content = await GetWebPageContent(url);
- int charCount = content.Length;
- int byteCount = Encoding.UTF8.GetByteCount(content);
- string encoding = "UTF-8";
- string language = "zh-CN";
- string contentType = "text/html";
- string author = await GetMetaData("author");
- string description = await GetMetaData("description");
- string keywords = await GetMetaData("keywords");
- string updateDate = await GetMetaData("last-modified");
- string securityInfo = await GetSecurityInfo(url);
- string securityLevel = "High";
- string faviconUrl = await GetFaviconUrl();
- string encodingLanguage = "HTML, JavaScript";
- MessageBox.Show(
- $"常规信息:\n" +
- $"标题:{title}\n" +
- $"地址(URL):{url}\n" +
- $"编码:{encoding}\n" +
- $"语言:{language}\n" +
- $"内容类型:{contentType}\n" +
- $"\n网页内容信息:\n" +
- $"字符数:{charCount}\n" +
- $"字节数:{byteCount}\n" +
- $"编码方式:HTML, XML\n" +
- $"\n网页元数据:\n" +
- $"作者:{author}\n" +
- $"描述:{description}\n" +
- $"关键词:{keywords}\n" +
- $"更新日期:{updateDate}\n" +
- $"\n网页安全信息:\n" +
- $"安全证书信息:{securityInfo}\n" +
- $"安全级别:{securityLevel}\n" +
- $"\n其他信息:\n" +
- $"收藏夹图标:{faviconUrl}\n" +
- $"编码语言:{encodingLanguage}",
- "网页属性",
- MessageBoxButtons.OK,
- MessageBoxIcon.Information
- );
- }
- private async Task<string> GetWebPageContent(string url)
- {
- try
- {
- HttpResponseMessage response = await httpClient.GetAsync(url);
- response.EnsureSuccessStatusCode();
- return await response.Content.ReadAsStringAsync();
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- private async Task<string> GetMetaData(string name)
- {
- try
- {
- string script = $"document.querySelector('meta[name=\"{name}\"]').content;";
- return await webView21.CoreWebView2.ExecuteScriptAsync(script);
- }
- catch (Exception)
- {
- return "不可用";
- }
- }
- private async Task<string> GetFaviconUrl()
- {
- try
- {
- string script = "Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);";
- string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync(script);
- return faviconUrl.Trim('"');
- }
- catch (Exception)
- {
- return "不可用";
- }
- }
- private async Task<string> GetSecurityInfo(string url)
- {
- try
- {
- HttpResponseMessage response = await httpClient.GetAsync(url);
- response.EnsureSuccessStatusCode();
- if (response.RequestMessage.RequestUri.Scheme == "https")
- {
- var headers = response.Headers;
- if (headers.Contains("Strict-Transport-Security"))
- return "HSTS已开启";
- }
- return "无HSTS";
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (comboBox1.Focused)
- {
- if (comboBox1.SelectedText != "")
- {
- Clipboard.SetText(comboBox1.SelectedText);
- comboBox1.Text = comboBox1.Text.Remove(comboBox1.SelectionStart, comboBox1.SelectionLength);
- comboBox1.Focus();
- }
- }
- else if (textBox1.Focused)
- {
- if (textBox1.SelectedText != "")
- {
- Clipboard.SetText(textBox1.SelectedText);
- textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart, textBox1.SelectionLength);
- }
- }
- }
- private async void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (comboBox1.Focused)
- {
- if (comboBox1.SelectedText != "")
- Clipboard.SetText(comboBox1.SelectedText);
- }
- else if (textBox1.Focused)
- {
- if (textBox1.SelectedText != "")
- Clipboard.SetText(textBox1.SelectedText);
- }
- else if (isWebView2Focused)
- {
- string script = "window.getSelection().toString();";
- string selectedText = await webView21.CoreWebView2.ExecuteScriptAsync(script);
- selectedText = selectedText.Trim('"');
- if (!string.IsNullOrEmpty(selectedText))
- {
- Clipboard.SetText(selectedText);
- }
- }
- }
- private async void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (comboBox1.Focused)
- {
- if (comboBox1.SelectionLength > 0)
- {
- string clipboardText = Clipboard.GetText();
- comboBox1.Text = comboBox1.Text.Remove(comboBox1.SelectionStart, comboBox1.SelectionLength) + clipboardText;
- comboBox1.SelectionStart = comboBox1.Text.Length;
- }
- else
- {
- string clipboardText = Clipboard.GetText();
- comboBox1.Text += clipboardText;
- comboBox1.SelectionStart = comboBox1.Text.Length;
- }
- }
- else if (textBox1.Focused)
- {
- textBox1.Paste();
- }
- }
- private async void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (comboBox1.Focused)
- {
- comboBox1.SelectAll();
- }
- else if (textBox1.Focused)
- {
- textBox1.SelectAll();
- }
- else
- {
- string script = "document.execCommand('selectAll', false, null);";
- await webView21.CoreWebView2.ExecuteScriptAsync(script);
- }
- }
- private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private async void 添加到收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- string resxFilePath = Path.Combine(docPath, @"EdgeVoyager\favoritesMenuItem.resx");
- string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
- string pageTitle = title.Trim('"');
- string pageUrl = webView21.CoreWebView2.Source.ToString();
- bool titleExists = false;
- using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
- {
- foreach (DictionaryEntry entry in reader)
- {
- if (entry.Key.ToString() == pageTitle)
- {
- titleExists = true;
- break;
- }
- }
- }
- if (!titleExists)
- {
- ResXResourceReader reader = new ResXResourceReader(resxFilePath);
- Hashtable resources = new Hashtable();
- foreach (DictionaryEntry entry in reader)
- {
- resources.Add(entry.Key, entry.Value);
- }
- reader.Close();
- resources.Add(pageTitle, pageUrl);
- using (ResXResourceWriter writer = new ResXResourceWriter(resxFilePath))
- {
- foreach (DictionaryEntry entry in resources)
- {
- writer.AddResource(entry.Key.ToString(), entry.Value);
- }
- writer.Generate();
- }
- for (int i = 0; i < 收藏夹AToolStripMenuItem.DropDownItems.Count; i++)
- {
- if (收藏夹AToolStripMenuItem.DropDownItems[i] is ToolStripSeparator &&
- 收藏夹AToolStripMenuItem.DropDownItems[i].Name == "toolStripSeparator10")
- {
- for (int j = 收藏夹AToolStripMenuItem.DropDownItems.Count - 1; j > i; j--)
- {
- 收藏夹AToolStripMenuItem.DropDownItems.RemoveAt(j);
- }
- break;
- }
- }
- using (ResXResourceReader reader1 = new ResXResourceReader(resxFilePath))
- {
- foreach (DictionaryEntry entry in reader1)
- {
- string resourceName = entry.Key.ToString();
- string resourceValue = entry.Value.ToString();
- ToolStripMenuItem menuItem = new ToolStripMenuItem
- {
- Text = resourceName,
- Tag = resourceValue
- };
- menuItem.Click += MenuItem_Click;
- 收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
- }
- }
- }
- }
- private void MenuItem_Click(object sender, EventArgs e)
- {
- ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
- string url = (string)menuItem.Tag;
- webView21.CoreWebView2.Navigate(url);
- }
- private void 整理收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Form2 form2 = new Form2();
- form2.ShowDialog();
- string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- string resxFilePath = Path.Combine(docPath, @"EdgeVoyager\favoritesMenuItem.resx");
- for (int i = 0; i < 收藏夹AToolStripMenuItem.DropDownItems.Count; i++)
- {
- if (收藏夹AToolStripMenuItem.DropDownItems[i] is ToolStripSeparator &&
- 收藏夹AToolStripMenuItem.DropDownItems[i].Name == "toolStripSeparator10")
- {
- for (int j = 收藏夹AToolStripMenuItem.DropDownItems.Count - 1; j > i; j--)
- {
- 收藏夹AToolStripMenuItem.DropDownItems.RemoveAt(j);
- }
- break;
- }
- }
- using (ResXResourceReader reader1 = new ResXResourceReader(resxFilePath))
- {
- foreach (DictionaryEntry entry in reader1)
- {
- string resourceName = entry.Key.ToString();
- string resourceValue = entry.Value.ToString();
- ToolStripMenuItem menuItem = new ToolStripMenuItem
- {
- Text = resourceName,
- Tag = resourceValue
- };
- menuItem.Click += MenuItem_Click;
- 收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
- }
- }
- }
- private void Form1_Activated(object sender, EventArgs e)
- {
- string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- string resxFilePath = Path.Combine(docPath, @"EdgeVoyager\favoritesMenuItem.resx");
- for (int i = 0; i < 收藏夹AToolStripMenuItem.DropDownItems.Count; i++)
- {
- if (收藏夹AToolStripMenuItem.DropDownItems[i] is ToolStripSeparator &&
- 收藏夹AToolStripMenuItem.DropDownItems[i].Name == "toolStripSeparator10")
- {
- for (int j = 收藏夹AToolStripMenuItem.DropDownItems.Count - 1; j > i; j--)
- {
- 收藏夹AToolStripMenuItem.DropDownItems.RemoveAt(j);
- }
- break;
- }
- }
- using (ResXResourceReader reader1 = new ResXResourceReader(resxFilePath))
- {
- foreach (DictionaryEntry entry in reader1)
- {
- string resourceName = entry.Key.ToString();
- string resourceValue = entry.Value.ToString();
- ToolStripMenuItem menuItem = new ToolStripMenuItem
- {
- Text = resourceName,
- Tag = resourceValue
- };
- menuItem.Click += MenuItem_Click;
- 收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
- }
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Settings1.Default.windows_num++;
- Settings1.Default.Save();
- }
- private void Form1_FormClosed(object sender, FormClosedEventArgs e)
- {
- Settings1.Default.windows_num--;
- Settings1.Default.Save();
- }
- }
- }
|