private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (!finished) { title = webBrowser1.DocumentTitle; icon = GetIcon(); url = webBrowser1.Url.AbsoluteUri; isHome = DnsConvert.ToPunycode(url) == Home; toolStripMenuItem1.Enabled = !isHome; UrlObject uo; if (!accs.ContainsUrl(url, out uo)) { UrlObject u = new UrlObject(title, url, icon); u.IsHome = isHome; accs.Add(u); } SaveAccessRecordSyncFav(); LoadHistoryList(); access = accs.GetUrlAccessCount(Url); finished = true; } button8.Enabled = true; toolStripProgressBar1.Visible = false; OnDocumentCompleted(e); }
public void Navigate(string address) { if (string.IsNullOrEmpty(address)) { GoHome(); return; } if (address == "about:blank") { return; } if (!address.StartsWith("http://", true, null)) { address = "http://" + address; } address = DnsConvert.ToPunycode(address); if (IsValidURL(address)) { HttpWebResponse res = null; try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(address); res = (HttpWebResponse)req.GetResponse(); webBrowser1.Navigate(address); } catch (Exception ex) { Log("Goto", ex); string engine = KellCommon.ReadIni("AppSettings", "Search", KellCommon.IniFile); if (!string.IsNullOrEmpty(engine)) { string[] engEnc = engine.Split('|'); if (engEnc.Length > 2) { webBrowser1.Navigate(engEnc[1] + "=" + System.Web.HttpUtility.UrlEncode(kellComboBox1.Text.Trim(), Encoding.GetEncoding(engEnc[2].ToLower()))); } else { webBrowser1.Navigate(engEnc[1] + "=" + kellComboBox1.Text.Trim()); } } } finally { if (res != null) { res.Close(); } } } else { System.Windows.Forms.MessageBox.Show("网址错误!"); } }
public static Icon GetUrlIcon(this List <UrlObject> urls, string urlString) { foreach (UrlObject url in urls) { if (DnsConvert.ToPunycode(url.Url) == DnsConvert.ToPunycode(urlString.ToLower())) { return(url.Image); } } return(null); }
public static string GetUrlTitle(this List <UrlObject> urls, string urlString) { foreach (UrlObject url in urls) { if (DnsConvert.ToPunycode(url.Url) == DnsConvert.ToPunycode(urlString.ToLower())) { return(url.Title); } } return(""); }
public static int GetUrlAccessCount(this List <UrlObject> urls, string urlString) { foreach (UrlObject url in urls) { if (DnsConvert.ToPunycode(url.Url) == DnsConvert.ToPunycode(urlString.ToLower())) { return(url.Access); } } return(0); }
private void GoHome() { string home = DnsConvert.ToPunycode(Home); if (!string.IsNullOrEmpty(home)) { webBrowser1.Navigate(home); } else { webBrowser1.Navigate("www.xn--2qus7hcfs21e.net"); } }
public static void SyncAccToFav() { foreach (UrlObject uo in favs) { uo.IsHome = DnsConvert.ToPunycode(uo.Url) == Home; UrlObject url; if (accs.ContainsUrl(uo.Url, out url)) { uo.Access = url.Access; uo.IsHome = url.IsHome; } } }
public static bool ContainsUrl(this List <UrlObject> urls, string urlString, out UrlObject uo) { uo = null; foreach (UrlObject url in urls) { if (DnsConvert.ToPunycode(url.Url) == DnsConvert.ToPunycode(urlString.ToLower())) { uo = url; return(true); } } return(false); }
public static bool UpdateUrl(this List <UrlObject> urls, string urlString, string title, Icon icon) { foreach (UrlObject url in urls) { if (DnsConvert.ToPunycode(url.Url) == DnsConvert.ToPunycode(urlString.ToLower())) { url.Access++; url.Title = title; url.Image = icon; return(true); } } return(false); }
private void LoadHistoryList() { kellComboBox1.Items.Clear(); if (File.Exists(AccessFile)) { using (FileStream fs = new FileStream(AccessFile, FileMode.Open, FileAccess.Read)) { if (fs.Length > 0) { try { BinaryFormatter bf = new BinaryFormatter(); accs = (List <UrlObject>)bf.Deserialize(fs); } catch (Exception e) { accs = new List <UrlObject>(); System.Windows.Forms.MessageBox.Show(e.Message); } } else { accs = new List <UrlObject>(); } } } else { accs = new List <UrlObject>(); } foreach (UrlObject u in accs) { u.IsHome = DnsConvert.ToPunycode(u.Url) == Home; } accs.Sort(new UrlComparer()); foreach (UrlObject uo in accs) { if (uo.Image != null) { kellComboBox1.AddItem(uo.Url, uo.Title + Environment.NewLine + uo.Url, uo.Image.ToBitmap()); } else { kellComboBox1.AddItem(uo.Url, uo.Title + Environment.NewLine + uo.Url); } } }
private Icon GetIcon() { Icon icon = null; string icoUri = webBrowser1.Url.Scheme + "://" + DnsConvert.ToPunycode(webBrowser1.Url.Authority) + "/favicon.ico"; if (IsValidURL(icoUri)) { HttpWebResponse res = null; try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(icoUri); res = (HttpWebResponse)req.GetResponse(); Stream s = res.GetResponseStream(); if (s != null) { if (s.CanRead) { byte[] data = new byte[100]; List <byte> buffer = new List <byte>(); int len = 0; while ((len = s.Read(data, 0, data.Length)) > 0) { buffer.AddRange(data.Take <byte>(len)); } MemoryStream ms = new MemoryStream(buffer.ToArray()); icon = new Icon(ms, iconWidth, iconWidth); } } } catch (Exception ex) { Log("Download Icon", ex); } finally { if (res != null) { res.Close(); } } } return(icon); }
private void button1_Click(object sender, EventArgs e) { bool save = false; UrlObject uo; if (KellWebBrowser.Favorites.ContainsUrl(textBox2.Text, out uo)) { if (MessageBox.Show("当前页面已经存在于收藏夹中,确定要覆盖原来的?", "覆盖提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK) { uo.Title = textBox1.Text; uo.Image = KellWebBrowser.DocumentIcon; UrlObject u; if (KellWebBrowser.HistoryUrlList.ContainsUrl(textBox2.Text, out u)) { uo.Access = u.Access; } if (DnsConvert.ToPunycode(uo.Url) == KellWebBrowser.Home) uo.IsHome = true; save = true; } } else { uo = new UrlObject(textBox1.Text, textBox2.Text, KellWebBrowser.DocumentIcon); UrlObject u; if (KellWebBrowser.HistoryUrlList.ContainsUrl(textBox2.Text, out u)) { uo.Access = u.Access; } if (DnsConvert.ToPunycode(uo.Url) == KellWebBrowser.Home) uo.IsHome = true; KellWebBrowser.Favorites.Add(uo); save = true; } if (save) { using (FileStream fs = new FileStream(KellWebBrowser.FavFile, FileMode.Create, FileAccess.Write)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, KellWebBrowser.Favorites); } MessageBox.Show("当前页面已经成功保存到收藏夹中!"); } }
private void button5_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex > -1) { KellWebBrowser.Home = DnsConvert.ToPunycode(KellWebBrowser.Favorites[listBox1.SelectedIndex].Url); foreach (UrlObject u in KellWebBrowser.Favorites) { u.IsHome = false; } KellWebBrowser.Favorites[listBox1.SelectedIndex].IsHome = true; foreach (UrlObject u in KellWebBrowser.HistoryUrlList) { u.IsHome = DnsConvert.ToPunycode(u.Url) == KellWebBrowser.Home; } listBox1.Items[listBox1.SelectedIndex] = KellWebBrowser.Favorites[listBox1.SelectedIndex]; if (homeIndex > -1) { listBox1.Items[homeIndex] = home; } homeIndex = listBox1.SelectedIndex; home = KellWebBrowser.Favorites[listBox1.SelectedIndex]; //MessageBox.Show("主页设置完毕!"); } }
private void SaveAccessRecordSyncFav() { foreach (UrlObject u1 in accs) { u1.IsHome = DnsConvert.ToPunycode(u1.Url) == Home; } foreach (UrlObject u2 in favs) { u2.IsHome = DnsConvert.ToPunycode(u2.Url) == Home; } if (accs.UpdateUrl(url, title, icon)) { using (FileStream fs = new FileStream(AccessFile, FileMode.Create, FileAccess.Write)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, accs); } } UrlObject u; if (accs.ContainsUrl(url, out u)) { UrlObject uo; if (favs.ContainsUrl(url, out uo)) { uo.Access = u.Access; uo.Title = u.Title; uo.Image = u.Image; using (FileStream fs = new FileStream(FavFile, FileMode.Create, FileAccess.Write)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, favs); } } } }
private void toolStripMenuItem1_Click(object sender, EventArgs e) { Home = DnsConvert.ToPunycode(Url); toolStripMenuItem1.Enabled = false; }