This project is read-only.

Javascript communication?

Nov 19, 2010 at 6:14 AM

How to call a Javascript method that is already present on a page shown in an Awesomium view? Is it possible?

How about calling a c# method from Javascript? Is it possible?

Nov 19, 2010 at 4:47 PM
Edited Nov 19, 2010 at 4:48 PM

Suppose you have this page:

<html>
<head>
<script type="text/javascript">
    var text="";
    function displayMessage(text) { 
        en = document.getElementById("text");
        en.innerHTML = text;
    }
</script>
</head>
<body>
<p id="text"></p>
</body>
</html>

 

The easiest way to call displayMessage is:

webView.ExecuteJavaScript("displayMessage('Hello')");

 

If you need more control over the parameters:

using System.Collections.ObjectModel;

Collection<JSValue> parameters = new Collection<JSValue>();
parameters.Add(new JSValue("Hello"));
webView.CallJavaScriptFunction("", "displayMessage", parameters);

 

In the next release (probably 1.6 final), this will also work:

webView.CallJavaScriptFunction("", "displayMessage", new JSValue[]{new JSValue("Hello")});

 

 

To call C# from JavaScript, set up the Callback handler and create an object through which to receive calls:

webView.Callback += new EventHandler<CallbackEventArgs>(webView_Callback);

webView.CreateObject("client");
webView.SetObjectCallback("client", "message");

void webView_Callback(object sender, CallbackEventArgs e)
{
    if (e.CallbackName == "message")
        System.Windows.Forms.MessageBox.Show(e.Args[0].ToString());
}

 

And in JavaScript:

<html>
<head>
<script type="text/javascript">
    client.message("Hello from JS")
</script>
</head>
<body></body>
</html>

 

 

I hope this helps.

Dec 27, 2010 at 5:46 PM
Edited Dec 27, 2010 at 6:12 PM

Hey again!

I seem to get an exception with the latest checkout (55067) when the wrapper handles a callback (this is with no arguments but the error persists when they are specified).  It may be related to this issue: http://awesomiumdotnet.codeplex.com/workitem/5010.

System.AccessViolationException was unhandled
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=AwesomiumDotNet
  StackTrace:
       at Awesomium.JSValue.{ctor}(JSValue* , JSValue* )
       at AwesomiumDotNet.WebViewListenerWrapper.onCallback(WebViewListenerWrapper* , WebView* caller, basic_string\,std::allocator >* objectName, basic_string\,std::allocator >* callbackName, vector >* args)
       at Awesomium.WebCore.update(WebCore* )
       at AwesomiumDotNet.WebCore.Update()
       at libInterface.Resources.Chromium.WebCoreShared.tryUpdateCore() in C:\src\libInterface\Resources\Chromium\WebCoreShared.cs:line 136
       at libInterface.UIBrowser.render(Device pDevice, Sprite pSprite, Double fAppTime, Single fElapsedTime) in C:\src\libInterface\UIBrowser.cs:line 897
       at libInterface.UIPane.render(Device pDevice, Sprite pSprite, Double fAppTime, Single fElapsedTime) in C:\src\libInterface\UIPane.cs:line 448
 
Sometimes it is a SEHException, but the story is the same:
System.Runtime.InteropServices.SEHException was unhandled
  Message=External component has thrown an exception.
  Source=AwesomiumDotNet
  ErrorCode=-2147467259
  StackTrace:
       at Awesomium.JSValue.{ctor}(JSValue* , JSValue* )
       at AwesomiumDotNet.WebViewListenerWrapper.onCallback(WebViewListenerWrapper* , WebView* caller, basic_string\,std::allocator >* objectName, basic_string\,std::allocator >* callbackName, vector >* args)
       at Awesomium.WebCore.update(WebCore* )
       at AwesomiumDotNet.WebCore.Update()

Here is a screenshot with a few of the values open:

Image and video hosting by TinyPic

 

Hope this is some help... I'm kinda stumped on this one - can't tell if it's awesomium being bad or something else..

Best,

John

Dec 27, 2010 at 8:07 PM

It might be another incompatibility between Awesomium compiled with MSVC9 and AwesomiumDotNet compiled with MSVC10. The internal representation of classes, strings, etc. is not guaranteed to be the same between each version of the compiler. There seem to be all sorts of problems with AwesomiumDotNet in MSVC10, so I suppose this is the case.

To keep track of when Chromium might be compiled for MSVC10, this is probably the best place to look: http://code.google.com/p/chromium/issues/detail?id=25628

Dec 27, 2010 at 9:38 PM

Cheers for the quick reply!

 

So it seems to be a fairly big problem lol.  Would your P/Invoke wrapper work around this?  And while I'm not mega experienced with C++, do you think that if I compiled AwesomiumDotNet with the v90 toolset it might stand a better chance?   (Unless you already did and I managed to miss a RC2 v90 binary knocking around somewhere? :-) )

Dec 27, 2010 at 11:08 PM

I haven't got the P/Invoke version working yet, so I don't know if that will help.

But I just tried the VS2008 version of RC2 in VS2010 and it seems to work if I add an App.config file to the C# project:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
    <requiredRuntime version="v4.0.20506"/>
  </startup>
</configuration>
I've been putting off the VS2010 problem for until Awesomium 1.6 is released, so hopefully this is a working fix until then.

Dec 28, 2010 at 1:45 AM

I am pretty sure the P/Invoke wrapper would get round it.   Do you need a hand making it?

I've already got the useLegacyV2RuntimeActivationPolicy flag enabled but sadly it is making no difference.   I'll try to get hold of VS2008 and build AwesomiumDotNet and see if it helps.  Will keep you posted... sadly looks like I'm gonna have to go back to running 1.5 .. bum!

 

Jan 11, 2011 at 6:06 PM

Thank you for the reply a while ago, and with the useLegacyV2RuntimeActivationPolicy flag enabled I'm able to successfully call Javascript from c# as you suggest in your example, but sadly calling c# methods from javascript crashes as described by John above.

Any workarounds for this yet? (I'm forced to using vs2010 for my app..)

Thanks

Jan 12, 2011 at 11:42 AM

John, if you want to help with the P/Invoke wrapper, then that would be great, because I can't seem to get that thing to work. Eventually we should move to P/Invoke anyway to support Linux/Mono. If you need a branch to work on, then I can make you a contributor.

Sinsro, I'll be more active in fixing issues once 1.6 is released, otherwise I'll spend too much time trying to figure out if I'm dealing with Awesomium bugs or AwesomiumDotNet bugs.

Jan 12, 2011 at 5:03 PM

Thank you for your quick reply, and no worries, I am just using a workaround where I collect all client calls to my c# program in a string which is later polled through a remote Javascript call. Not as elegant, but it does the trick. :)