4

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!

Community
  • 1
  • 1
HelpingHand
  • 1,045
  • 11
  • 26

3 Answers3

1

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Thanks for your answer! I'm not really trying to alter or even preserve the CSS of the page. What I'm looking for is to be able to capture links clicked within the WebView without capturing it's attempts at fetching external resources. – HelpingHand Dec 02 '16 at 02:17
1

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;
}
Rafael Steil
  • 4,538
  • 4
  • 25
  • 30
  • Thanks for the answer. Is there any way to differentiate between a user clicked link and a resource load? – HelpingHand Dec 02 '16 at 00:37
  • Not what I am aware of. Of course you could make some assumptions, like "if it does not end with .css, .js, .jpg .png. gif etc etc, then must be a regular link". But this is weak and may lead to false positives, if the page has a resource you didn't map. If you have control over the webpage, you can always insert some magic string and look for it. – Rafael Steil Dec 02 '16 at 00:43
  • Isn't that what your solution does? – HelpingHand Dec 02 '16 at 00:44
  • Yes, but like I said in the previous comment, if you forget to map some extension (for example, a ".webm" file"), your code will "think" it is a user clicked link. However, I don't know any other way to do it. – Rafael Steil Dec 02 '16 at 00:47
  • Actually, I've found an alternative solution which doesn't require blocking certain resource requests... Just posting here in case you were interested. It's in my accepted answer. – HelpingHand Dec 04 '16 at 18:40
1

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);
HelpingHand
  • 1,045
  • 11
  • 26