有什么方法可以在我的 c# 程序中捕获从 cefsharp 中的 javascript 抛出的错误吗?
Is there any way to catch an error thrown from javascript in cefsharp in my c# program?
举例来说,我是 运行 使用 ExecuteJavaScriptAsync(..) 方法通过 cefSharp 编写的脚本,并且在 运行 脚本时抛出错误,我如何检测它然后在我的 C# 程序中捕获它?
如果您需要计算 return 值的代码,请使用 Task EvaluateScriptAsync(string script, TimeSpan? timeout) 方法。 Javascript 代码异步执行,因此使用 .Net 任务 class 到 return 响应,其中包含错误消息、结果和成功 (bool) 标志。
// Get Document Height
var task = frame.EvaluateScriptAsync("(function() { var body = document.body, html = document.documentElement; return Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight ); })();", null);
task.ContinueWith(t =>
{
if (!t.IsFaulted)
{
var response = t.Result;
EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message;
}
}, TaskScheduler.FromCurrentSynchronizationContext());
举例来说,我是 运行 使用 ExecuteJavaScriptAsync(..) 方法通过 cefSharp 编写的脚本,并且在 运行 脚本时抛出错误,我如何检测它然后在我的 C# 程序中捕获它?
如果您需要计算 return 值的代码,请使用 Task EvaluateScriptAsync(string script, TimeSpan? timeout) 方法。 Javascript 代码异步执行,因此使用 .Net 任务 class 到 return 响应,其中包含错误消息、结果和成功 (bool) 标志。
// Get Document Height
var task = frame.EvaluateScriptAsync("(function() { var body = document.body, html = document.documentElement; return Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight ); })();", null);
task.ContinueWith(t =>
{
if (!t.IsFaulted)
{
var response = t.Result;
EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message;
}
}, TaskScheduler.FromCurrentSynchronizationContext());