Any way to hide elements from webview? (Android)

There is a webpage that I pull using webview, however I would like to hide 1 text link at the top. Is there any way to do this? The link is in the body, so I cannot hide the element of the body as a whole. A web page is all the text and one small image at the bottom, but the text is generated every time it is downloaded, so I can’t just copy / paste the body.

thanks

+13
android webview
source share
4 answers
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); 
+21
source share

I understood! By entering javascript, I had to use

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

Deletes the link (code). Replacing ('a') with ('img') will delete the images.

(thanks lexanderA - Embedding JavaScript in a WebView )

+6
source share

I am new to Android Studio, I was able to create an application with codes found on the Internet to show that my site is available on Google Play.

This is MainActivity.java:

  package mypackage; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { public WebView mywebview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mywebview=(WebView)findViewById(R.id.webview); WebSettings WebSEttings=mywebview.getSettings(); WebSEttings.setJavaScriptEnabled(true); // Code to reflect changes into DOM wbesite in app WebSEttings.setDomStorageEnabled(true); mywebview.loadUrl("https://www.example.com"); //Code for opening the app mywebview.setWebViewClient(new WebViewClient()); } //Code for the back button @Override public void onBackPressed() { if (mywebview.canGoBack()) { mywebview.goBack(); } else { super.onBackPressed(); } } } 

I have included JavaScript and some code for the changes made to the website so that they are reflected in the application. Works great.

But I tried to hide the identifier (or class) without success. What I used:

// hide the element by the class name webview.loadUrl ("javascript: (function () {" + "document.getElementsByClassName ('your_class_name') [0] .style.display = 'none';}) ()");

in a public void. Can someone tell me what I am missing here and why this is not working? I would appreciate it.

Thanks in advance.

0
source share

I am using WebViewSuite

and realize it

 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 the function to hide the 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 helpful

Note: if the identifier does not exist, JavaScript receives an error. example, if there are no id filters, the footer and header are not displayed = 'none' if you do not trust to split like this

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

All Articles