2

I have a plain HTML file that I would like to calls via JxBrowser (backend Java).

JxBrowser allows you to add JavaScript code via:

// Java code 
Browser browser = new Browser(...);
browser.loadURL(...);
...
String test = "setTimeout(function() { alert('hello'); location.reload();}, 6000)";
browser.executeJavaScript(test);

This code will indeed refresh the page, but only once because when the html page is refreshed, whatever was added by JxBrowser seems to be cleared.

Without modifying the html file (or the front end), is it possible to add a script such that this page can be continuously refreshed every 6 seconds?

The only other way I can think of is to periodically execute browser.executeJavaScript(test).

1. create a new nonblocking thread
2. for every 6 seconds, call browser.executeJavaScript(test)

Thank you.

meowDestroyer
  • 125
  • 1
  • 2
  • 7
  • You loaded the URL, then modified the DOM. Then the JS you added reloaded the original document. Why would you expect that the JS you inserted into the DOM would still be there? I'm not an expert on JxBrowser, but I expect any attempt to use injected JS will fail. You will probably have to write Java code in its own thread to refresh the page regularly. Remember that the browser engine is running in a separate process and interacting with your code via inter-process communication. – Jim Garrison Nov 20 '20 at 23:49

2 Answers2

0

Try using setIntraval instead of setTimeout:

String test = "setInterval(function() { alert('hello'); location.reload();}, 6000)";
browser.executeJavaScript(test);

This would execute the code every 6 seconds (6000ms). Here is a simple example of setInterval:

setInterval(function() { console.log('refresh page') }, 6000)
Ido
  • 89
  • 11
  • `String test = "setInterval(function() { alert('hello'); location.reload();}, 6000)"; browser.executeJavaScript(test);` It appears that the following doesn't work either, which I suspect is due to location.reload(). If I remove location.reload(), then I would get the pop up every 6 seconds. – meowDestroyer Nov 20 '20 at 23:08
0

Why reloading web page from JavaScript? Why not doing it from Java code instead? For example:

Browser browser = engine.newBrowser();
Navigation navigation = browser.navigation();
navigation.loadUrl("https://www.google.com");
new java.util.Timer().schedule(new TimerTask() {
    @Override
    public void run() {
        navigation.reload();
    }
}, 1_000, 60_000);
Vladimir
  • 1
  • 1
  • 23
  • 30