2

I want to load some content or page in a JavaFX WebView and offer a Bridge object to Java so the content of the page can do calls into java.

The basic concept of how to do this is described here: https://blogs.oracle.com/javafx/entry/communicating_between_javascript_and_javafx

Now my question is: When is a good time inject the bridge-object into the WebView so it is available as soon as possible.

One option would be after page load as described here: https://stackoverflow.com/a/17612361/1520422

But is there a way to inject this sooner (before the page content itself is initialized), so the bridge-object is available DURING page-load (and not only after page-load)?

2 Answers 2

4

Since no one has answered, I'll tell you how I'm doing it, although it is ugly. This provides the ability for the page to function normally in non-Java environments but receive a Java object in Java environments.

I start by providing an onStatusChanged handler to the WebEngine. It listens for a magic value for window.status. If the magic value is received, the handler installs the Java object. (In my case, it's more complex, because I have some more complex orchestration: I'm executing a script that provides a client-side API for the page and then sets another magic value on window.status to cause the Java object to be sent to an initialization method of the client-side API).

Then in my target page, I have the following code in the first script in the page:

window.status = "MY-MAGIC-VALUE";
window.status = "";

This code is essentially a no-op in a "normal" browser but triggers the initialization when running in the custom JavaFX embedding.

Sign up to request clarification or add additional context in comments.

1 Comment

I found this solution by myself already. Thanks anyway. I was hoping for something that was more like designed for this purpose... Still i accepted your solution.
-1

In Java 8, you can trigger event changing from SCHEDULED to RUNNING to inject objects at this time. The objects will present in WebEngine before JavaScript running. Java 7, I see the state machine quite differs in operating, no solution given for Java 7.

webEngine.getLoadWorker().stateProperty().addListener(
   new ChangeListener<State>(){
   public void changed(ObservableValue<? extends State> ov, 
             State oldState, 
             State newState) 
         {
            // System.out.println("old: "+oldState+", new: "+newState);
             if(newState == State.RUNNING && 
                oldState == State.SCHEDULED){
                 JSObject window = (JSObject)webEngine.executeScript("window");
                 window.setMember("foutput", foutput);
             }
         }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.