15

I've scoured the internet looking for an answer, but I must be asking the wrong question. I have a C# winforms app with an Awesomium web control. Am I able to call methods defined in the app from javascript in the page that loads? If so, how? (sample js code would be greatly appreciated). Thanks!

Daniel Szabo
  • 7,181
  • 6
  • 48
  • 65

3 Answers3

32

The approach depends on which version of Awesomium you're using. There's been a bit of a change of how this is done in the upcoming version 1.7 (currently at 1.7 RC3) and how it was done before. Note that there's one improvement in 1.7, in that .NET methods can now return values when JS calls a method on your app. I don't believe this was possible prior to 1.7.

Here are the two methods:

test.html (used for both versions)

...
<script type="text/javascript">

    function myMethod() {
        document.write("In myMethod, calling .NET but expecting no return value.<br/>");

        jsobject.callNETNoReturn();
    }

    function myMethodExpectingReturn() {
        document.write("In myMethodExpectingReturn, calling .NET and expecting return value.<br/>");

        var returnVal2 = jsobject.callNETWithReturn("foo");
        document.write("Got value from .NET: " + returnVal2 + "<br/>");
    }

    function myMethodProvidingReturn(whatToReturn) {
        var returnVal =  whatToReturn + "bar";
        document.write("Returning '" + returnVal + "' to .NET.");

        return returnVal;
    }
</script>
...

Version 1.7

Form1.cs

public Form1()
{
    InitializeComponent();

    //webView is an instance of WebControl defined in your form
    webView.DocumentReady += WebViewOnDocumentReady;

    webView.Source = new Uri("test.html");
}

private void WebViewOnDocumentReady(object sender, UrlEventArgs urlEventArgs)
{
    webView.DocumentReady -= WebViewOnDocumentReady;

    JSObject jsobject = webView.CreateGlobalJavascriptObject("jsobject");

    jsobject.Bind("callNETNoReturn", false, JSHandler);
    jsobject.Bind("callNETWithReturn", true, JSHandler);

    webView.ExecuteJavascript("myMethod()");
    webView.ExecuteJavascript("myMethodExpectingReturn()");
    var result = webView.ExecuteJavascriptWithResult("myMethodProvidingReturn('foo')");
    Console.WriteLine(result.ToString());
}

private void JSHandler(object sender, JavascriptMethodEventArgs args)
{
    if (args.MustReturnValue)
    {
        Console.WriteLine("Got method call with return request");
        args.Result = "Returning " + args.Arguments[0];
    }
    else
    {
        Console.WriteLine("Got method call with no return request");
    }
}

Version 1.6

Form.cs

public Form1()
{
    InitializeComponent();

    //webView is an instance of WebControl defined in your form
    webView.DomReady += WebViewOnDomReady;

    webView.Source = new Uri("test.html");
}

private void WebViewOnDomReady(object sender, EventArgs eventArgs)
{
    webView.DomReady -= WebViewOnDomReady;

    webView.CreateObject("jsobject");
    webView.SetObjectCallback("jsobject", "callNETNoReturn", JSHandler);

    webView.ExecuteJavascript("myMethod()");

    var result = webView.ExecuteJavascriptWithResult("myMethodProvidingReturn('foo')");
    Console.WriteLine(result.ToString());
}

private void JSHandler(object sender, JSCallbackEventArgs args)
{
    Console.WriteLine("Got method call with no return request");
}
HotN
  • 4,216
  • 3
  • 40
  • 51
  • Bind is now deprecated. Can we update this answer to reflect newer versions? – Dagrooms Jun 03 '15 at 13:38
  • 1
    @Dagrooms, I no longer use Awesomium. I have switched to an open source alternative, CEFSharp (github.com/cefsharp/CefSharp). First, it's free for all commercial use, unlike Awesomium. More importantly, Awesomium's current version (1.7.5) runs on Chromium 18 and scores poorly on HTML5 tests (335 of 555 on html5test.com). CEFSharp runs on Chromium 39 and scores much better on HTML5 tests (470 of 555 on html5test.com). For the benefit of those still using Awesomium, please feel free to edit my answer or post your own. – HotN Jun 03 '15 at 14:11
2

In C++: (the .NET bindings will likely be similar)

Define a callback class:

class TestListener : public Awesomium::WebViewListener {
public:
    virtual void onCallback(
        Awesomium::WebView* caller,
        const std::wstring& objectName,
        const std::wstring& callbackName,
        const Awesomium::JSArguments& args
    ) {
        if (objectName == L"myApi" && callbackName == L"doMagicFoo") {
            cout << "callback called with " << args.size() << " args\n";
        }
    }

    //...implement all the other pure virtual functions...  
};

Then as you set up your WebView:

TestListener bob;
webView->setListener(&bob);
webView->createObject(L"myApi");
webView->setObjectCallback(L"myApi", L"doMagicFoo");

Then in your HTML/JS:

<button onclick="myApi.doMagicFoo('super', 45)">do native call</button>
sirbrialliance
  • 3,612
  • 1
  • 25
  • 15
0

For newer versions

Calling from JavaScript:

webControl1.LoadingFrameComplete += LoadingFramecompleted;

public void LoadingFramecompleted(object sender, FrameEventArgs e){
    //after loading complete create global object
    JSObject obj = webControl1.CreateGlobalJavascriptObject("jsobject");
    obj.Bind(myMethod);
}

private JSValue myMethod(object sender, JavascriptMethodEventArgs e)
{
    MessageBox.Show("hello world");
    return "My response";
}

Inside JavaScript

jsobject.myMethod(); //myMethod is the method name defined in c#

To Call JavaScript

webControl1.ExecuteJavascript("SayHello()");
PJ3
  • 3,870
  • 1
  • 30
  • 34