Cefsharp a pit for C # calls to JavaScript

Source: Internet
Author: User

A previously developed product after the launch of a strange bug, after debugging found in the use of CEF control

Evaluatescriptasync

This method is not guaranteed to be successful.

After a query, found that the official document (Https://github.com/cefsharp/CefSharp/wiki/General-Usage#devtools) has such a paragraph

2. How does call a JavaScript method that returns a result?

If you need to evaluate code which returns a value, use the Task<JavascriptResponse> EvaluateScriptAsync(string script, TimeSpan? timeout) method. JavaScript code is executed asynchronously and as such uses the. Net Task class to return a response, which contains Erro R message, result and a success ( bool ) flag.

Get Document HeightVarTask =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}}, Taskscheduler.fromcurrentsynchronizationcontext ());     

For a more detailed example check out this Gist

Notes

    • Scripts is executed at the frame level, and every page have at least one frame ( MainFrame )
    • Only trivial values can is returned (like int, bool, string etc)-not a complex (user-defined) type which you have define D yourself. This is because there are no (easy) A-expose a random JavaScript object to the. NET world, at least not today. However, one possible technique is to turn the JavaScript object you wish to return to your. NET code into a JSON string w ith the JavaScript JSON.toStringify() method and return that string to your. NET code. Then you can decode this string into a. NET object with something like Json.NET. See this MSDN link for more information. (https://msdn.microsoft.com/en-us/library/ie/cc836459 (v=vs.94). aspx)

You can get the return value to JS in this way:

1 task. ContinueWith (t =2{3     if (!  t.isfaulted)4    {5         var response = T.result; 6         " NULL " ): Response. Message; 7     }8 }, Taskscheduler.fromcurrentsynchronizationcontext ());

So using this, I made a "water id".

Each time the call is 1, the other end of the code after execution, return the ID, if the call fails (or the returned ID is not recognized), then re-initiator, if the other end receives two consecutive IDs, then do not do processing (but still return the ID of the.).

Such a method can also be used to solve some packet loss problems.

Cefsharp a pit for C # calls to JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.