This example demonstrates how to use JavaScript MutationObserver in combination with JavaScript-.NET Bridge API. The goal of this example is to transmit every change of the HTML element to the .NET side and display it in the Output window.

The HTML page that we are listening to has timer that updates value of the span element. The source code of the page is the following:

<!DOCTYPE html>
      <span class="countdown" id="countdown"></span>
    <script src=""
    <script type="text/javascript">
        $(document).ready(function () {
          var counter = 1;
          setInterval(function() { $(".countdown").text(counter++); }, 1000);

The source code of the WPF Window that displays the web page, injects MutationObserver and listens to it is the following:


public partial class MainWindow : Window
    private WPFBrowserView view;
    private MutationObserverListener listener;

    public MainWindow()

        listener = new MutationObserverListener();

        view = new WPFBrowserView();
        view.Browser.FinishLoadingFrameEvent += 

        string indexFilePath = System.IO.Path.Combine(
                Environment.CurrentDirectory, "index.html");

    private void Browser_FinishLoadingFrameEvent(object sender, FinishLoadingEventArgs e)
        if (e.IsMainFrame)
            // Injecting the listener .NET object into Javascript
            JSValue value = view.Browser.ExecuteJavaScriptAndReturnValue("window");
            value.AsObject().SetProperty("MutationObserverListener", listener);

            // Script for attaching observer to the element with id 'countdown'
            string observerScript =
            "var spanElement = document.getElementById('countdown');" +
            "var observer = new MutationObserver(" +
            "function(mutations){" +
            "window.MutationObserverListener.CharacterDataChanged(spanElement.innerHTML);" +
            "});" +
            "var config = { childList: true };" +
            "observer.observe(spanElement, config);";

            // Executing script that attaches observer

public class MutationObserverListener
    public void CharacterDataChanged(string innerText)


Public Partial Class MainWindow
    Inherits Window

    Private view As WPFBrowserView
    Private listener As MutationObserverListener

    Public Sub New()
        listener = New MutationObserverListener()
        view = New WPFBrowserView()
        AddHandler view.Browser.FinishLoadingFrameEvent, AddressOf Browser_FinishLoadingFrameEvent
        Dim indexFilePath As String = System.IO.Path.Combine(Environment.CurrentDirectory, "index.html")
    End Sub

    Private Sub Browser_FinishLoadingFrameEvent(ByVal sender As Object, ByVal e As FinishLoadingEventArgs)
        If e.IsMainFrame Then
            Dim value As JSValue = view.Browser.ExecuteJavaScriptAndReturnValue("window")
            value.AsObject().SetProperty("MutationObserverListener", listener)
            Dim observerScript As String = "var spanElement = document.getElementById('countdown');" &
                                           "var observer = new MutationObserver(" & "function(mutations){" &
                                           "window.MutationObserverListener.CharacterDataChanged(spanElement.innerHTML);" &
                                           "});" & "var config = { childList: true };" &
                                           "observer.observe(spanElement, config);"
        End If
    End Sub