2

When I am testing this very sample program, the webview does not display the html page correctly.

  1. First page (first circle) is displayed correctly ! But ..
  2. It should change of page every 10s, it does not.
  3. It should display graphs (when you click on the second circle), it does not.
  4. It should display display images (when you click on the third circle) it does not.

Here is my config : openjdk-17

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>17.0.0.1</version>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-web</artifactId>
    <version>17.0.0.1</version>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-fxml</artifactId>
    <version>17.0.0.1</version>
</dependency>

Code

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.StageStyle;

public class Main extends Application {
    @Override
    public void start(final Stage pStage) {
        WebView lWebView = new WebView();

        lWebView.getEngine().setJavaScriptEnabled(true);
        lWebView.getEngine().load("http://it-topics.com/index3.html");
        
        VBox lVBox = new VBox(lWebView);

        pStage.setScene(new Scene(lVBox));
        pStage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

My overall point is to make the webview work the same as it would a browser. Any idea to fix points 2, 3 and 4 ?

jewelsea
  • 150,031
  • 14
  • 366
  • 406
Sylvain
  • 113
  • 7
  • hmm .. the page doesn't behave as you describe in a normal browser (firefox on mobile) – kleopatra Oct 15 '21 at 05:40
  • Using Samsung S8, Android V9, latest versions Firefox or Chrome I get this result : The only difference I see with my description is that it is starting with the secoond circle, all the points I describe are still true. Is it what you mean ? If so, you are right but it does not really invalidate my question, doesn't it ? – Sylvain Oct 15 '21 at 08:42
  • okay, seems to be my oldish mobile version - now on desktop I see what you describe. Don't know enough about the fx webengine to help, though, sry – kleopatra Oct 15 '21 at 09:24
  • Youtube videos do not launch either : lWebView.getEngine().load("https://www.youtube.com/watch?v=nfA45OKvzM4"); // youtube test – Sylvain Oct 15 '21 at 10:33
  • You need a protocol to load an external url into a WebEngine. – jewelsea Oct 15 '21 at 19:39
  • @jewelsea Yes, you are right, the actual code I have used is with h t t p s : / / w w w. etc but stackoverflow editor had removed this part... – Sylvain Oct 15 '21 at 20:17
  • @user2243632 YouTube videos work fine in WebView for me. I used the test code from [Play a Youtube video using JavaFX](https://stackoverflow.com/a/18761332/1155209). Example urls which worked `http://www.youtube.com/embed/nfA45OKvzM4?autoplay=1`, `http://youtube.com/watch?v=nfA45OKvzM4`, `https://www.youtube.com/watch?v=nfA45OKvzM4`. Environment Windows 10, JavaFX 17.0.0.1, JDK Eclipse Temurin 17. – jewelsea Oct 15 '21 at 20:54

1 Answers1

4

Background on Test Environment

I ran some experimentations to determine what is causing (at least some of) your reported issues with the display of your provided page.

For my tests I was using JavaFX 17.0.0.1 running Eclipse Temurin 17 JDK and Windows 10 Pro OS on MS Surface Book 2.

The sample webpage from your question displayed in Chrome 94 without the issues you note in your question, and in JavaFX WebView experienced the issues you note in your question.

To debug the issue, I added a console listener to the web engine, as demonstrated in the most popular answers to:

Once I did so, I saw the following error logged to the console:

Console: [https://cdn.jsdelivr.net/npm/chart.js:13] ReferenceError: Can't find variable: ResizeObserver

Information on ResizeObserver

Doing some research I could see that the ResizeObserver implementation isn't available for all browser implementations. So I guess it isn't implemented in WebView.

Information on ResizeObserver from Mozilla

Sample test page for ResizeObserver from Mozilla (I am not responsible for the weird text message in the sample):

Loading the sample page from mozilla in WebView confirms the issue with lack of support for ResizeObserver, with the following error printed when the WebConsoleListener is attached:

Console: [https://mdn.github.io/dom-examples/resize-observer/resize-observer-text.html:110] Resize observer not supported!

How to fix it

You can add the ResizeObserver ability to WebView (or other browsers which do not support the feature) via a polyfill.

The best way to fix it is to request the developers of the website you are using to integrate a polyfill for ResizeObserver into their website to allow it to be used on a wider variety of clients.

An example polyfill which fixes the issue is:

If you download the source for the website you are using and import the polyfill JavaScript in the page before other items, then, most of the issues you reference in your question go away and work as expected on WebView:

  1. No error is displayed in the console regarding the ResizeObserver.
  2. The display automatically changes every 10 seconds.
  3. Graphs display when you click on the second circle.
  4. An image displays when you click on the third circle (though the image is larger than what I see on Chrome and clipped).

Polyfill javascript source for ResizeObserver:

Filing a feature request for ResizeObserver support for JavaFX WebView

If you wish, you can file a feature request for the JavaFX WebView requesting resize observer support.

To do so, you could subscribe to the openjfx-dev mailing list and post a message about this there, linking back to this question.

Or, file a bug report against JavaFX. If you do so, I'd advise referencing the mozilla resize observer test page and associated documentation rather than the test page in your original question.

Test Code

Must be run using the information from: this answer to allow access to an internal com.sun.javafx implementation.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import com.sun.javafx.webkit.WebConsoleListener;

public class WebViewPageLoad extends Application {
    @Override
    public void start(final Stage stage) {
        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();

        engine.getLoadWorker().exceptionProperty().addListener((ov, t, t1) ->
                System.out.println("Received exception: "+t1.getMessage())
        );

        WebConsoleListener.setDefaultListener((webViewReference, message, lineNumber, sourceId) ->
                System.out.println("Console: [" + sourceId + ":" + lineNumber + "] " + message)
        );

// test page from original question:        
        engine.load("http://it-topics.com/index3.html");
// test page for resize observer polyfill (functions as expected).        
//        engine.load("https://que-etc.github.io/resize-observer-polyfill/");
// canonical ResizeObserver test page from mozilla.        
//        engine.load("https://mdn.github.io/dom-examples/resize-observer/resize-observer-text.html");
// referencing a local hacked version of the it-topics.com index3.html.
//        engine.load(WebViewPageLoad.class.getResource("it.html").toExternalForm());

        VBox layout = new VBox(webView);

        stage.setScene(new Scene(layout));
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Thank you very much @jewelsea for your fully detailed and brilliant answer. – Sylvain Oct 17 '21 at 06:56
  • @user2243632 I ran the example again, using the url provided `http://it-topics.com/index3.html`, it no longer gives an error for the ResizeObserver, so I guess somebody fixed the website, at least for now. Note JavaFX itself still doesn't handle the ResizeObserver, so if you use the mozilla test page for ResizeObserver, it will report an error `https://mdn.github.io/dom-examples/resize-observer/resize-observer-text.html`. – jewelsea Oct 20 '21 at 09:58