简体   繁体   中英

CefSharp KeyboardHandler and using EvaluateScriptAsync

I am using the CefSharp browser control in a Winforms project and when enter is hit on the keyboard I need to run a javascript script and get a value back. I am using an IKeyboardHandler and in the OnKeyEvent I know when the user has hit enter. In the OnKeyEvent when I use the ExecuteScriptAsyncWhenPageLoaded function of the browser to run a script that exists on the loaded page the script is run. When I use the EvaluateScriptAsync function, to run a script and get a value back, the javascript doesn't execute and therefore I never get a value back.

Here is my code:

public class KeyboardHandler : IKeyboardHandler
{
        public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
        {
            if (windowsKeyCode == 0x0D)
            {
                string script = @"(function(){
                                return 1 + 1;
                                })();";
                var task = browserControl.EvaluateScriptAsync(script);
                task.Wait();
                var response = task.Result; <-- **never gets here**
                return true;
            }
            return true;
        }
}

Any help and/or ideas greatly appreciated!

Update: I tried using ContinueWith as suggested by @amaitland (using his code from: https://gist.github.com/amaitland/7a41cc87b88dfcd30e0e ) as follows but I still don't get any value back. When the timeout expires the result is "not yet computed", and IsFaulted = false.

    public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
{
        bool result = false;
        if (windowsKeyCode == 0x0D)
        {
                string script = @"(function(){
                                                return 1 + 1;
                                                })();";
                EvaluateScript(script, TimeSpan.FromSeconds(30), browserControl);
                return true;
        }
        return result;
}
       public object EvaluateScript(string script, TimeSpan timeout, IWebBrowser browserControl)
{
        var browser = (CefSharp.WinForms.ChromiumWebBrowser)browserControl;
        object result = null;

        if (browser.IsBrowserInitialized && !browser.IsDisposed && !browser.Disposing)
        {
                var task = browser.EvaluateScriptAsync(script, timeout);
                var complete = task.ContinueWith(t =>
                {
                        if (!t.IsFaulted)
                        {
                                var response = t.Result;
                                result = response.Success ? (response.Result ?? "null") : response.Message;
                        }
                }, TaskScheduler.Default);
                //This caused the problem -> complete.Wait();

        }
        return result;
}

For anyone looking for hours like I was for a simple example of getting data back, this works:

string jsscript = @"function Foo(){ var data =11;return data} Foo();";
var task = browser.GetMainFrame().EvaluateScriptAsync(jsscript)
  .ContinueWith(t =>
  {
      var result = t.Result;
      string s = "";
  });

result.Result will equal 11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM