0

I am trying to incorporate a browser into a console application using CefSharp Offscreen with dotnet core.

Is it possible to view the web page inside the Chromium browser this way?

In the code below (largely taken from their Offscreen github example) I am trying to display a simple HTML page (later it'll be React or Angular but for now HTML is fine).

In their code they l;oad up a webpage in Chromium and take a picture of it, I want to be able to view the webpae that it takes a screenshot of.

    public static void Start()
    {
        //Only required for PlatformTarget of AnyCPU
        AppDomain.CurrentDomain.AssemblyResolve += Resolver;
        Console.WriteLine("apply cef settings");
        CefSettings settings = new CefSettings();

        settings.RegisterScheme(new CefCustomScheme
        {
            SchemeName = "localfolder",
            SchemeHandlerFactory = new FolderSchemeHandlerFactory(
            rootFolder: @"..\..\..\..\html",
            defaultPage: "index.html"
        )
        });

        Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
        CefSharpSettings.ShutdownOnExit = true;
        Console.WriteLine("load web page");
        ChromeBrowser = new ChromiumWebBrowser("localfolder://cefsharp/");
        Console.ReadKey();
        Cef.Shutdown();
    }
PurpleSmurph
  • 2,055
  • 3
  • 32
  • 52
  • Are you wanting to see what's going on for debugging purposes? You can open DevTools or enable remote debugging and use Chrome to connect to the browser. – amaitland Mar 03 '21 at 19:26
  • Mainly debugging, yes, but don't know whar address to put in the address bar. – PurpleSmurph Mar 03 '21 at 19:55
  • You can open DevTools programatically https://github.com/cefsharp/CefSharp/wiki/Trouble-Shooting#javascript-debugging – amaitland Mar 03 '21 at 20:10
  • I am still struggling with this - is there much objecive difference between the backend of WPF and Offscreen? IE if I use WPF to display the browser whilst testing can I switch to using offscreen easily once I know CefSharp is rendering what I want? – PurpleSmurph Mar 04 '21 at 15:35
  • They are built on the same rendering engine. They both implement the same interface http://cefsharp.github.io/api/87.1.x/html/T_CefSharp_IWebBrowser.htm – amaitland Mar 04 '21 at 19:05
  • Thank you for that, will be my back up to use WPF. I can get the dev tools up now, but can't see the actual browser, I know it's there as I can screen shot it. I need to be able to make changes to input fields. Is `ChromeBrowser.EvaluateScriptAsync( "document.querySelector('[name=field1]').value = 'CefSharp Was Here!'");` the only/best way to target elements programmatically if that is the only option? – PurpleSmurph Mar 05 '21 at 10:12
  • The OffScreen version is basically headless, it's not intended to have a user interface, you can debug html/JavaScript using DevTools, you can view the DOM, even record what's going on and look at the image at a point in time. The OffScreen version is also largely obsolete with Chrome headless combined with puppeteer Sharp being in a lot of cases a better option. As for JavaScript any standards compliant code should work the same as in Chrome, I don't know what you are trying to achieve so cannot really comment. – amaitland Mar 05 '21 at 20:00
  • Sorry for the delayed response and thank you for your help. What I'm trying to achieve is to test out what the browser will show as in virtual reality. So perhaps winforms or WPF would show more how it'll look then use Offscreen for putting it into a VR world? Is Offscreen soon to be discontinued/not supported then? – PurpleSmurph Mar 08 '21 at 18:43
  • The WPF version uses the same rendering engine as the OffScreen version. Both are slower than the WinForms version. No plans to discontinue the OffScreen version. – amaitland Mar 08 '21 at 20:51
  • Thank you for your help, will use WinForms to test and Offscreen to put into the VR scene. – PurpleSmurph Mar 10 '21 at 09:39

0 Answers0