[csharp] CefSharp hints

Costas

Administrator
Staff member
reference
http://github.com/cefsharp/CefSharp/wiki/Trouble-Shooting
http://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#CallJS
framework requirements per version

.NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework

This project contains .NET CLR bindings for The Chromium Embedded Framework (CEF) by Marshall A. Greenblatt. A small Core of the bindings are written in C++/CLI but the majority of code here is C#. It can of course be used from any CLR language, e.g. C# or VB.

http://github.com/cefsharp/CefSharp

to use CEFv3 - VC++ 2012 runtimes required download
your first app

JavaScript:
        //the browser
        //when CefSharpv1 - private readonly WebView web_view;
        private readonly ChromiumWebBrowser web_view; //CefSharpv3

        public Form1()
        {
            InitializeComponent();

            //when CefSharpv1 - web_view = new WebView("http://x.com/", new BrowserSettings());

            //required when CefSharpv3
            Cef.Initialize(new CefSettings()); //(CefSharp.Core required)
            web_view = new ChromiumWebBrowser("http://x.com/api/"); //CefSharpv3

            web_view.Dock = DockStyle.Fill;
            this.Controls.Add(web_view);
        }


how to read the cookies

JavaScript:
//more https://github.com/cefsharp/CefSharp/issues/826
.
.
//somewhere in form
            //when CefSharpv1 - CEF.VisitAllCookies(new webview_cookies());
            Cef.VisitAllCookies(new webview_cookies()); //CefSharpv3 (CefSharp.Core required)
.
.
    class webview_cookies : ICookieVisitor
    {
        public bool Visit(Cookie cookie, int count, int total, ref bool deleteCookie)
        {
            //here you can store the cookie value to a static class

            Console.WriteLine(cookie.Name +" = " +  cookie.Value);

            return true;
        }
    }


how to disable browser context menu (aka disable view source)

JavaScript:
        public Form1()
        {
            InitializeComponent();
.
.//init browser
.
            web_view.MenuHandler = new  webview_menu();
        }

    class webview_menu : IMenuHandler
    {
        public bool OnBeforeMenu(IWebBrowser browser)
        {
            return true;
        }
    }


Redirect user to another URL by code

JavaScript:
        //where the browser named
        private readonly WebView web_view;

        //on a button
        web_view.Load("https://pipiscrew.com/");

        //or load html via :
        web_view.LoadHtml("[B]Please wait!</b>");

        //reload page and ignore cache
        web_view.Reload(true);


sniffing url + autofill elements via Javascript on the fly

JavaScript:
        public Form1()
        {
            InitializeComponent();

            Cef.Initialize(new CefSettings());

            web_view = new ChromiumWebBrowser("http://x.com/");
            web_view.FrameLoadEnd += new EventHandler<FrameLoadEndEventArgs>(web_view_FrameLoadEnd); //when CefSharpv1 -- .LoadCompleted += new LoadCompletedEventHandler(web_view_LoadCompleted);
            web_view.Dock = DockStyle.Fill;
           
            panel1.Controls.Add(web_view);
        }

        void web_view_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
            if (e.Url.ToLower().StartsWith("https://www.x.com/login.php?skip_api_login"))
            {
                web_view.ExecuteScriptAsync("document.getElementById('email').value=" + '\'' + General.login_name + '\'');
                web_view.ExecuteScriptAsync("document.getElementById('pass').value=" + '\'' + General.login_password + '\'');
                web_view.ExecuteScriptAsync("document.getElementById('login_form').submit();");
                //web_view.ExecuteScript("document.getElementsByName('login').click()");
            }
        }


Browser Settings - change useragent / use cache / store cookies (CefSharp3 only)

JavaScript:
        //reference - https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/CefExample.cs

        //the browser
        private readonly ChromiumWebBrowser web_view;
        private readonly string cache_dir = Application.StartupPath + "\\tmp";

        public Form1()
        {
            InitializeComponent();

            Directory.CreateDirectory(cache_dir);

////
            var settings = new CefSettings();
            settings.UserAgent = "pipiscrew_browser_v" + Cef.CefSharpVersion;

            //the location where cache data will be stored on disk. If empty an in-memory cache will be used
            //http://magpcss.org/ceforum/apidocs3/projects/%28default%29/_cef_settings_t.html#cache_path       
            settings.CachePath = Application.StartupPath;

            //enable store cookies - method1
            //To persist session cookies (cookies without an expiry date or validity interval)
            settings.CefCommandLineArgs.Add("persist_session_cookies", "1");

            Cef.Initialize(settings);

            //enable store cookies - method2
            //To persist session cookies (cookies without an expiry date or validity interval)
            //Cef.SetCookiePath(cache_dir, true);
////
            web_view = new ChromiumWebBrowser("http://x.com/");
            web_view.Dock = DockStyle.Fill;
        }


How to download file ?

JavaScript:
.
.
web_view = new ChromiumWebBrowser("http://x.com/");
web_view.DownloadHandler = new DownloadHandler();
.
.
    //used to download file - https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/DownloadHandler.cs
    public class DownloadHandler : IDownloadHandler
    {
        public bool OnBeforeDownload(DownloadItem downloadItem, out string downloadPath, out bool showDialog)
        {
            downloadPath = downloadItem.SuggestedFileName;
            showDialog = true;

            return true;
        }

        public bool OnDownloadUpdated(DownloadItem downloadItem)
        {
            return false;
        }
    }


From JS call NET function

JavaScript:
// tested&working with v39 - src - https://stackoverflow.com/a/23425179/1320686
// for newer versions - https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions#CallJS
web_view.RegisterJsObject("callbackObj", new CallbackObjectForJs());

.
.
.

public class CallbackObjectForJs
{
    public void showMessage(string msg)
    {
        MessageBox.Show(msg);
    }
}

.
.
.

void web_view_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
    Console.WriteLine(e.Url.ToLower());
    if (e.Url.ToLower().StartsWith("https://www.x.com/login.php?skip_api_login"))
    {
        //inject your javascript
        web_view.ExecuteScriptAsync(x.Properties.Resources.script);
    }
}

.
.
.

//x.Properties.Resources.script
<script type="text/javascript">
    callbackObj.showMessage('message from js');
</script >

/////////////////////// 2020 - CefSharp v79.x ////////////////////

//src - https://github.com/cefsharp/CefSharp/issues/2990
CefSharpSettings.LegacyJavascriptBindingEnabled = true;
CefSharpSettings.WcfEnabled = true;
.
.
web_view.JavascriptObjectRepository.Register("callbackObj", new CallbackObjectForJs(), isAsync: false, options: BindingOptions.DefaultBinder);

snap5061.png




snap507.png




as for cefsharp.common.79.1.360, downloading nuget packages, rename it to .zip, inside CefSharp folder is precompiled the dlls
<code>https://www.nuget.org/packages/CefSharp.WinForms/
https://www.nuget.org/packages/CefSharp.Common/
https://www.nuget.org/packages/cef.redist.x64/</code>

v79.x sample :
JavaScript:
CefSettings settings = new CefSettings();
settings.UserAgent = General.browser_agent;

settings.CachePath = Application.StartupPath + "\\cache\\";
Directory.CreateDirectory(cache_dir);

settings.PersistSessionCookies = true;

//JS
CefSharpSettings.LegacyJavascriptBindingEnabled = true;
CefSharpSettings.WcfEnabled = true;


//Initialize
Cef.Initialize(settings);

web_view = new ChromiumWebBrowser(General.url);
web_view.FrameLoadEnd += new EventHandler<FrameLoadEndEventArgs>(web_view_FrameLoadEnd);
web_view.Dock = DockStyle.Fill;

//JS to NET - https://github.com/cefsharp/CefSharp/issues/2990
web_view.JavascriptObjectRepository.Register("callbackObj", new CallbackObjectForJs(), isAsync: false, options: BindingOptions.DefaultBinder);

panel1.Controls.Add(web_view);



public class CallbackObjectForJs
{
    public void showMessage(string msg)
    {
        MessageBox.Show(msg);       
    }
}



Today tested EO.Web v2020.1.45 (dependencies = 4 files / 74mb),

EO.WebBrowser dynamically create child processes and run browser engine inside the child process at runtime. By default, Windows system file rundll32.exe is used to create the child processes.

here are some snippets :
JavaScript:
//set cache - https://www.essentialobjects.com/forum/postsm46061_Cache-Path-solved.aspx
EO.WebBrowser.Runtime.DefaultEngineOptions.CachePath = Application.StartupPath + @"\cache";

webControl1.WebView.Url = "https://www.google.com";

//keyboard hook
webView1.Command += new CommandHandler(WebView_Command);
webView1.Shortcuts = new EO.WebBrowser.Shortcut[]
{
    new EO.WebBrowser.Shortcut(12, EO.WebBrowser.KeyCode.F12, false, false, false),
};

//register JS to .NET handler - https://www.essentialobjects.com/Products/WebBrowser/JSExtension.aspx
webView1.RegisterJSExtensionFunction("callbackObj", new JSExtInvokeHandler(callbackJS));


private void callbackJS(object sender, JSExtInvokeArgs e)
{
    if (e.Arguments!=null)
        your_function(e.Arguments[0].ToString());
}

//keyboard hook
private void WebView_Command(object sender, CommandEventArgs e)
{
    if (e.CommandId == 12)
        EO.WebBrowser.WebView.ShowDebugUI();
}

//inject any JS once the page load completed
private void webView1_LoadCompleted(object sender, EO.WebBrowser.LoadCompletedEventArgs e)
{
    //https://www.essentialobjects.com/doc/webbrowser/advanced/js.aspx
    webView1.EvalScript("alert('hi');");
}



Today tested Mozilla.Geckofx60.64 v60.0.50

JavaScript:
Xpcom.Initialize(Application.StartupPath + "\\xulrunner"); //x.nupkg\content\Firefox content, copy to app\xulrunner\
Console.Write(Xpcom.ProfileDirectory);
var geckoWebBrowser = new GeckoWebBrowser { Dock = DockStyle.Fill };

this.Controls.Add(geckoWebBrowser);
geckoWebBrowser.Navigate("http://pipiscrew.com/");

/*
howto - https://stackoverflow.com/a/33716554
profile directory - XpCom.ProfileDirectory - https://stackoverflow.com/a/52239283 + https://stackoverflow.com/a/20614986
cookies - https://stackoverflow.com/a/24762756
*/

OR ChromeDriver
OR Xilium.CefGlue, CefSharp implementations of embedded Chromium (CEF) without WinForms or WPF, but can be extended to use WinForms or WPF. Chromely uses Windows, Linux and MacOS native GUI API as "thin" chromium hosts.



How to get html page content before rendered in CEFSharp with Response Filtering

JavaScript:
https://stackoverflow.com/questions/42435004/how-to-get-html-page-content-before-rendered-in-cefsharp

https://stackoverflow.com/questions/45816851/using-cefsharp-to-capture-resource-response-data-body

https://github.com/cefsharp/CefSharp/issues/2433

https://github.com/cefsharp/CefSharp/wiki/General-Usage#response-filtering

https://github.com/cefsharp/CefSharp/search?q=responsefilter

https://github.com/cefsharp/CefSharp/search?q=ExampleResourceRequestHandler


https://github.com/cefsharp/CefSharp/tree/4608924d5a0c676857720917550674b88bd7b701/CefSharp.Example

https://github.com/cefsharp/CefSharp/blob/4608924d5a0c676857720917550674b88bd7b701/CefSharp.Example/Handlers/ExampleRequestHandler.cs

https://github.com/cefsharp/CefSharp/blob/4608924d5a0c676857720917550674b88bd7b701/CefSharp.Example/Handlers/ExampleResourceRequestHandler.cs
 
Top