阻止 WebView 加载特定的 URL。

huangapple go评论85阅读模式
英文:

Prevent WebView from loading specific URLs

问题

private class mWebViewClient extends WebViewClient {
/* @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

}*/

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);

}
@Override
public void onLoadResource(WebView view, String url) {
   view.loadUrl("javascript:var footer = document.getElementById(\"footer\"); footer.parentNode.removeChild(footer); var header = document.getElementById(\"header_main\"); header.parentNode.removeChild(header); var bread = document.getElementsByClassName('breadcrumb breadcrumbs avia-breadcrumbs')[0].style.display='none';");

}

}

英文:

I am developing a web view app for my company website. In the articles pages, we have links to related products. For example site.com/product/product name. How can I disable all the link that starts with site.com/product/?
I have searched here and other websites but I could not find a solution.

 private class mWebViewClient extends WebViewClient {
   /* @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
       
    }*/
   @Override
   public void onPageFinished(WebView view, String url) {
       super.onPageFinished(view, url);
       progressBar.setVisibility(View.GONE);
   }
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
       super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.VISIBLE);
        

    }
    @Override
    public void onLoadResource(WebView view, String url) {
       view.loadUrl("javascript:var footer = document.getElementById(\"footer\"); footer.parentNode.removeChild(footer); var header = document.getElementById(\"header_main\"); header.parentNode.removeChild(header); var bread = document.getElementsByClassName('breadcrumb breadcrumbs avia-breadcrumbs')[0].style.display='none'");



    }
}

答案1

得分: 1

Sure, here's the translated code:

实现以下方法 `shouldOverrideUrlLoading()`:

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        if (request.getUrl().toString().contains("site.com/product/")) {
            return true;
        } else {
            return super.shouldOverrideUrlLoading(view, request);
        }
    }
英文:

Implement the method shouldOverrideUrlLoading() as following:

@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    if(request.getUrl().toString().contains("site.com/product/")) {
        return true;
    }
    else {
        return super.shouldOverrideUrlLoading(view, request);
    }
}

huangapple
  • 本文由 发表于 2020年7月24日 17:00:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63070309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定