3

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

Research done:

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?

1
  • Consider moving 'Research done:' to an answer
    – zohar
    May 28, 2014 at 14:04

1 Answer 1

1

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

1
  • 2
    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
    May 28, 2014 at 19:52

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.