Beispiel #1
0
            public SiteManagerCompletedEventArgs Navigate(Uri url, int wait = 0)
            {
                SiteManagerCompletedEventArgs result = new SiteManagerCompletedEventArgs() { ResponseUri = url };
                bool done = false;
                System.Windows.Controls.WebBrowser wb = null;

                Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background,
                    new Action(() =>
                    {
                        PutControl(wb = new System.Windows.Controls.WebBrowser());

                        wb.Navigated += (s, e) =>
                        {
                            result.ResponseUri = e.Uri;
                            HideScriptErrors(s as System.Windows.Controls.WebBrowser, true);
                        };

                        wb.LoadCompleted += (s, e) =>
                        {
                            result.ResponseUri = e.Uri;
                            done = true;
                        };

                        wb.Navigate(url);
                    }));

                #region Wait (done and pause) or 30 sec
                DateTime endTime = DateTime.Now.AddSeconds(30);
                while (!done && (DateTime.Now < endTime))
                    Thread.Sleep(100);

                if (done)
                    Wait(wait);
                #endregion

                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(() =>
                    {
                        try
                        {
                            mshtml.HTMLDocument doc = wb.Document as mshtml.HTMLDocument;
                            if (doc != null && doc.documentElement != null)
                                result.Content = doc.documentElement.innerHTML;
                        }
                        catch (Exception ex)
                        {
                            Helpers.Old.Log.Add(ex, "SiteManagerIE.Navigate().GetHTML()");
                        }
                        finally
                        {
                            RemoveControl(wb);
                            wb = null;
                            GC.Collect();
                        }
                    }));

                return result;
            }
Beispiel #2
0
            public SiteManagerCompletedEventArgs Navigate(Uri url, int wait = 0)
            {
                SiteManagerCompletedEventArgs result = new SiteManagerCompletedEventArgs() { ResponseUri = url };
                try
                {
                    var url2 = url.AbsoluteUri;
                    result.Content = CEFLoader.CEFLoader.GetHTML(ref url2, wait);
                    result.ResponseUri = new Uri(url2);
                }
                catch (Exception ex)
                {
                    Helpers.Old.Log.Add(ex, "SiteManagerCHR.Navigate().GetHTML()");
                }
                return result;

                //if (!inited)
                //    throw new Exception("SiteManagerCHR not initialized");

                //SiteManagerCompletedEventArgs result = new SiteManagerCompletedEventArgs() { ResponseUri = url };
                //bool done = false;
                //bool canDone = false;

                //CefSharp.OffScreen.ChromiumWebBrowser wv = null;

                //#region Activate

                //#region LoadCompleteEvent

                //EventHandler<CefSharp.FrameLoadEndEventArgs> FrameLoadEndEvent = new EventHandler<CefSharp.FrameLoadEndEventArgs>(
                //    (s, e) =>
                //    {
                //        if (wv.Address == e.Url && e.Url != BlankPage && canDone)
                //        {
                //            done = true;
                //        }
                //        else if (wv.Address == e.Url && e.Url == BlankPage && !canDone)
                //        {
                //            canDone = true;
                //            wv.Load(url.AbsoluteUri);
                //        }
                //    }
                //);

                //EventHandler<LoadingStateChangedEventArgs> LoadingStateChange = new EventHandler<LoadingStateChangedEventArgs>(
                //    (s,e) =>
                //    {
                //        if (e.IsLoading)
                //            return;

                //        string addr = string.Empty;
                //        Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                //            new Action(() =>
                //            {
                //                addr = wv.Address;
                //            }));

                //        if (addr != BlankPage && canDone)
                //        {
                //            done = true;
                //        }
                //        else if (addr == BlankPage && !canDone)
                //        {
                //            canDone = true;
                //            wv.Load(url.AbsoluteUri);
                //            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                //                new Action(() =>
                //                {
                //                    wv.Load(url.AbsoluteUri);
                //                    //wv.Address = url.AbsoluteUri;
                //                }));
                //            //wv.Load(url.AbsoluteUri);
                //        }
                //    }

                //    );

                //#endregion

                //Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
                //    new Action(() =>
                //    {
                //        try
                //        {
                //            //wv = GetWebView();
                //            wv = new CefSharp.OffScreen.ChromiumWebBrowser();
                //            //PutControl(wv);
                //            //wv.IsBrowserInitializedChanged += BrowserInitialized;
                //            wv.FrameLoadEnd += FrameLoadEndEvent;
                //            //wv.FrameLoadStart += LoadStartEvent;
                //            wv.Load(BlankPage);
                //        }
                //        catch (Exception ex)
                //        {
                //            Helpers.Log.Add(Helpers.Log.GetExceptionText(ex, "SiteManagerCHR.Navigate().#Activate"));
                //        }
                //    }));

                //#endregion

                //#region Wait (done and pause) or 30 sec
                //DateTime endTime = DateTime.Now.AddSeconds(30);
                //while (!done && (DateTime.Now < endTime))
                //    Thread.Sleep(100);

                //if (done)
                //    Wait(wait);

                //#endregion

                //#region Disconect
                ////Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                ////    new Action(() =>
                ////    {
                ////        wv.PropertyChanged -= BrowserInitialized;
                ////        wv.Stop();
                ////    }));

                ////#region Wait (done) or 5 sec
                ////endTime = DateTime.Now.AddSeconds(5);
                ////while (!done && (DateTime.Now < endTime))
                ////    Thread.Sleep(100);

                ////#endregion

                //#endregion

                //#region GetHTML

                //Task<JavascriptResponse> resTask = null;
                //Task<string> resTask2 = null;

                //Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                //    new Action(() =>
                //    {
                //        if (wv != null)
                //            try
                //            {
                //                wv.LoadingStateChanged -= LoadingStateChange;
                //                wv.FrameLoadEnd -= FrameLoadEndEvent;
                //                if (done)
                //                {
                //                    result.ResponseUri = new Uri(wv.Address);
                //                    resTask2 = wv.GetSourceAsync();
                //                    //wv.Content
                //                    //wv.GetMainFrame().EvaluateScript("123");
                //                    //resTask = wv.EvaluateScriptAsync(@"document.getElementsByTagName ('html')[0].innerHTML");
                //                    //if (evalScriptTask.Status == TaskStatus.RanToCompletion)
                //                    //{
                //                    //    var evalScript = wv.EvaluateScriptAsync(@"document.getElementsByTagName ('html')[0].innerHTML").Result;
                //                    //    result.Content = evalScript?.Result?.ToString() ?? string.Empty;
                //                    //}
                //                }
                //            }
                //            catch (Exception ex)
                //            {
                //                Helpers.Log.Add(Helpers.Log.GetExceptionText(ex, "SiteManagerCHR.Navigate().GetHTML()"));
                //            }
                //            finally
                //            {
                //                ReturnWebView(wv);
                //            }
                //    }));

                //if (resTask2 != null)
                //{
                //    resTask2.Wait();
                //    result.Content = resTask2.Result;
                //}
                ////resTask.Wait((int)new TimeSpan(0,0,5).TotalMilliseconds);
                ////result.Content = resTask?.Result?.ToString() ?? string.Empty;

                //#endregion
                //return result;
            }