public object OpenWebPage(ref object url, bool isShow = true) { try { if (isShow) { ie.Visible = true; } else { ie.Visible = false; } ie.Navigate2(ref url); while (true) { if (!isBusy()) { break; } Thread.Sleep(1000); } return(ie.Document); } catch (Exception ex) { SharpLog.Error("OpenWebPage", ex.ToString()); } return(null); }
/// <summary> /// Navigates the browser to the provided URI. /// </summary> /// <param name="uri"> The URI to navigate to. </param> protected override void BrowserNavigateTo(string uri) { try { LogManager.Write("InternetExplorer navigated to " + uri + ".", LogLevel.Verbose); object nil = null; object absoluteUri = uri; _browser.Navigate2(ref absoluteUri, ref nil, ref nil, ref nil, ref nil); WaitForComplete(); var htmlDocument = _browser.Document as IHTMLDocument2; if (htmlDocument == null) { throw new Exception("Failed to run script because no document is loaded."); } Thread.Sleep(50); } catch (Exception) { // A COMException with "RPC_E_DISCONNECTED" happens when the browser switches security context. Thread.Sleep(150); ReinitializeBrowser(); BrowserNavigateTo(uri); } }
public void OpenUrl(String url) { //object o = null; ie = new SHDocVw.InternetExplorerClass(); if (this.allowNewWnd) { ie.Resizable = false; ie.FullScreen = false; } else { ie.FullScreen = true; ie.Resizable = false; } ie.AddressBar = false; ie.MenuBar = false; ie.ToolBar = 0; ie.Visible = true; SendMessage(new IntPtr(ie.HWND), WM_SYSCOMMAND, SC_MAXIMIZE, 0); ie.Navigate2(url); ie.BeforeNavigate2 += new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(beforeNavHandler); ie.NewWindow2 += new SHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(wbEvents_NewWindow2); ie.NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(wbEvents_NewWindow3); ie.NewProcess += new SHDocVw.DWebBrowserEvents2_NewProcessEventHandler(wbEvents_NewProcess); ie.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(wbEvents_DocumentComplete); ie.WindowClosing += new SHDocVw.DWebBrowserEvents2_WindowClosingEventHandler(wbEvents_WindowClosing); }
//public WebBrowserEvents(InternetExplorer _intExp, ref SWATWikiGenerator _write) //{ // _writer = _write; // Random rand = new Random(); // browserId = rand.Next(100000); // if (_intExp == null) // { // manualNavigation = true; // ie = new InternetExplorerClass(); // //add a property for identification // ie.PutProperty("ID", browserId); // ie.Visible = true; // object url = "about:blank"; // object nullObj = String.Empty; // ConnectWindow(ie); // ie.Navigate2(ref url, ref nullObj, ref nullObj, ref nullObj, ref nullObj); // HTMLEvents.lastBrowser = browserId; // } // else // { // manualNavigation = false; // ie = _intExp; // ie.PutProperty("ID", browserId); // ConnectWindow(ie); // } //} public WebBrowserEvents(String flag, SHDocVw.InternetExplorer _intExp, ref SWATWikiGenerator _write) { _writer = _write; Random rand = new Random(); browserId = rand.Next(100000); if (flag.Equals("AttachToWindow")) { init = false; manualNavigation = false; ie = _intExp; ie.PutProperty("ID", browserId); ConnectWindow(ie); //Debug.WriteLine((((HTMLDocument)ie.Document).frames).length); try { FramesCollection frames = (((HTMLDocument)ie.Document).frames); for (int i = 0; i < frames.length; i++) { object refIndex = i; try { HTMLDocument doc = (HTMLDocument)((HTMLWindow2Class)frames.item(ref refIndex)).document; if (doc.Equals((HTMLDocument)ie.Document)) continue; HTMLEvents aEvent = new HTMLEvents(_writer, doc, ie); _eventsList.Add(aEvent); } catch (UnauthorizedAccessException) { } } } catch (Exception) { } _eventsList.Insert(0, new HTMLEvents(_writer, ie.Document, ie)); checkAttach(); } else if (flag.Equals("NewWindow")) { manualNavigation = true; ie = new InternetExplorerClass(); //add a property for identification ie.PutProperty("ID", browserId); ie.Visible = true; object url = "about:blank"; object nullObj = String.Empty; ConnectWindow(ie); ie.Navigate2(ref url, ref nullObj, ref nullObj, ref nullObj, ref nullObj); HTMLEvents.lastBrowser = browserId; } else if (flag.Equals("PopUp")) { manualNavigation = false; ie = _intExp; ie.PutProperty("ID", browserId); ConnectWindow(ie); } else if (flag.Equals("AttachToModalWindow")) { IntPtr dialogHwnd = IntPtr.Zero; //int indexCount = NativeMethods.GetWindowWithSubstring("content.html", 0, 0, ref dialogHwnd); dialogHwnd = NativeMethods.GetForegroundWindow(); if (dialogHwnd != IntPtr.Zero) { // This block of code SHOULD take a window handle and return an IHTMLDocument object to dialogDoc // Then it will assign that object to _doc. It follows an example in the MSDN library and WatiN uses it as well. // If it performs correctly, then we should be attached to the Dialog. dialogHwnd = NativeMethods.GetChildWindowHwnd(dialogHwnd, "Internet Explorer_Server"); //dialogHwnd = NativeMethods.GetForegroundWindow(); Int32 result = 0; Int32 dialog; Int32 message; message = NativeMethods.RegisterWindowMessage("WM_HTML_GETOBJECT"); NativeMethods.SendMessageTimeout(dialogHwnd, message, 0, 0, NativeMethods.SMTO_ABORTIFHUNG, 1000, ref result); //NativeMethods.SendMessage(dialogHwnd, NativeMethods.WM_CLOSE, 0, 0); IHTMLDocument2 dialogDoc = null; System.Guid dialogID = typeof(mshtml.HTMLDocument).GUID; dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc); //HTMLFrameBase dialogDoc = null; //System.Guid dialogID = typeof(mshtml.HTMLFrameBase).GUID; //dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc); //dialogDoc.title = "Hello?"; //IHTMLDialog dialogDoc = null; //System.Guid dialogID = typeof(mshtml.HTMLDialog).GUID; //dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc); //IHTMLWindow2 dialogDoc = null; //System.Guid dialogID = typeof(IHTMLWindow2).GUID; //dialog = NativeMethods.ObjectFromLresult(result, ref dialogID, 0, ref dialogDoc); //dialogDoc.close(); //Console.WriteLine("URL: " + dialogDoc.close + "\nTitle: " + dialogDoc.title); //ie = (InternetExplorer)dialogDoc; //ie.PutProperty("ID", browserId); //ConnectWindow(ie); //doc = (HTMLDocument)dialogDoc; } } _browserList.Add(this); }
public sealed override async Task <Tuple <string, LinkFetchResult> > GetSourceLink(string url) { var ie = new IE(); //Create new browser (IE) ie.Navigate2(url); while (ie.ReadyState != tagREADYSTATE.READYSTATE_COMPLETE) { } await Pause(DelayInMilliseconds); Browser browser = ((Browser)ie); HTMLDocument doc = ((HTMLDocument)browser.Document); //Get document IHTMLElement button = doc.getElementById("btn_download"); //Define HTML button button.click(); string tempFile = $"{System.IO.Path.GetTempPath()}{Guid.NewGuid().ToString()}.lf.html"; await JavaScriptProcessingTime(); doc = ((HTMLDocument)browser.Document); using (var output = new FileStream(tempFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)) { byte[] buffer = ASCIIEncoding.UTF8.GetBytes(((HTMLDocument)browser.Document).documentElement.outerHTML); await output.WriteAsync(buffer, 0, buffer.Length); } var line = string.Empty; var input = new StreamReader(tempFile); Match match = null; while ((line = input.ReadLine()) != null) //Scan file for link. { if (line == "") { continue; } match = _sourceUrlPattern.Match(line); if (match.Success) { break; } } var sourceUrl = string.Empty; var response = LinkFetchResult.SUCCESSFULL; if (match.Success) { sourceUrl = match.Groups[1].Value; } else { response = LinkFetchResult.FAILED; } return(new Tuple <string, LinkFetchResult>(sourceUrl, response)); }