abi*_*day 3 javascript c# cefsharp
举例来说,我正在使用 ExecuteJavaScriptAsync(..) 方法通过 cefSharp 运行脚本,并且在运行脚本时抛出错误,我如何检测它然后在我的 c# 程序中捕获它?
如果您需要评估返回值的代码,请使用 Task EvaluateScriptAsync(string script, TimeSpan? timeout) 方法。Javascript 代码是异步执行的,因此使用 .Net Task 类返回响应,其中包含错误消息、结果和成功 (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());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
449 次 |
| 最近记录: |