Deshabilitar la carga de imágenes en JavaFX WebEngine

I've searched everywhere but found no way to disable loading of images by Java WebEngine.

Investigación realizada:

I found some ideas, such as using URL.setURLStreamHandlerFactory() to use my own URLStreamHandler, and having that analyze the URL to only return URLConnections for URL's that don't end in .jpg .png etc. BUt that has many problems: Sometimes the image url doesn't end in .jpg, if it's a dynamic image, such as a captcha. So how can I disable automatic image loading from WebEngine?

preguntado el 28 de mayo de 14 a las 14:05

Consider moving 'Research done:' to an answer -

1 Respuestas

I'm no JSoup expert but it should be easy enough to do something like this.

import java.net.URL;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class NoImg extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        WebView wv = new WebView();
        WebEngine we = wv.getEngine();
        Document doc = Jsoup.parse(new URL("http://www.google.com"), 5000);
        doc.select("img").stream().forEach((element) -> {
            element.remove();
        });
        we.loadContent(doc.outerHtml());
        Scene scene = new Scene(wv, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

It seems to work fine. You need the jsoup.jar

contestado el 28 de mayo de 14 a las 15:05

yes but sometimes the page requires a lot of javascript when manipulating the DOM, for example if I select a certain combobox a certain textfield is enabled in the dom through the page javascript, so using the WebEngine.load() is needed because it handles all javascript processing when manipulating the dom, filling out forms etc, whereas jsoup I don't think does that, it only parses the base html into dom, w/out future js processing when manipulating the dom. Also, images are sometimes inserted w/out the img tag, for example using the style background-image. - ttilt

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.