javascript - Stop Android WebView from trying to loadcapture resources like CSS on loadData() - Stack Overflow

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

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("") ){
        // 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!

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

Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Dec 1, 2016 at 23:57 HelpingHandHelpingHand 1,06511 silver badges27 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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.

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;
}

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745160864a4614384.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信