Search code examples
javascriptc#cefsharpchromium-embedded

How to get JavaScript output as string in CefSharp?


I am trying to get the output of this javascript string but so far what I have tried did not work

My current code is this. response is blank what I want is 4

var task = chromiumWebBrowser1.EvaluateScriptAsync("2+2");
            var response = task.Result;
            MessageBox.Show(String.Format("output", response));

Any help would be great.


Solution

  • The EvaluateScriptAsync method returns Task<JavascriptResponse>. You can await the Task to obtain the JavascriptResponse object then get the Result or error.

    JavascriptResponse response = await chromiumWebBrowser.EvaluateScriptAsync("1 + 1");
    
    if (response.Success)
    {
        var onePlusOne = (int)response.Result;
    }
    else
    {
        var error = response.Message; 
    }