【问题标题】:javafx webview does not display correctly an html pagejavafx webview 无法正确显示 html 页面
【发布时间】:2021-10-14 20:47:20
【问题描述】:

当我测试这个非常示例程序时,webview 无法正确显示 html 页面。

  1. 首页(第一个圆圈)显示正确!但是..
  2. 应该每 10 秒换一页,实际上没有。
  3. 它应该显示图表(当您单击第二个圆圈时),它没有。
  4. 它应该显示显示图像(当您单击第三个圆圈时)它不显示。

这是我的配置: 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>

代码

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();
    }
}

我的总体观点是让 webview 像浏览器一样工作。有什么想法可以解决第 2、3 和 4 点吗?

【问题讨论】:

  • hmm .. 该页面的行为与您在普通浏览器中描述的不同(移动设备上的 firefox)
  • 使用三星 S8、Android V9、最新版本的 Firefox 或 Chrome 我得到了这个结果:我看到的唯一区别是它是从第二个圆圈开始的,我描述的所有点仍然是真的。是你的意思吗?如果是这样,你是对的,但它并没有真正使我的问题无效,不是吗?
  • 好吧,这似乎是我的旧移动版本 - 现在在桌面上我看到了你的描述。不过,对 fx 网络引擎了解不足,无法提供帮助,抱歉
  • Youtube 视频也无法启动:lWebView.getEngine().load("youtube.com/watch?v=nfA45OKvzM4"); // youtube 测试
  • 您需要一个协议来将外部 url 加载到 WebEngine 中。

标签: javafx webview


【解决方案1】:

测试环境背景

我进行了一些实验,以确定是什么原因导致(至少部分)您报告的与您提供的页面的显示有关的问题。

对于我的测试,我使用的是 JavaFX 17.0.0.1,在 MS Surface Book 2 上运行 Eclipse Temurin 17 JDK 和 Windows 10 Pro OS。

在 Chrome 94 中显示的问题示例网页没有您在问题中指出的问题,在 JavaFX WebView 中遇到了您在问题中指出的问题。

为了调试问题,我向 Web 引擎添加了一个控制台侦听器,如以下最流行的答案所示:

这样做后,我看到控制台记录了以下错误:

控制台:[https://cdn.jsdelivr.net/npm/chart.js:13] ReferenceError:找不到变量:ResizeObserver

关于 ResizeObserver 的信息

做一些研究,我可以看到 ResizeObserver 实现不适用于所有浏览器实现。所以我猜它没有在 WebView 中实现。

来自 Mozilla 的 ResizeObserver 信息

来自 Mozilla 的 ResizeObserver 示例测试页面(我不对示例中的奇怪短信负责):

在 WebView 中从 mozilla 加载示例页面确认了不支持 ResizeObserver 的问题,并在附加 WebConsoleListener 时打印以下错误:

控制台:[https://mdn.github.io/dom-examples/resize-observer/resize-observer-text.html:110] 不支持调整观察者大小!

如何解决

您可以通过polyfill 将 ResizeObserver 功能添加到 WebView(或其他不支持该功能的浏览器)。

解决此问题的最佳方法是要求您正在使用的网站的开发人员将 ResizeObserver 的 polyfill 集成到他们的网站中,以便在更广泛的客户端上使用它。

解决问题的示例 polyfill 是:

如果您下载您正在使用的网站的源代码并在其他项目之前在页面中导入 polyfill JavaScript,那么您在问题中引用的大多数问题都会消失并在 WebView 上按预期工作:

  1. 控制台中未显示有关 ResizeObserver 的错误。
  2. 显示每 10 秒自动更改一次。
  3. 点击第二个圆圈时会显示图表。
  4. 当您单击第三个圆圈时会显示一个图像(尽管该图像比我在 Chrome 上看到的要大并被剪裁了)。

ResizeObserver 的 Polyfill javascript 源代码:

提交 ResizeObserver 支持 JavaFX WebView 的功能请求

如果您愿意,您可以提交 JavaFX WebView 的功能请求,请求调整观察器的大小。

为此,您可以订阅 openjfx-dev mailing list 并在此处发布有关此问题的消息,并链接回此问题。

或者,提交bug report against JavaFX。如果您这样做,我建议您参考 Mozilla 调整大小观察者测试页面和相关文档,而不是原始问题中的测试页面。

测试代码

必须使用来自: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 的详尽而精彩的回答。
  • @user2243632 我再次运行了这个例子,使用http://it-topics.com/index3.html提供的url,它不再给ResizeObserver报错,所以我猜有人修复了这个网站,至少现在是这样。注意JavaFX本身还是不处理ResizeObserver的,所以如果你用mozilla测试页做ResizeObserver,会报错https://mdn.github.io/dom-examples/resize-observer/resize-observer-text.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-19
  • 1970-01-01
  • 2011-03-31
  • 2018-03-19
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多