Search code examples
javascriptjavajxbrowser

Listen changes on page using JxBrowser


In Java project I use JxBrowser.

I have page

<!DOCTYPE html>
<html>
<body>

Start: <input type="text" id="startTimeField"> ms
<hr>
<button id="buttonToClick" onclick="myFunction()">Start</button>

<script>
    function myFunction() {
       document.getElementById('startTimeField').value = new Date().getMilliseconds();
    }
</script>

</body>
</html>

I load page correct with JxBrowser.

When button "Start" is clicked, then value of the text input is setted.

I want add listener using JxBrowser object in Java to listen startTimeField text input changes.

Ex: startTimeField is setted by 0, after click Start button the field change value (by actual ms time) and I want detect this changes in Java code and get the value of change.


Solution

  • Since the question basically asks how to listen to events in the DOM in Java, I think this is what you are looking for:

    JxBrowser - DOM Events

    You will have to do something similar to this (in your Java code):

    DOMElement element = browser.getDocument().findElement(By.id("startTimeField"));
    element.addEventListener(DOMEventType.OnChange, new DOMEventListener() {
        public void handleEvent(DOMEvent event) {
            // your code for reacting to the change here
        }
    }, false);
    

    I did not test this code but adapted it from the documentation, so it might not compile and work right away, but this should lead you on the right track.