Any way to hide elements from webview? (android)

前端 未结 3 1986
孤城傲影
孤城傲影 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:35

    I got it! By injecting javascript I had to use

    webview.loadUrl("javascript:(function() { " + "document.getElementsByTagName('a')[0].style.display = 'none'; " + "})()");

    That removes the link ( code). Replacing ('a') with ('img') would remove the images.

    (thanks lexanderA - Injecting JavaScript into a WebView )

    0 讨论(0)
  • 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';})()");
    
    0 讨论(0)
  • 2020-12-09 11:50
    final WebView webview = (WebView)findViewById(R.id.browser);
    
        webview.getSettings().setJavaScriptEnabled(true);
    
        webview.setWebViewClient(new WebViewClient() {
         @Override
        public void onPageFinished(WebView view, String url)
        {
            // hide element by class name
            webview.loadUrl("javascript:(function() { " +
                    "document.getElementsByClassName('your_class_name')[0].style.display='none'; })()");
            // hide element by id
            webview.loadUrl("javascript:(function() { " +
                    "document.getElementById('your_id').style.display='none';})()");
    
        }
        });
    
    webview.loadUrl(url);
    
    0 讨论(0)
提交回复
热议问题