Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.

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

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

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

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

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.JSObject;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

public class ContentListening {

    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);

        browser.addScriptContextListener(new ScriptContextAdapter() {
            public void onScriptContextCreated(ScriptContextEvent event) {
                Browser browser = event.getBrowser();
                JSObject window = browser.executeJavaScriptAndReturnValue("window")
                window.setProperty("java", new JavaObject());
                browser.executeJavaScript("window.onload = function() {"
                        + "const element = document.getElementById('counter');\n"
                        + "const observer = new MutationObserver(\n"
                        + "    function(mutations) {\n"
                        + ";\n"
                        + "    });\n"
                        + "const config = {childList: true};\n"
                        + "observer.observe(element, config); };");

        String path = new File(".").getAbsolutePath();

        browser.loadURL(path + "/index.html");

     * The object observing DOM changes.
     * <p>
     * <p>The class and methods that are invoked from JavaScript code must be public.
    public static class JavaObject {

        @SuppressWarnings("unused") // invoked by callback processing code.
        public void onDomChanged(String innerHtml) {
            System.out.println("DOM node changed: " + innerHtml);