4

I created a new WPF project, and added a Awesomium 1.6.3 WebControl to it.

Then, I added this code to MainWindow.xaml.cs:

    private void webControl1_Loaded(object sender, RoutedEventArgs e)
    {
        webControl1.LoadURL("https://www.google.com/");
    }

    private void webControl1_DomReady(object sender, EventArgs e)
    {
        var wc = new WebClient();
        webControl1.ExecuteJavascript(jQuery);
        webControl1.ExecuteJavascript(@"var __jq = jQuery.noConflict();");
        webControl1.ExecuteJavascript(@"alert(__jq);");
        using(var result = webControl1.ExecuteJavascriptWithResult(@"(function() { return 1; })();"))
        {
            MessageBox.Show(result.ToString());
        }
        //using (var result = webControl1.ExecuteJavascriptWithResult(@"(function() { return __jq('a'); })();"))
        //{
        //    MessageBox.Show(result.ToString());
        //}
    }

And it alerts "1" and then "function (a,b){...}", which is out of order, now that I think about it, but whatever, that's another issue.

As soon as I uncomment the bottom code, it alerts "1" and then hangs. Why? How can I can some information about the links on a page? Or reliably pass some information back to C#? Or get access to the DOM with C#?

Edit: jQuery is just a string containing the jQuery 1.7 code.

2 Answers 2

5

Regarding why the following line hangs:

webControl1.ExecuteJavascriptWithResult(@"(function() { return __jq('a'); })();")

This is because ExecuteJavascriptWithResult can only return basic Javascript types (either a String, Number, Boolean, Array, or a user-created Object). You attempt to return a native DOM Element Object which cannot be mapped to one of these types and so the request fails.

Sign up to request clarification or add additional context in comments.

5 Comments

Would be nice if it threw an exception with an explanation at the very least. I guess I can still pick out the properties I want and return those as strings I guess. Thanks for your help!
Also, what's a user-created object and how does it differ from a jQuery object?
It's a bug, it should return null immediately. I'll add it to our to-do list.
A user-created object is any object created by a script during runtime. A DOM element object (such as that returned by document.getElementById or wrapped by a jQuery selector function) is created by the system and is actually a thin wrapper around a native object (which lives in C++ and cannot be duplicated).
Ah...so it's not the jQuery object itself that's the problem, it's the fact that it contains native objects. Gotcha!
1

An easy way to return complex objects would be to convert into a string using JSON.stringify(), then parse back out in your C# managed code.

For example:

JSValue rawToken = browser.ExecuteJavascriptWithResult(@"JSON.stringify(someTokenObjectHere);");
if (rawToken.IsString)
{
    // For generic objects:
    JObject payload = JObject.Parse(rawToken.ToString());
    // For typed objects:
    MyCustomTokenObject payload = JsonConvert.DeserializeObject<MyCustomTokenObject>(rawToken.ToString());
}

(It may be advantageous to include Newtonsoft.Json for the serialization stuff.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.