在Webview中禁用自动图像加载/

在Webview中禁用自动图像加载/,webview,webkit,javafx,Webview,Webkit,Javafx,我正在使用JavaFXWebView开发一个web刮板。出于结疤的目的,我不需要加载图像。在加载页面时,Webkit会生成大量UrlLoader线程。因此,我认为最好禁用图像,这样可以节省大量系统资源。有人知道如何在Webview中禁用自动图像加载吗?解决方案 为http定义自己的协议处理程序,并过滤掉任何具有图像mime类型或内容的内容 URL.setURLStreamHandlerFactory(new HandlerFactory()); 示例代码 样本注释 该示例使用以下概念:

我正在使用JavaFXWebView开发一个web刮板。出于结疤的目的,我不需要加载图像。在加载页面时,Webkit会生成大量UrlLoader线程。因此,我认为最好禁用图像,这样可以节省大量系统资源。有人知道如何在Webview中禁用自动图像加载吗?

解决方案

为http定义自己的协议处理程序,并过滤掉任何具有图像mime类型或内容的内容

URL.setURLStreamHandlerFactory(new HandlerFactory());
示例代码

样本注释

该示例使用以下概念:

该示例仅探测文件名以确定内容类型,而不是附加到url的输入流。虽然探测输入流是确定url所连接的资源是否实际是图像的更准确的方法,但探测流的效率稍低,因此提出的解决方案以精度换取效率

提供的解决方案仅演示由http协议服务的位置,而不演示由https协议服务的位置

提供的解决方案使用sun.net.www.protocol.http.Handler类,该类在Java 9中可能不公开(因此该解决方案可能不适用于Java 9)

urlStreamHandlerFactory是JVM的全局设置,因此一旦设置,它将保持这种状态(例如,任何java.net.URL连接的所有图像都将被忽略)

示例解决方案返回一个空白(透明)图像,并通过网络加载该图像。为了提高效率,图像可以作为资源从类路径加载,而不是通过网络加载


您可以返回空连接,而不是与空映像的连接,如果您这样做,web视图代码将开始向控制台报告空指针异常,因为它没有获得预期的url连接,并将用x映像替换所有映像以显示缺少映像(我不建议使用返回空连接的方法).

及时,我刚刚实现了这个功能。下面的帖子是一个演示。那么你最终返回了什么来代替
这里的某个东西呢?
?我也很有兴趣知道你放置了什么,我现在正面临着同样的问题完整的例子!嘿,我想我也遇到了同样的情况。但是使用了不同的方法我的案例是如何仅禁用未显示的某些图像,而不是禁用所有图像…?@John Vu
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.*;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.*;

public class LynxView extends Application {
    private static final String BLANK_IMAGE_LOC =
            "https://upload.wikimedia.org/wikipedia/commons/c/ce/Transparent.gif";
    public static final String WEBSITE_LOC =
            "http://fxexperience.com";
    public static final String IMAGE_MIME_TYPE_PREFIX =
            "image/";

    @Override
    public void start(Stage stage) throws Exception {
        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        engine.load(WEBSITE_LOC);

        stage.setScene(new Scene(new StackPane(webView)));
        stage.show();
    }

    public static void main(String[] args) throws IOException {
        URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
            @Override
            public URLStreamHandler createURLStreamHandler(String protocol) {
                if ("http".equals(protocol)) {
                    return new sun.net.www.protocol.http.Handler() {
                        @Override
                        protected URLConnection openConnection(URL url, Proxy proxy) throws IOException {
                            String[] fileParts = url.getFile().split("\\?");
                            String contentType = URLConnection.guessContentTypeFromName(fileParts[0]);
                            // this small hack is required because, weirdly, svg is not picked up by guessContentTypeFromName
                            // because, for Java 8, svg is not in $JAVA_HOME/lib/content-types.properties
                            if (fileParts[0].endsWith(".svg")) {
                                 contentType = "image/svg";
                            }
                            System.out.println(url.getFile() + " : " + contentType);
                            if ((contentType != null && contentType.startsWith(IMAGE_MIME_TYPE_PREFIX))) {
                                return new URL(BLANK_IMAGE_LOC).openConnection();
                            } else {
                                return super.openConnection(url, proxy);
                            }
                        }
                    };
                }

                return null;
            }
        });

        Application.launch();
    }
}
 public URLStreamHandler createURLStreamHandler(String protocol) {
     if ("http".equals(protocol)) { 
         return new URLFortuneHandler(); 
     }
     else return null;
 }
}

public class URLFortuneHandler extends sun.net.www.protocol.http.Handler {
    protected URLConnection openConnection(URL url) throws IOException {
        String file = url.getFile();
        int mid= file.lastIndexOf(".");
        String ext = file.substring(mid+1,file.length());        
        if ("jpg".equals(ext) || "png".equals(ext)) 
            return somethinghere;
        else 
            return super.openConnection(url);
    }    
}