Javascript Callback support?

1,807 views
Skip to first unread message

Drew Shafer

unread,
Sep 2, 2012, 6:05:15 AM9/2/12
to cefs...@googlegroups.com
Hi, I've been using CefSharp for a new project and it looks like it's a great fit for my purposes.  One question - when calling a CLR function from Javascript code, is there API support for supplying a javascript callback function to execute when the call completes?  Assuming that it's not there yet (I couldn't find it...) would you accept a pull request that added this functionality?

Jack O'Connor

unread,
Sep 2, 2012, 1:11:00 PM9/2/12
to cefs...@googlegroups.com
I don't think there's currently a way to do this directly, but you can make it work using ExecuteScript (or EvaluateScript). That function takes a string of arbitrary JS code and runs it in the browser, which you can use to call back to global functions. If you want to pass arbitrary C# strings as arguments to your callback, you can avoid writing a lot of tricky escaping code by using a JSON serializer. Here's an example using Json.NET:

void CallJSFunction(WebView myWebView, string functionName, params object[] args)
{
    // Build the list of arguments
    StringBuilder argsList = new StringBuilder();
    bool firstArg = true;
    foreach (object arg in args)
    {
        if (!firstArg)
        {
            argsList.Append(",");
        }
        firstArg = false;
        argsList.Append(JsonConvert.SerializeObject(arg));
    }
 
    // We serialize the name as well, so that it's guaranteed to be
    // a string. This makes it impossible to do anything other than
    // call a global function.
    string script = String.Format(
        "window[{0}]({1});",
        JsonConvert.SerializeObject(functionName),
        argsList);
 
    myWebView.ExecuteScript(script);
}

NB: If your code is being called by JS, then it's probably not running in the UI thread. Don't call methods on the browser controls from any thread other than the UI thread! See BeginInvoke for how to do thread marshaling, if this issue is new to you.

(Anthony, maybe we should make these methods throw exceptions when called from a different thread, like many builtin Forms methods do? I'm not familiar with how WPF works with threads stuff though.)

On Sun, Sep 2, 2012 at 3:05 AM, Drew Shafer <m...@drewshafer.com> wrote:
Hi, I've been using CefSharp for a new project and it looks like it's a great fit for my purposes.  One question - when calling a CLR function from Javascript code, is there API support for supplying a javascript callback function to execute when the call completes?  Assuming that it's not there yet (I couldn't find it...) would you accept a pull request that added this functionality?

--
You received this message because you are subscribed to the Google Groups "CefSharp" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cefsharp/-/kFPYrz5q26kJ.
To post to this group, send email to cefs...@googlegroups.com.
To unsubscribe from this group, send email to cefsharp+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cefsharp?hl=en.

Drew Shafer

unread,
Sep 3, 2012, 9:05:52 PM9/3/12
to cefs...@googlegroups.com
Yeah, I realized I could do that, but it seemed a bit ugly.  I went ahead and created a branch to add this feature, because it seems pretty important for allowing idiomatic javascript code in CefSharp apps.  Pull request is at https://github.com/chillitom/CefSharp/pull/84

I added tests, and I'm pretty sure that the solution is good.  However, I am *not* a c++ expert (let alone c++/cli) - my bread-and-butter is C#.  I'd appreciate feedback on the changes if there is something I should have done differently.

-DS

Drew Shafer

unread,
Sep 4, 2012, 11:20:32 AM9/4/12
to cefs...@googlegroups.com
Deleted original pull request, created a new one at  https://github.com/chillitom/CefSharp/pull/86  (I'm sortof new to git, so I didn't realize that it's a really bad idea to submit a pull request from my master branch).

-DS

anthony taranto

unread,
Sep 4, 2012, 3:01:33 PM9/4/12
to cefs...@googlegroups.com
Thanks for the pull req, will discuss on github.

Nathan Sizemore

unread,
Feb 10, 2014, 8:12:12 PM2/10/14
to cefs...@googlegroups.com
Did this ever get implemented?

In the CEF binaries, there is a way to bind a callback using app.whatever in JS.  Would be cool to get this going in C# as well.  

Per Lundberg

unread,
Feb 11, 2014, 6:11:04 AM2/11/14
to cefs...@googlegroups.com
Hi,

What you can do is use the approach mentioned in the FAQ:


If you need stuff running on a background thread, you will have to create a thread yourself (e.g. using TPL Task.Factory.StartNew() or whatever approach you prefer). There has been talk about "implicit asynchrony" in e.g. https://github.com/cefsharp/CefSharp/pull/87, but I closed since I felt that it's implications were to grave to take on the (stable) CefSharp1 branch.

If you want to influence the decisions taken on CefSharp3 in this regard, this issue might be the best one to voice your opinion: https://github.com/cefsharp/CefSharp/issues/159
(We should probably split that issue into a couple of smaller ones, to make it more apparent what works and what not - EvaluateScript() and ExecuteScript() actually do work, to a certain extent, but the "bind my CLR object to make it accessible to JS" is the slightly harder part, which has unfortunately not been implemented yet as far as CefSharp3 is concerned.)

Best regards,
Per

Nathan Sizemore

unread,
Feb 11, 2014, 7:27:36 AM2/11/14
to cefs...@googlegroups.com
The link provided on Issue #159 gives me a 404...

From my experiments so far, ExecuteScript/EvaluateScript do not seem to fire until all processing in current thread has stopped.  This is actually my first C# project, so I'm not entirely sure what options are available to me in order to make it fire asynchronously?  I'm going to look more in C# libs and see if there is some type of simple AsyncTask available in .Net before I go ahead and pop out a new Thread.  
The reason being able to have callback binding is important to me, is JavaScript does not have a native way to set event listeners/callbacks on actual JavaScript Objects.  Event Handlers are only available for DOM Events.  So, if I register a C# Object in JS land, Whenever it's property changes, I would like to do something on the JavaScript side to fire for UI.  
I've managed to find a work around, and will be creating an example project this weekend and maybe a few Gists and will submit to you guys to post in the FAQs Docs section to help out others..

Nathan

Jørn Hansen

unread,
Feb 11, 2014, 9:06:15 AM2/11/14
to cefs...@googlegroups.com


On Tuesday, February 11, 2014 1:27:36 PM UTC+1, Nathan Sizemore wrote:
The link provided on Issue #159 gives me a 404...

Ah, then I'm not the only one having issues with the GitHub issues (sorry couldn't resist ;-) I'm experiencing the same.

On top of that it seems GitHub does not even think I'm human anymore. Here is an indication of what I'm seeing https://f.cloud.github.com/assets/766423/2136993/361e751e-931d-11e3-9eba-4c67079977c1.png (it looks like cashed values of the number of issues do not match with the number of open issues)

I have reported both problems via email to the human beings at GitHub. I will post here in the Google Group when I hear from them regarding the issues on the issues.

Regards,
JornH

Jørn Hansen

unread,
Feb 11, 2014, 10:43:21 AM2/11/14
to cefs...@googlegroups.com
Looks like we are back from 16 open issues to 28 again. As a sort of confirmation I can now see https://github.com/cefsharp/CefSharp/issues/159 mentioned by Nathan above again. Also I heard back from a kind human being at GitHub that my robot status has been cleared.

Regards,
JornH
Reply all
Reply to author
Forward
0 new messages