V8Context not created when passing "about:blank" to ChromiumWebBrowser's constructor
c#, cefsharp, chromium-embedded
Solution
You will have to use the following functions:
WebBrowser.GetMainFrame().ExecuteJavaScriptAsync(string code);
WebBrowser.GetMainFrame().EvaluateScriptAsync(string code);
This bypasses the regular check for `V8Context`.
More information from CefSharp, found here:
For frames that do not contain JavaScript then no V8Context will be created. Executing a script once the frame has loaded it's possible to create a V8Context.
You can use `browser.GetMainFrame().ExecuteJavaScriptAsync(script)` or `browser.GetMainFrame().EvaluateScriptAsync` to bypass these checks.
Problem
The steps to reproduce this are very simple. Just download the latest version of CefSharp.WinForms (57.0.0) with nuget, then add a button and this code to a form: ``` public partial class Form1 : Form { ChromiumWebBrowser WebBrowser; public Form1() { InitializeComponent(); WebBrowser = new ChromiumWebBrowser("about:blank"); panel1.Controls.Add(WebBrowser); WebBrowser.Dock = DockStyle.Fill; } private void testButton_Click(object sender, EventArgs e) { MessageBox.Show(WebBrowser.CanExecuteJavascriptInMainFrame.ToString()); } } ``` Then run the application, wait a few seconds to make sure "about:blank" has loaded and press the `testButton`. The message box will show `False`. In fact, if I attempt to use `EvaluateScriptAsync` I will get an exception telling me the context has not been created. One way to solve this is to call `ShowDevTools`, which seems to somehow force a context to be created. Another solution is to navigate to a non local page such as Google. In this case, even if I go back to "about:blank" I'll be able to run scripts. I tried using a custom scheme registered with `CefSettings.RegisterScheme`, but navigating to my custom page still does not create a context (I tried passing "about:blank" to the constructor and then navigating to my custom scheme and the other way around too and none worked). So, is it possible to have CefSharp create a context without having to navigate to a non-local page or to show DevTools?