I am trying to trigger an event in JavaScript using a Java class.
How can I send javascript code (either direct input or from a file) in a Java class to a specified destination file which is already opened in the browser (Firefox)?
Currently I’m using the ScriptEngineManager to run my JavaScript code, but the code is executed in my Java environment and I don’t know how to trigger an event in this way (as I cannot use e.g. window.postMessage(“hi”, “*”);)
Any suggestions how to solve this? Work-arounds are also appreciated (preferably without extensions, plugins, …).
Best regards.
When executing from java you have no access to window events – because there is no browser.
I don’t really understand the use case here but you could load the destination file into an iframe and then do a meta refresh – once you update the files contents then the updated results will be displayed on the next refesh.
In any case you can only return results from the inputted javascript functions back to java – not call events.
You could pass a javascript function (a call that simulates the event) back to the browser and then execute it.
How you would pass this back to the client depends on the situation :
EDIT
The server has no link to the browser between requests. To asynchronously receive a server event in a browser you could have a look at:
So the browser would receive notification when the task is complete and then it would be able to react – for instance prompt for the tab to close.