Any way to hide elements from webview? (android)

前端 未结 3 2009
孤城傲影
孤城傲影 2020-12-09 11:24

There\'s a webpage I pull up with webview, however i\'d like to hide the 1 text link at the top. Is there a way to do this? The link is in the body, so I can\'t hide the bod

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 11:36

    I use WebViewSuite

    and implement this

    webViewSuite = findViewById(R.id.webViewSuite);
    webViewSuite.startLoading("https://example.com/blog/");
    

    and added customizeClient to WebViewSuite

    webViewSuite.customizeClient(new WebViewSuite.WebViewSuiteCallback() {
          @Override
          public void onPageStarted(WebView view, String url, Bitmap favicon) {
          }
    
          @Override
          public void onPageFinished(WebView view, String url) {
            hideSomeSectionOfBlog(view);
          }
    
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
          }
        });
    

    and use function to hide elements

     private void hideSomeSectionOfBlog(WebView view) {
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl("javascript:(function() { " +
          "document.getElementById('Top_bar').style.display='none';" +
          "document.getElementById('Filters').style.display='none';" +
          "document.getElementById('Footer').style.display='none';" +
          "document.getElementsByClassName('Header').style.display='none';" +
          "})()");
      }
    

    Hope to be useful

    Note: if id not exist JavaScript get error. example if Filters do not exists id ,Footer and Header do not display='none' If you do not trust Separate like this

    view.loadUrl("javascript:(function() { " +
          "document.getElementById('Footer').style.display='none';})()");
    view.loadUrl("javascript:(function() { " +
          "document.getElementById('Header').style.display='none';})()");
    

提交回复
热议问题