How to disable specific resource loading in Jcef?

Having problems with building or using the JCEF Java binding? Ask your questions here.

How to disable specific resource loading in Jcef?

Postby rivolt80 » 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.

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...." />)
Last edited by rivolt80 on Fri Apr 13, 2018 9:56 am, edited 1 time in total.
rivolt80
Newbie
 
Posts: 9
Joined: Fri Apr 13, 2018 5:16 am

Re: How to disable specific resource loading in Jcef?

Postby rivolt80 » Fri Apr 13, 2018 9:54 am

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?
rivolt80
Newbie
 
Posts: 9
Joined: Fri Apr 13, 2018 5:16 am


Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 14 guests