Using DotNetBrowser JavaScript API you can work with JavaScript objects directly from .NET code. First, you need to load a web page and wait until it's loaded completely to access its JavaScript. Then using the Browser.ExecuteJavaScriptAndReturnValue()method you can access the required JavaScript object. For example:

C#

JSValue document= browser.ExecuteJavaScriptAndReturnValue("document");

VB.NET

Dim document As JSValue = browser.ExecuteJavaScriptAndReturnValue("document")


The method returns JSValue so you need to make sure that received value is a JavaScript object:

C#

if (document is JSObject)
{
    JSObject documentObject = (JSObject)document;
}

VB.NET

If TypeOf document Is JSObject Then
    Dim documentObject As JSObject = CType(document, JSObject)
End If


Now you can work with the JavaScript document object. You can access and modify its properties. For example, modify its document.title property:

C#

// Call document.title = "New Document Title";
bool success = documentObject.SetProperty("title", JSValue.Create("New Document Title"));

VB.NET

' Call document.title = "New Document Title";
Dim success As Boolean = documentObject.SetProperty("title", JSValue.Create("New Document Title"))


Example


C#

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

namespace JavaScriptCSBridgeSample
{
    class Program
    {
        public static void Main()
        {
            using (Browser browser = BrowserFactory.Create())
            {
                ManualResetEvent waitEvent = new ManualResetEvent(false);

                browser.FinishLoadingFrameEvent += delegate (object sender, FinishLoadingEventArgs e)
                {
                    if (e.IsMainFrame)
                    {
                        var window = browser.ExecuteJavaScriptAndReturnValue("window");
                        if (window is JSObject)
                        {
                            ((JSObject)window).SetProperty("CSFunction", new CSFunctionCallback("New", "title"));
                        }
                        browser.ExecuteJavaScriptAndReturnValue("MyClass.foo();");
                    }
                };
                browser.TitleChangedEvent += delegate (object sender, TitleEventArgs e)
                {
                    Console.Out.WriteLine("\"document.title\" \"" + e.Title + "\"");
                    waitEvent.Set();
                };


                browser.LoadHTML(@"<html>
                                     <body>
                                        <script type='text/javascript'>
                                            var MyClass = { foo: function () { document.title = CSFunction(); } }
                                        </script>
                                     </body>
                                   </html>");

                waitEvent.WaitOne();
                Thread.Sleep(2000);
            }
        }
    }

    class CSFunctionCallback : JSFunctionCallback
    {
        string a;
        string b;

        public CSFunctionCallback(string a, string b) : base()
        {
            this.a = a;
            this.b = b;
        }

        public object Invoke(params object[] args)
        {
            Console.Out.WriteLine("CSFunction is invoked");
            return JSValue.Create(string.Format("{0} {1}", a, b));
        }
    }
}


VB.NET

Imports System.Threading
Imports DotNetBrowser
Imports DotNetBrowser.Events

Module Module1
    Sub Main()
        Using browser As Browser = BrowserFactory.Create()

            Dim waitEvent As ManualResetEvent = New ManualResetEvent(False)

            AddHandler browser.FinishLoadingFrameEvent,
                sub(sender As Object, e As FinishLoadingEventArgs)

                    If e.IsMainFrame Then
                        Dim window = browser.ExecuteJavaScriptAndReturnValue("window")

                        If TypeOf window Is JSObject Then
                            CType(window, JSObject).SetProperty("CSFunction", new CSFunctionCallback("New", "title"))
                        End If

                        browser.ExecuteJavaScriptAndReturnValue("MyClass.foo();")
                    End If
                End sub

            AddHandler browser.TitleChangedEvent,
                sub(sender As Object, e As TitleEventArgs)


                    Console.Out.WriteLine("""document.title"" """ + e.Title + "")
                    waitEvent.Set()
                End sub


            browser.LoadHTML("<html>
                                 <body>
                                    <script type='text/javascript'>
                                        var MyClass = { foo: function () { document.title = CSFunction(); } }
                                    </script>
                                 </body>
                               </html>")

            waitEvent.WaitOne()
            Thread.Sleep(2000)

        End Using
    End Sub
End Module

Friend Class CSFunctionCallback
    Implements JSFunctionCallback
    Private a As String
    Private b As String

    Public Sub New(a As String, b As String) 
        Me.a = a
        Me.b = b
    End Sub

    Public Function Invoke(ParamArray args As Object()) As Object Implements JSFunctionCallback.Invoke
        Console.Out.WriteLine("CSFunction is invoked")
        return JSValue.Create(string.Format("{0} {1}", a, b))
    End Function
End Class