JavaFX webview, getting document height

How to get document height for webview control in JavaFx?

+4
source share
2 answers

You can get the height of the document displayed in the WebView using the following call:

webView.getEngine().executeScript(
    "window.getComputedStyle(document.body, null).getPropertyValue('height')"
);

Full application to demonstrate call usage:

import javafx.application.Application;
import javafx.beans.value.*;
import javafx.scene.Scene;
import javafx.scene.web.*;
import javafx.stage.Stage;
import org.w3c.dom.Document;

public class WebViewHeight extends Application {
  @Override public void start(Stage primaryStage) {
    final WebView webView = new WebView();
    final WebEngine engine = webView.getEngine();
    engine.load("http://docs.oracle.com/javafx/2/get_started/animation.htm");
    engine.documentProperty().addListener(new ChangeListener<Document>() {
      @Override public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) {
        String heightText = webView.getEngine().executeScript(
          "window.getComputedStyle(document.body, null).getPropertyValue('height')"
        ).toString();
        double height = Double.valueOf(heightText.replace("px", ""));    

        System.out.println(height);
      }
    });
    primaryStage.setScene(new Scene(webView));
    primaryStage.show();
  }

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

Source of the above answer: Oracle JavaFX forums. WebView Stream Configuration .


Tracked error tracking request for Java API for related function:

RT-25005 Automatically preferred WebView sizing .

+5
source

This is what you are looking for:

Double.parseDouble(webView.getEngine().executeScript("document.height").toString())
0
source

Source: https://habr.com/ru/post/1015826/


All Articles