本文介绍了阻止Android WebView尝试加载/捕获资源,如loadData()上的CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景



这似乎与许多其他问题是重复的。相信我,它不是。



我试图加载HTML数据到,能够捕获用户超链接请求。在这个过程中,我发现了,它完全符合我想要做的事情,除了捕获其他请求CSS文件和图片:

  //当网址即将加载
时,您告诉您要捕获的webclient @Override
public boolean shouldOverrideUrlLoading(WebView view,WebResourceRequest request){
return true;
}
//在这里你执行一个动作,当你想要的URL加载时
@Override
public void onLoadResource(WebView view,String url){
if(url.equals(http://cnn.com)){
//做任何你想做的事
}
}

我已经关闭了自动图片加载,网络加载和Javascript执行:

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

但是这些操作并没有阻止捕获这些请求。



也许有不同的过程来捕获链接点击,但是这或者是停止加载外部资源。



问题 h2>

如何防止 WebView 捕获(或尝试加载)CSS,JS或图像等资源请求?



否则,如果我无法阻止捕获或尝试加载,我该如何区分点击的链接和网络资源?

解决方案

我找到了一种忽略自动化WebView资源请求的方法。
$ b

通过在第一次WebView初始化时忽略请求,我可以将其他用户的点击隔离开来:

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

//载入一个WebView,定义一个WebViewClient捕获链接,单击
WebView webview = new WebView(this);
WebViewClient webviewClient = new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view,WebResourceRequest request){
return true;

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

我没有测试过这个解决方案,没有阻止JavaScript执行和自动图片加载,但它应该工作,不管:

  Web设置设置= webview.getSettings(); 
settings.setBlockNetworkLoads(true);
settings.setBlockNetworkImage(true);
settings.setJavaScriptEnabled(false);


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!

解决方案

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

这篇关于阻止Android WebView尝试加载/捕获资源,如loadData()上的CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 17:44