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