Page 1 of 1

How to disable specific resource loading in Jcef?

PostPosted: Fri Apr 13, 2018 5:45 am
by rivolt80
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.

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...." />)

Re: How to disable specific resource loading in Jcef?

PostPosted: Fri Apr 13, 2018 9:54 am
by rivolt80
Okay I got it. Image loading can be disabled by appending the command line switch "disable-image-loading" while setting up the AppHandler:

Code: Select all
CefApp.addAppHandler(new CefAppHandlerAdapter(null) {
            @Override
            public void onBeforeCommandLineProcessing(String s, CefCommandLine cefCommandLine) {
                cefCommandLine.appendSwitch("disable-image-loading");
                super.onBeforeCommandLineProcessing(s, cefCommandLine);
            }
        });


But images loaded using the data uri scheme are still visible in the pages. How can I disable them?