An offscreen instance of Chromium that you can use to take snapshots or evaluate JavaScript.
Inheritance: IRenderWebBrowser
示例#1
20
        public NewSkyBet(AllMarkets marktes)
        {
            var cef = Chrome.Instanse;
            isLoad = false;
            browser = Chrome.InstSkyBet;
            browser.ConsoleMessage += BrowserOnConsoleMessage;
            browser.LoadingStateChanged += BrowserOnLoadingStateChanged;
            browser.BrowserInitialized += BrowserOnBrowserInitialized; 
            allMarkets = marktes;

            State = ThreadState.Running;
            isIgnoredMarket = false;
        }
        public static void Main(string[] args)
        {
            const string testUrl = "https://www.google.com/";

            Console.WriteLine("This example application will load {0}, take a screenshot, and save it to your desktop.", testUrl);
            Console.WriteLine("You may see Chromium debugging output, please wait...");
            Console.WriteLine();

            //Perform dependency check to make sure all relevant resources are in our output directory.
            Cef.Initialize(new CefSettings(), shutdownOnProcessExit: true, performDependencyCheck: true);

            // Create the offscreen Chromium browser.
            browser = new ChromiumWebBrowser(testUrl);

            // An event that is fired when the first page is finished loading.
            // This returns to us from another thread.
            browser.LoadingStateChanged += BrowserLoadingStateChanged;

            // We have to wait for something, otherwise the process will exit too soon.
            Console.ReadKey();

            // Clean up Chromium objects.  You need to call this in your application otherwise
            // you will get a crash when closing.
            Cef.Shutdown();
        }
示例#3
0
        public WebBrowser(Arguments args)
            : this()
        {
            var address = args["address"].Value as string;

            DispatcherThread.StartNew(() =>
            {
                //TODO: Add ability to define browser settings.
                if (address != null)
                    m_webBrowser = new ChromiumWebBrowser(address);
                else
                    m_webBrowser = new ChromiumWebBrowser("");

            }).Wait();

            //Ensure that the web browser is initialized.
            using (var evt = new ManualResetEvent(false))
            {
                m_webBrowser.BrowserInitialized += (o, e) => evt.Set();

                DispatcherThread.StartNew(() =>
                {

                    if (m_webBrowser.IsBrowserInitialized)
                    {
                        evt.Set();
                    }
                });

                evt.WaitOne();
            }
        }
示例#4
0
        void CreateOffScreenBrowser()
        {
            BrowserInitializedEvent.Reset();
            OffScreenBrowser      = new CefSharp.OffScreen.ChromiumWebBrowser();  // Create the offscreen Chromium browser.
            OffScreenBrowser.Size = Screen.PrimaryScreen.Bounds.Size;             // be sure big enough for full screen display

            OffScreenBrowser.BrowserInitialized        += BrowserInitializedEventHandler;
            OffScreenBrowser.FrameLoadStart            += FrameLoadStartEventHandler;
            OffScreenBrowser.LoadingStateChanged       += BrowserLoadingStateChanged;
            OffScreenBrowser.AddressChanged            += AddressChangedEventHandler;
            OffScreenBrowser.StatusMessage             += StatusMessageEventHandler;
            OffScreenBrowser.ConsoleMessage            += ConsoleMessageEventHandler;
            OffScreenBrowser.FrameLoadEnd              += FrameLoadEndEventHandler;
            OffScreenBrowser.TitleChanged              += TitleChangedEventHandler;
            OffScreenBrowser.LoadError                 += LoadErrorEventHandler;
            OffScreenBrowser.JavascriptMessageReceived += JavascriptMessageReceivedEventHandler;
            OffScreenBrowser.Paint += OnPaintEventHandler;

            BrowserInitializedEvent.WaitOne();             // wait for initialization to complete

            if (Debug)
            {
                DebugLog.Message("Offscreen browser created");
            }
            return;
        }
示例#5
0
        public void StartOffscreenRender(string url)
        {
#if WITHCEF
            var settings = new CefSharp.OffScreen.CefSettings()
            {
                //By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
                CachePath = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache"),
            };

            // play with surfaces
            settings.SetOffScreenRenderingBestPerformanceArgs();

            //Perform dependency check to make sure all relevant resources are in our output directory.
            // Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);

            var rqs = new RequestContextSettings();
            rqs.CachePath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache");
            var rq = new RequestContext(rqs, null);

            var bs = new BrowserSettings();
            bs.BackgroundColor = 0xff000000;

            this.theOffscreenBrowser      = new CefSharp.OffScreen.ChromiumWebBrowser(url, bs, rq);
            this.theOffscreenBrowser.Size = new System.Drawing.Size(400, 800);
            this.theOffscreenBrowser.LoadingStateChanged += TheBrowser_LoadingStateChanged;
            this.theOffscreenBrowser.FrameLoadEnd        += TheOffscreenBrowser_FrameLoadEnd;
示例#6
0
        public void TakeScreenshotOffscreen(string url, string filename)
        {
            var offscreenBrowser = new CefSharp.OffScreen.ChromiumWebBrowser();

            offscreenBrowser.BrowserInitialized += (o, args) =>
            {
                offscreenBrowser.Load(url);
                offscreenBrowser.FrameLoadEnd += async(sender1, eventArgs) =>
                {
                    if (eventArgs.Frame.IsMain)
                    {
                        var sizes = await offscreenBrowser.EvaluateScriptAsync(JavascriptQueries.RequestForMaxSize);

                        var widthAndHeight = (List <object>)sizes.Result;

                        var screenshotSize = new System.Drawing.Size((int)widthAndHeight[0], (int)widthAndHeight[1]);
                        offscreenBrowser.Size = screenshotSize;

                        await Task.Delay(500);

                        var bitmap = await offscreenBrowser.ScreenshotAsync(false, PopupBlending.Main);

                        bitmap.Save(filename, ImageFormat.Png);
                        offscreenBrowser.Dispose();
                        Process.Start(new System.Diagnostics.ProcessStartInfo
                        {
                            UseShellExecute = true,
                            FileName        = filename
                        });
                    }
                };
            };
        }
示例#7
0
 public Bet365(AllMarkets markteMarkets)
 {
     Debug.WriteLine("Start browser2");
     var cef = Chrome.Instanse;
     isLoad = false;
     isIgnoredMarket = false;
     browser = Chrome.InstBet365;
     browser.ConsoleMessage += BrowserOnConsoleMessage;
     allMarkets = markteMarkets;
 }
示例#8
0
        public frmSetup()
        {
            InitializeComponent();
            b_login = new ChromiumWebBrowser(URL);
            b_login.FrameLoadEnd += B_login_FrameLoadEnd;
            OnAct = Act;
            connectionTimeout.Tick += ConnectionTimeout_Tick;

            //Form test = new Form();
            //test.Controls.Add(b_login);
            //test.Show();
        }
        public ChromiumHeadlessBrowserWithCefSharp(string url, int width, int height)
        {
            Cef.Initialize(new CefSettings());

            // Create the offscreen Chromium browser
            var settings = new BrowserSettings
                               {
                                   FileAccessFromFileUrlsAllowed = true,
                                   UniversalAccessFromFileUrlsAllowed = true,
                                   WebSecurityDisabled = true,
                               };

            this.browser = new ChromiumWebBrowser(url, settings);

            // Set the size of the browser
            this.browser.Size = new Size(width, height);

            // An event that is fired when the first page is finished loading.
            // This returns to us from another thread.
            this.browser.FrameLoadEnd += this.BrowserFrameLoadEnd;
            this.browser.ConsoleMessage += this.BrowserConsoleMessage;
        }
示例#10
0
文件: Crawler.cs 项目: Cossa6/truffle
        public Crawler()
        {
            OnPageInject = ThreadSafePageInject;
            OnCrawlRequest = ThreadSafeCrawlRequest;

            d_handler = new DownloadHandler();
            d_handler.DownloadHandled += D_handler_DownloadHandled;

            //Initialize the off-screen browser for crawling
            b_root = new ChromiumWebBrowser("local");
            b_root.BrowserSettings.ApplicationCache = CefState.Disabled; //Caching occasionally causes the crawler to miss new files
            b_root.BrowserSettings.ImageLoading = CefState.Disabled; //We don't want to waste our time/data with pictures :D
            b_root.DownloadHandler = d_handler;
            b_root.JsDialogHandler = new JsDialogHandler();
            b_root.FrameLoadEnd += b_root_FrameLoadEnd; //This allows us to inject JS in a timely manner
            OnSyncCompleted += Crawler_OnSyncCompleted;

            this.CreateHandle();
            Application.DoEvents();

            //Form test = new Form();
            //test.Controls.Add(b_root);
            //test.Show();
        }
示例#11
0
        private void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    if (m_webBrowser != null)
                    {
                        m_webBrowser.Dispose();
                        m_webBrowser = null;
                    }

                    if (DispatcherThread != null)
                    {
                        DispatcherThread.Dispose();
                        m_webBrowser = null;
                    }
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                disposedValue = true;
            }
        }
示例#12
0
 /// <summary>
 /// Dispose of this instance.
 /// </summary>
 public void Dispose()
 {
     browser      = null;
     BitmapBuffer = null;
     PopupBuffer  = null;
 }
示例#13
0
 public void Dispose()
 {
     browser = null;
 }
示例#14
0
 private static string MainAsync(ref string url, TimeSpan timeout)
 {
     var browserSettings = new BrowserSettings() { };
     using (var browser = new ChromiumWebBrowser(url, browserSettings: browserSettings)) //, requestcontext: requestContext
         return MainAsync(browser, ref url, timeout);
 }
示例#15
0
        private void reloadToMainPage()
        {
            if ((browser.IsLoading))
            {
                CheckLoad();
                return;
            }
            if (browser.IsBrowserInitialized)
                browser.Delete();
            isLoad = false;
            browser.Dispose();

            browser = Chrome.InstSkyBet;
            CheckLoad();
        }
示例#16
0
 public static ChromiumWebBrowser InitSkyBet(string URL)
 {
     skyBetChromiumWebBrowser.Dispose();
     skyBetChromiumWebBrowser = new ChromiumWebBrowser(URL);
     return skyBetChromiumWebBrowser;
 }
示例#17
-10
文件: Crawler.cs 项目: Cossa6/truffle
 /// <summary>
 /// Forces Special Consideration files to download immediately after they open in Adobe's chrome plugin.
 /// Ideally, I would disable the plugin - but for some reason doing this caused CefSharp to stall.
 /// If any CefSharp experts figure out a cleaner way to do this, let me know.
 /// </summary>
 /// <param name="b">The browser to inject into</param>
 private void specialConsiderationInject(ChromiumWebBrowser b)
 {
     string code = "document.body.innerHTML = \"<a id='d_link' href='" + current_page + "' download>Clickity</a>\"; document.querySelector('#d_link').click();";
     var task = b.EvaluateScriptAsync(code);
     task.ContinueWith(t => {
         //For debug ouput when testing
         if (!t.IsFaulted) {
             //Console.WriteLine("Special Consideration @ " + current_page);
         }
     }, TaskScheduler.FromCurrentSynchronizationContext());
 }
示例#18
-12
 public void LoadPageSkyBet(string Url)
 {
     browser.ConsoleMessage -= BrowserOnConsoleMessage;
     browser.LoadingStateChanged -= BrowserOnLoadingStateChanged;
     browser.BrowserInitialized -= BrowserOnBrowserInitialized;
     browser.Delete();
     browser.Dispose();
     browser = new ChromiumWebBrowser(Url);
     browser.ConsoleMessage += BrowserOnConsoleMessage;
     browser.LoadingStateChanged += BrowserOnLoadingStateChanged;
     browser.BrowserInitialized += BrowserOnBrowserInitialized;
     isLoad = false;
     //  browser.RegisterAsyncJsObject("Alljs", Properties.Resources.jsAllLoadSkybet);
     var alljs = Properties.Resources.jsAllLoadSkybet;
     var countUpdate = 0;
     while (true)
     {
         Thread.Sleep(200);
         countUpdate++;
         Application.DoEvents();
         if (!browser.IsLoading)
         {
             if (browser.IsBrowserInitialized)
             {
                 var sss = browser.EvaluateScriptAsync(alljs).Result;
                 var result = browser.EvaluateScriptAsync("checkLoad()").Result;
                 if ((result.Result != null) && ((bool) result.Result == true))
                 {
                     var ss = browser.EvaluateScriptAsync(alljs).Result;
                     break;
                 }
             }
         }
         if (countUpdate > 30)
         {
             if (browser.IsBrowserInitialized)
                 browser.Delete();
             browser.Dispose();
             browser = Chrome.InitSkyBet(Url);
             browser.ConsoleMessage += BrowserOnConsoleMessage;
             browser.LoadingStateChanged += BrowserOnLoadingStateChanged;
             browser.BrowserInitialized += BrowserOnBrowserInitialized;
             isLoad = false;
             countUpdate = 0;
         }
     }
 }
示例#19
-14
    public static int BrowserWidth(ChromiumWebBrowser b)
    {
        // Get Document Height
        var task = b.EvaluateScriptAsync("(function() { var body = document.body, html = document.documentElement; return  Math.max( body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth ); })();");

        task.ContinueWith(t =>
        {
            if (!t.IsFaulted)
            {
                var response = t.Result;
                var EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message;
                //MessageBox.Show(response.Result.ToString());
                //MessageBox.Show(EvaluateJavaScriptResult.ToString());
            }
        });

        if (task.Result.Result == null) { return 0; }
        return Convert.ToInt32(task.Result.Result.ToString());
    }
示例#20
-26
文件: Crawler.cs 项目: Cossa6/truffle
 /// <summary>
 /// Injects the Javascript code and processes the result.
 /// </summary>
 /// <param name="b">The ChromimumWebBrowser to inject into.</param>
 private void Inject(ChromiumWebBrowser b)
 {
     if (!emergency_stop) {
         //All clear. 99.99% of the time the above doesn't matter.
         var task = b.EvaluateScriptAsync(js_inject);
         task.ContinueWith(t => {
             if (!t.IsFaulted) {
                 var response = t.Result;
                 if (response.Success == true && response.Result != null) {
                     var result = response.Result.ToString();
                     if (result != string.Empty && result != "null" && result != "login successful" && result != "login unsuccessful") {
                         potentialLoginFails = 0; //No need to worry :-)
                         OnLoginSuccess?.Invoke(new object(), new EventArgs()); //Tell the main class it's all good
                         Dictionary<string, string> conversion = JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
                         foreach (var pair in conversion) {
                             int dummy = 0;
                             if (int.TryParse(pair.Key, out dummy)) {
                                 urls.Add(new CrawlableURL(current_unitcode, pair.Value));
                                 //Console.WriteLine("[* Discovered] [" + current_unitcode + "] " + pair.Value);
                             }
                             else {
                                 string u_code = pair.Key.Substring(0, 6);
                                 urls.Add(new CrawlableURL(u_code, pair.Value));
                                 Unit.EnsureExists(new Unit(u_code, pair.Value));
                                 //Console.WriteLine("[* Discovered] [" + pair.Key.Substring(0, 6) + "] " + pair.Value);
                             }
                         }
                         if (urls.Count > 0) {
                             if (OnCrawlingEvent != null)
                                 OnCrawlingEvent(new object(), new EventArgs());
                             Continue();
                         }
                     }
                     else if (result == "login successful" || result == "login unsuccessful") {
                         ++potentialLoginFails;
                         if (potentialLoginFails > potentialLoginFailsThreshold) {
                             //We might have a problem xD
                             emergency_stop = true;
                             b_root.Load("local"); //Park until everything is sorted out.
                             potentialLoginFails = 0;
                             if (OnLoginProblem != null)
                                 OnLoginProblem(new object(), new EventArgs());
                         }
                     }
                 }
             }
         }, TaskScheduler.FromCurrentSynchronizationContext());
     }
 }