18

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 body element in whole. The webpage is all text, and one tiny image at the bottom, but the text is generated each time you load it, so I can't just copy/paste the body.

Thanks

Maddy
  • 4,525
  • 4
  • 33
  • 53
ajent
  • 273
  • 1
  • 2
  • 6

3 Answers3

26
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);
Adharsh M
  • 2,773
  • 2
  • 20
  • 33
Linh
  • 433
  • 6
  • 11
8

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 )

Cassie
  • 5,223
  • 3
  • 22
  • 34
ajent
  • 273
  • 1
  • 2
  • 6
1

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';})()");
abolfazl bazghandi
  • 935
  • 15
  • 29