using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotNetBrowser;
using DotNetBrowser.Events;
using System.Threading;

namespace ExecuteJavaScript
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Browser browser = BrowserFactory.Create())
            {
                ManualResetEvent waitEvent = new ManualResetEvent(false);
                browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
                {
                    // Wait until main document of the web page is loaded completely.
                    if (e.IsMainFrame)
                    {
                        // Execute JavaScript code and get return value from JavaScript.

                        JSValue returnValue = e.Browser.ExecuteJavaScriptAndReturnValue("wpfBridge");
                        // Make sure that return value is an object.
                        if (returnValue.IsObject())
                        {
                            String Val="Hello World";
                            returnValue.AsObject().SetProperty("formDataString", Val);
                        }
                        waitEvent.Set();
                    }
                };
                browser.LoadHTML("<html><head><script>var wpfBridge = new Object();</script></head><body></body></html>");
                waitEvent.WaitOne();
            }
        }
    }
}