Cefsharp Offscreen EvaluateScriptAsync

时间:2018-02-22 06:35:54

标签: c# cefsharp cefsharp.offscreen

我使用的是Cefsharp Winforms,最近我一直试图切换到Offscreen。一切正常,但现在我的代码在返回页面源之前不会等待EvaluateScriptAsync完成。

或许我只是不太明白这项任务是如何运作的。到目前为止,这是我的进展:

private static void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
{
    var browser = (CefSharp.OffScreen.ChromiumWebBrowser)sender;

    if (e.Frame.IsMain)
    {
        browser.FrameLoadEnd -= WebBrowserFrameLoadEnded;

        var x = browser.EvaluateScriptAsync("/* some javascript codes */");

        if (x.IsCompleted && x.Result.Success)
        {
            x.ContinueWith(a =>
            {
                var task = browser.GetSourceAsync();

                task.ContinueWith(d =>
                {
                    if (d.IsCompleted)
                    {
                        globalRtnVal = d.Result;
                    }
                }).ConfigureAwait(false);
            });
        }
    }
}

我的主要代码是这样的:

/* some codes */

CefSharp.OffScreen.ChromiumWebBrowser asd = new CefSharp.OffScreen.ChromiumWebBrowser(/* url */);
asd.BrowserSettings.Javascript = CefSharp.CefState.Enabled;
asd.BrowserSettings.WebSecurity = CefSharp.CefState.Disabled;

asd.FrameLoadEnd += WebBrowserFrameLoadEnded;

int tryCount = 0;

do
{
    Thread.Sleep(3000);

    RtnHtml = globalRtnVal;

    if (String.IsNullOrEmpty(RtnHtml))
        tryCount++;

    if (tryCount == 10 && String.IsNullOrEmpty(RtnHtml))
    {
        /* some codes */
        return null;
    }
}
while (String.IsNullOrEmpty(RtnHtml));

/* some codes */

0 个答案:

没有答案