HelpingHand
HelpingHand

Reputation: 1065

Stop Android WebView from trying to load/capture resources like CSS on loadData()

Background

This may seem to be a duplicate to many other questions. Trust me that it isn't.

I'm trying to load html data into a WebView, being able to capture user hyperlink requests. In the process I've found this answer which does exactly what I want to do, except it captures other requests to things like CSS files and images:

// you tell the webclient you want to catch when a url is about to load
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
    return true;
}
// here you execute an action when the URL you want is about to load
@Override
public void onLoadResource(WebView view, String url){
    if( url.equals("http://cnn.com") ){
        // do whatever you want
    }
}

I've shut off automatic image loading, network loads, and Javascript execution:

settings.setBlockNetworkLoads(true);
settings.setBlockNetworkImage(true);
settings.setJavaScriptEnabled(false);

But these do nothing as to preventing the capture of these requests.

Maybe there's a different procedure to capturing the link click, but it was either this or to stop the loading of external resources.

Question

How do I prevent WebView from capturing (or attempting to load) resource requests like CSS, JS, or images?

Otherwise if I can't prevent capturing or attempting to load, how can I differentiate between links clicked and web resources?

Thanks ahead!

Upvotes: 4

Views: 5809

Answers (3)

HelpingHand
HelpingHand

Reputation: 1065

I've found a way to ignore automated WebView resource requests.

By ignoring requests in the first second of WebView initialization, I am able to isolate user based clicks from the rest:

final Long time = System.currentTimeMillis()/1000;

//load up a WebView, define a WebViewClient for capturing link clicking
WebView webview = new WebView(this);
WebViewClient webviewClient = new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
        return true;
    }

    @Override
    public void onLoadResource(WebView view, String url){
        Long currentTime = System.currentTimeMillis()/1000;
        if (currentTime - time > 1) {
            //do stuff here
        }
    }
};

I have not tested this solution without blocking JavaScript execution and automatic image loading, but it should work regardless:

WebSettings settings = webview.getSettings();
settings.setBlockNetworkLoads(true);
settings.setBlockNetworkImage(true);
settings.setJavaScriptEnabled(false);

Upvotes: 1

Rafael Steil
Rafael Steil

Reputation: 4697

Short answer is, you can't.

A longer answer could be like this: you won't be able to do that because it is designed to be "capture all or capture nothing". Web requests are a general concept, not tied to a particular resource like images or css - in fact, it does not have any clue of what does are. That's why you won't find anything.

Do like this: in shouldOverrideUrlLoading, instead of returning true all the time, you only return true for the urls you want to handle yourself. For all other cases, like css and so forth, you return false, so the webview will take care of that for you.

For example:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String  url) {
    // Ignore css and js
    if (url.endsWith(".css") || url.endsWith(".js")) {
        return false;
    }

    return true;
}

Upvotes: 1

Gino Mempin
Gino Mempin

Reputation: 29630

You could override WebViewClient's shouldInterceptRequest and return some non-null response instead of the CSS, JS, images, etc. being fetched.

Example:

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        Log.d(TAG, "shouldInterceptRequest: " + url);

        if (url.contains(".css")
                || url.contains(".js")
                || url.contains(".ico")) { // add other specific resources..
            return new WebResourceResponse(
                    "text/css",
                    "UTF-8",
                    getActivity().getResources().openRawResource(R.raw.some_css));
        } else {
            return super.shouldInterceptRequest(view, url);
        }
    }

where R.raw.some_css is:

    body {
      font-family: sans-serif;
    }

Note:
I'm not sure what pages you're loading, but this approach may ruin the look of the page.

Upvotes: 1

Related Questions