How to register a Java Object in the JxBrowser before first JavaScript - jxbrowser

I am using JxBrowser 6.1.1 and succeed in registering a Java object in the JsContext in a onFinishLoadingFrame handler. The problem is that the property is not yet available when the first JavaScript is run in the page loaded by the JxBrowser.
The working call looks like this:
browser.executeJavaScriptAndReturnValue("window").asObject().setProperty("api", api);
When I do the same in the new onScriptContextCreated handler, the program halts executing:
JSValue jsWindow = browser.executeJavaScriptAndReturnValue("window");
The full code is and fails at line JSValue jsWindow = ...:
client.browser.addScriptContextListener(new ScriptContextAdapter() {
#Override
public void onScriptContextCreated(ScriptContextEvent event) {
Browser browser = event.getBrowser();
JSValue jsWindow = browser.executeJavaScriptAndReturnValue("window");
JSObject windowObject = jsWindow.asObject();
windowObject.setProperty("api", client.getApi());
}
});
I guess the window property is simply not there yet.
Is there a way to achieve this with the onScriptContextCreated event or is there a different way to achieve this?
The code also works fine when started in a separate thread as proposed below. But the problem remains that this code runs later than the first JavaScript in the browser.
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
JSValue jsWindow = browser.executeJavaScriptAndReturnValue("window");
JSObject windowObject = jsWindow.asObject();
windowObject.setProperty("api", client.getApi());
}
});
thread.start();

Related

Trying to synchronize states between JavaFX WebView DOM and controlling class

In my controller class, I have a WebView object set up
#FXML
private WebView newsletterPreview;
// some stuff...
urlString = url.toExternalForm();
WebEngine engine = newsletterPreview.getEngine();
bridge = new JSBridge(engine, this);
JSBridge code snippet ...
private String headlineCandidate;
public String getHeadlineCandidate() {
return headlineCandidate;
}
//mo stuff...
public JSBridge(WebEngine engine, ENewsLetterDialogController controller) {
engine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
if (newState == State.SUCCEEDED) {
window = (JSObject) engine.executeScript("window");
window.setMember("bridge", this);
engine.executeScript(loadDomListenerScript());
}
});
}
private String loadDomListenerScript() {
return WOWResourceUtils.resourceToString(LISTENER_SCRIPT);
}
public void captureHeadline(String element) {
this.headlineCandidate = element;
System.out.println(headlineCandidate);
}
Listener script snippet..
//listener script
"use strict";
document.addEventListener('click', function(e) {
e = e || window.event;
var target = e.target || e.srcElement, text = target.textContent ||
text.innerText;
window.bridge.captureHeadline(target.parentElement.outerHTML);
});
Where the LISTENER_SCRIPT is embedded Javascript as a resource, and headlineCandidate is a public string property
I have the following working:
I navigate load the web page, and I can get the JSBridge class to echo back the expected parent html of whatever element I click (obviously this is for testing). However I cannot find a good way to get that information back to Application thread so I can (for example) enable a button if the user clicks on the correct HTML element. Yes, this design is based on certain assumptions about the page. That's part of the use case.
THe root problem seems to be that the WebView is on a different thread than the Application, but I couldn't find a good way to synchronize the two
Should I try polling from Application Thread?
Sorry if it's been asked before, been searching here for about 3 days
Use Platform.runLater to communicate data from a non-JavaFX thread to the JavaFX thread:
// Java method invoked as a result of a callback from a
// JavaScript event handler in a webpage.
public void callbackJava(String data) {
// We are currently not on the JavaFX application thread and
// should not directly update the scene graph.
// Instead we call Platform.runLater to ship processing of data
// to the JavaFX application thread.
Platform.runLater(() -> handle(data));
}
public void handle(String data) {
// now we are on the JavaFX application thread and can
// update the scene graph.
label.setText(data);
}

Error in JavaFX WebView listener for click event while trying to record that a click has been performed on a page

The primary purpose is to Print "click operation has been performed" in the console, if any click is performed on the page loaded in the embedded browser, for achieving the aforementioned behavior I got the below code, it shows error.
((EventTarget) el).addEventListener("click", listener, false);
Here is the complete code snippet:
https://docs.oracle.com/javafx/2/api/javafx/scene/web/WebEngine.html
EventListener listener = new EventListener() {
public void handleEvent(Event ev) {
System.out.println("Click Operation has been performed");
}
};
Document doc = webEngine.getDocument();
Element el = doc.getElementById("dummyid");
((EventTarget) el).addEventListener("click", listener, false);
As shown in the link you've provided, you can call java methods by using JSObject.setMember method.
public class JavaApplication {
public void exit() {
Platform.exit();
}
}
...
JSObject window = (JSObject) webEngine.executeScript("window");
window.setMember("app", new JavaApplication());
You can call from the web page
Click here to exit application
This could be an alternative solution instead of using handlers

Multithreading using Callable while having a responsive graphical interface

I'm trying to get a responsive JavaFX graphical interface while executing a cmd command.
The command I'm executing is the following.
youtube-dl.exe --audio-format mp3 --extract-audio https://www.youtube.com/watch?v=l2vy6pJSo9c
As you see this is a youtube-downloader that converts a youtube link to an mp3-file.
I want this to be executed in a second thread and not in the main FX thread.
I've solved this by implementing interface Callable in the class StartDownloadingThread.
#Override
public Process call() throws Exception {
Process p = null;
p = ExecuteCommand(localCPara1, localCPara2, localDirectory).start();
try {
Thread.sleep(30);
}catch (InterruptedException e){}
return p;
}
The method ExecuteCommand just returns a ProcessBuilder object.
I try to use Thread.sleep to make the program return to the main thread and thus making the application responsive. Unfortunately the program still freezes.
This is how the method call is called.
ExecutorService pool = Executors.newFixedThreadPool(2);
StartDownloadingThread callable = new StartDownloadingThread(parameter1, parameter2, directory);
Future future = pool.submit(callable);
Process p = (Process) future.get();
p.waitFor();
How do I make my GUI responsive using the interface Callable?
Using a executor to run a task just for you to use the get method of the Future that is returned when submitting the task does not actually free the original thread to continue with other tasks. Later you even use the waitFor method on the original thread, which is likely to take even more time than anything you do in your Callable.
For this purpose the Task class may be better suited, since it allows you to handle success/failure on the application thread using event handlers.
Also please make sure an ExecutorService is shut down after you're done submitting tasks.
Task<Void> task = new Task<Void>() {
#Override
protected Void call() throws Exception {
Process p = null;
p = ExecuteCommand(localCPara1, localCPara2, localDirectory).start();
// why are you even doing this?
try {
Thread.sleep(30);
}catch (InterruptedException e){}
// do the rest of the long running things
p.waitFor();
return null;
}
};
task.setOnSucceeded(event -> {
// modify ui to show success
});
task.setOnFailed(event -> {
// modify ui to show failure
});
ExecutorService pool = Executors.newFixedThreadPool(2);
pool.submit(task);
// add more tasks...
// shutdown the pool not keep the jvm alive because of the pool
pool.shutdown();

JxBrowser HEAVYWEIGHT Dispose on WINDOW_CLOSE_REQUEST

I'm currently playing around with the Evaluation License for JxBrowser 6.2.
I'm creating the BrowserView as follows:
Browser browser = new Browser(BrowserType.HEAVYWEIGHT);
BrowserView browser_view = new BrowserView(browser);
I'm attaching the BrowserView component as follows:
stage.setScene(new Scene(browser_view));
If the Browser is configured to operate in LIGHTWEIGHT mode, I'm able to execute:
browser_view.getBrowser().dispose();
Platform.exit();
However, if the Browser is configured to operate in HEAVYWEIGHT mode, then the application hangs when executing:
browser_view.getBrowser().dispose();
I can see in the logs that the Dispose message was written, but it appears as though the JxBrowser Chromium processes never receive/process the message.
Any ideas?
As answered before me the solution to this is to dispose the browser after the stage has been hidden (closed).
A good approach would be to put those commands on the stop() method of JavaFX Application.
So that either way you close the window (by clicking the close button or programmatically by calling Platform.exit()), the browser will dispose (and the whole application will finish and exit).
Something like that:
#Override
public void stop() throws Exception {
stage.hide();
browser.dispose();
}
As a reference, I used configuration described here link (Section: 9. Pop-up Windows).
Platform.runLater(() -> {
browser.dispose();
});
Platform.runLater(() -> {
stage.close();
});
It looks like you need to ensure that the stage has been closed before calling dispose.
stage.close();
browser_view.getBrowser().dispose();
Platform.exit();
Please try calling this code asynchronously. Maybe it's just a deadlock:
Platform.runLater(new Runnable() {
#Override
public void run() {
browser_view.getBrowser().dispose();
Platform.exit();
}
});

JavaFX auto-scroll auto-update text

Newbie question about JavaFX that I haven't been able to answer, despite knowing it must be pretty simple to do and not finding any resources on it anywhere I've looked (tutorials, many of the Oracle online docs, articles, the well-known JavaFX bloggers, etc.)
I'm developing a command line (script) running application and I have successfully gotten output (via ProcessBuilder) from the script that I can display in an ongoing manner, as things happen on the command line. That is, I can do System.out.println(line); all day long, showing the output in the console, which simply returns output from an input stream returned by the 'myProcess' that's running, created like this:
BufferedReader bri = new BufferedReader(new InputStreamReader(myProcess.getInputStream()))
So I am able to see all the output coming back from the script.
I'd like to set-up a JavaFX TextArea or ScrollPane or, not sure what, to display this output text (there's a lot of it, like several thousand lines) as an ongoing 'progress' of what's taking place in the script, as it happens. I have a Scene, I have buttons and get input from this scene to start the script running, but now I'd like to show the result of clicking the button "RUN THIS SCRIPT", so to speak.
I assume I need to create a TextArea as described here or perhaps a TextBuilder would be useful to begin making it. Not sure.
I need a bit of help in how to setup the binding or auto-scroll/auto-update part of this.
Can someone provide me a place to start, to do this with JavaFX? I'd rather not use Swing.
(I'm using JavaFX 2.2, JDK 1.7u7, all the latest stuff, and yes, this is an FXML app--so doing it that way would be preferred.)
UPDATE: Sergey Grinev's answer was very helpful in the binding part. But here is some more detail on what I mean when I ask for "a bit of help in how to setup" -- basically, I need to return control to the main Scene to allow the user to Cancel the script, or to otherwise monitor what's going on. So I'd like to "spawn" the process that runs that script (that is, have some kind of 'free running process'), but still get the output from it. (I wasn't very clear on that in my initial question.)
The technique I'm using here (see below) is to do a waitFor on the process, but of course this means the dialog/Scene is 'hung' while the script executes. I'd like to gain control back, but how do I pass the 'p' (Process) to some other controller piece (or alternatively, simply kick off that other process passing in the parameters to start the script and have it start the script) that will then do the auto-update, via the binding Sergey Grinev mentions--without 'hanging' the Scene/window? Also: Can I then 'stop' this other process if the user requests it?
Here is my current code ('waits' while script--which takes 20-40 min to run!--completes; this is not what I want, I'd like control returned to the user):
public class MyController implements Initializable {
#FXML
private void handleRunScript(ActionEvent event) throws IOException {
ProcessBuilder pb = new ProcessBuilder("myscript.sh", "arg1", "arg2", ...);
Process p = pb.start();
try {
BufferedReader bri = new BufferedReader
(new InputStreamReader(p.getInputStream()));
String line;
while ((line = bri.readLine()) != null) {
System.out.println(line);
textAreaRight.setText(line);
}
bri.close();
p.waitFor();
}
catch (Exception err) {
err.printStackTrace();
}
}
#FXML
private void handleCancel(ActionEvent event) {
doSomethingDifferent();
}
}
To log strings you can use TextArea
To make it asynchronious you need to make a separate thread for output reader.
public class DoTextAreaLog extends Application {
TextArea log = new TextArea();
Process p;
#Override
public void start(Stage stage) {
try {
ProcessBuilder pb = new ProcessBuilder("ping", "stackoverflow.com", "-n", "100");
p = pb.start();
// this thread will read from process without blocking an application
new Thread(new Runnable() {
#Override
public void run() {
try {
//try-with-resources from jdk7, change it back if you use older jdk
try (BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
while ((line = bri.readLine()) != null) {
log(line);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}).start();
stage.setScene(new Scene(new Group(log), 400, 300));
stage.show();
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
public void stop() throws Exception {
super.stop();
// this called on fx app close, you may call it in user action handler
if (p!=null ) {
p.destroy();
}
}
private void log(final String st) {
// we can access fx objects only from fx thread
// so we need to wrap log access into Platform#runLater
Platform.runLater(new Runnable() {
#Override
public void run() {
log.setText(st + "\n" + log.getText());
}
});
}
public static void main(String[] args) {
launch();
}
}

Resources