How to disable specific resource loading in Jcef?
Posted: Fri Apr 13, 2018 5:45 am
I want to block all the requests for CefRequest.ResourceType.RT_IMAGE resources. How can I do this? I tried the following solution and it seems to work. The trick used is to intercept the request and change the method from GET to HEAD in onBeforeResourceLoad.
The problem with this is that the request is still being made to the server which is slower than simply blocking the request. The other technique I can think of is to set the URL to some value that is not "active". This way, the request fails right away making this solution a bit faster than the previous one.
In my opinion, the above tricks must be avoided if there's a better solution, like canceling/blocking the request by simply calling a method. I don't know if such a method exists. Can you give a more robust solution for doing what's mentioned. Also, I should point out that both tricks fail to prevent loading images loaded using the data uri scheme (e.g. <img src="data:image/jpeg...." />)
- Code: Select all
@Override
public boolean onBeforeResourceLoad(CefBrowser cefBrowser, CefFrame cefFrame, CefRequest cefRequest) {
if (cefRequest.getResourceType().equals(RT_IMAGE)) {
cefRequest.setMethod("HEAD");
}
return false;
}
The problem with this is that the request is still being made to the server which is slower than simply blocking the request. The other technique I can think of is to set the URL to some value that is not "active". This way, the request fails right away making this solution a bit faster than the previous one.
- Code: Select all
@Override
public boolean onBeforeResourceLoad(CefBrowser cefBrowser, CefFrame cefFrame, CefRequest cefRequest) {
if (cefRequest.getResourceType().equals(CefRequest.ResourceType.RT_IMAGE)) {
cefRequest.setURL("https://tyghjjknm.ertyu");
System.out.println("LOL!");
}
return false;
}
In my opinion, the above tricks must be avoided if there's a better solution, like canceling/blocking the request by simply calling a method. I don't know if such a method exists. Can you give a more robust solution for doing what's mentioned. Also, I should point out that both tricks fail to prevent loading images loaded using the data uri scheme (e.g. <img src="data:image/jpeg...." />)