传递"about:blank"时未创建V8Context到ChromiumWebBrowser的构造函数 [英] V8Context not created when passing "about:blank" to ChromiumWebBrowser's constructor
问题描述
重现此步骤非常简单.只需使用nuget下载最新版本的CefSharp.WinForms(57.0.0),然后将一个按钮和以下代码添加到表单中即可:
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());
}
}
然后运行该应用程序,等待几秒钟以确保已加载"about:blank",然后按 testButton
.该消息框将显示 False
.实际上,如果我尝试使用 EvaluateScriptAsync
,我会得到一个异常,告诉我尚未创建上下文.
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.
解决此问题的一种方法是调用 ShowDevTools
,这似乎以某种方式强制创建了上下文.另一种解决方案是导航到非本地页面,例如Google.在这种情况下,即使我回到"about:blank",我也将能够运行脚本.我尝试使用在 CefSettings.RegisterScheme
中注册的自定义方案,但是导航到我的自定义页面仍然不会创建上下文(我尝试将"about:blank"传递给构造函数,然后导航到我的自定义方案而另一种方法也没有,但是没有任何效果.
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).
那么,有可能让CefSharp创建上下文而无需导航到非本地页面或显示DevTools吗?
So, is it possible to have CefSharp create a context without having to navigate to a non-local page or to show DevTools?
推荐答案
您将必须使用以下功能:
You will have to use the following functions:
WebBrowser.GetMainFrame().ExecuteJavaScriptAsync(string code);
WebBrowser.GetMainFrame().EvaluateScriptAsync(string code);
这会绕过 V8Context
的常规检查.
有关CefSharp的更多信息,请参见此处:
More information from CefSharp, found here:
对于不包含JavaScript的框架,则不会创建V8Context.框架加载后执行脚本即可创建V8Context.
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.
您可以使用 browser.GetMainFrame().ExecuteJavaScriptAsync(script)
或 browser.GetMainFrame().EvaluateScriptAsync
绕过这些检查.
You can use browser.GetMainFrame().ExecuteJavaScriptAsync(script)
or browser.GetMainFrame().EvaluateScriptAsync
to bypass these checks.
这篇关于传递"about:blank"时未创建V8Context到ChromiumWebBrowser的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!