private void Test2Button_Click(object sender, EventArgs e) { // This is demonstration of async browser construction. // .Load method raises exception with message 'CefBrowser is not ready.', cause underlying CefBrowser doesn't created. // I propose to that .Load method locks internally and wait for CefBrowser (AfterHandleCreated message). // But note, that lock also lock main message loop (if called from UI thread), or we can got inifinite lock, if no message arrived - so some timeout required. // If we have plans to support singlethreaded cef message loop - we must process cef message loop. // We can give name for event AfterHandleCreated as BrowserReady or Ready. // Note, that this is event only signals, that browser constructed, - this is NOT DocumentReady or Navigated. // Also .BrowserReady event is good, but method to perform sync operation also can be useful. PrepareTest(); browser = new CefWebBrowser(); browser.Parent = panel; browser.Dock = DockStyle.Fill; try { browser.Load("http://google.com"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void HandleGoButtonClick(object sender, EventArgs e) { if (_browserControl.IsLoading) { _browserControl.Stop(); } else { _browserControl.Load(urlTextBox.Text); } }
protected void AddToolbarAndBrowserToTab(TabPage tabPage, CefWebBrowser browser) { TextBox urlbox = new TextBox(); urlbox.Top = 0; urlbox.Width = 200; //urlbox.Text = "https://getfirebug.com/releases/lite/1.2/"; //urlbox.Text = "file:///G:/en2/build/view/demo.html"; //urlbox.Text = "file:///G:/en2/build/view/demo.html"; Button nav = new Button(); nav.Text = "Go"; nav.Left = 200; Button newTab = new Button(); newTab.Text = "NewTab"; newTab.Left = 200 + nav.Width; Button closeTab = new Button(); closeTab.Text = "Close"; closeTab.Left = 200 + nav.Width + newTab.Width; //browser.PropertyChanged += (se, ev) => //{ // if (ev.PropertyName != "IsBrowserInitialized") return; // if (html == string.Empty) // { // string url = "https://dictionary.cambridge.org/grammar/british-grammar/above-or-over", text = string.Empty, _fix_lib = string.Empty; // Debug.WriteLine("#-> " + url); // text = f_link_getHtmlOnline(url); // string head = text.Split(new string[] { "<body" }, StringSplitOptions.None)[0], s = "<div" + text.Substring(head.Length + 5); // int posH1 = s.ToLower().IndexOf("<h1"); // if (posH1 != -1) s = s.Substring(posH1, s.Length - posH1); // head = Html.f_html_Format(url, head); // s = Html.f_html_Format(url, s); // //if (File.Exists("view/fix.html")) _fix_lib = File.ReadAllText("view/fix.html"); // text = head.Replace("<head>", @"<head><meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />" + _fix_lib) + "<body><article id=___body><!--START_BODY-->" + s + "<!--END_BODY--></article></body></html>"; // html = s; // } // browser.Reload(); // // browser.Document.Body.InnerHtml = html; // //Debug.WriteLine(browser.Document.Body.InnerHtml) // //string firebuglite_bookmark_run = "javascript:var firebug=document.createElement('script');firebug.setAttribute('src','http://localhost:56789/firebug-lite.1.2.js');document.body.appendChild(firebug);(function(){if(window.firebug.version){firebug.init();}else{setTimeout(arguments.callee);}})();void(firebug);"; // //browser.Navigate(firebuglite_bookmark_run); //}; nav.Click += delegate { // use javascript to warn if url box is empty. if (string.IsNullOrEmpty(urlbox.Text.Trim())) { browser.Load("javascript:alert('hey try typing a url!');"); } browser.Load(urlbox.Text); tabPage.Text = urlbox.Text; }; newTab.Click += delegate { AddTab(); }; closeTab.Click += delegate { m_tabControl.Controls.Remove(tabPage); browser.Dispose(); }; tabPage.Controls.Add(urlbox); tabPage.Controls.Add(nav); tabPage.Controls.Add(newTab); tabPage.Controls.Add(closeTab); tabPage.Controls.Add(browser); }