Remove Div From A Webpage For Webview Android Using Jsoup
I want to partially view a webpage on webview android and remove some div element from the webpage. I have a webpage like this
Solution 1:
You can do this without using Jsoup you know. Just use plain old javascript. The following code will show how to remove an element from the HTML page and display the rest.
finalWebViewmWebView= (WebView) findViewById(R.id.mWebViewId);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(newWebViewClient() {
@OverridepublicvoidonPageFinished(WebView view, String url)
{
mWebView.loadUrl("javascript:(function() { " +
"document.getElementById('a')[0].style.display='none'; " +
"})()");
}
});
mWebView.loadUrl(youUrl);
Solution 2:
Remove it from the document by selecting it and then using the remove
-method.
doc.select("div#a").remove();
System.out.println(doc);
Example:
Document doc = Jsoup.parse(html);
System.out.println("Before removal of 'div id=\"a\"' = ");
System.out.println("-------------------------");
System.out.println(doc);
doc.select("div#a").remove();
System.out.println("\n\nAfter removal of 'div id=\"a\"' = ");
System.out.println("-------------------------");
System.out.println(doc);
will result in
Before removal of 'div id="a"' =
-------------------------
<!DOCTYPE html><html><head></head><body><divid="a"><p>Remove aa</p></div><divid="b"><p>bb</p></div></body></html>
After removal of 'div id="a"' =
-------------------------
<!DOCTYPE html><html><head></head><body><divid="b"><p>bb</p></div></body></html>
Solution 3:
I had tried to use Jsoup to do something similar before, but my app always crash. If you are open to using Javascript only (which helps to make your app size smaller), here is what I did for my app:
webview3.setWebViewClient(newWebViewClient() {
@OverridepublicvoidonPageFinished(WebView view, String url) {
view.loadUrl("javascript:var con = document.getElementById('a'); " +
"con.style.display = 'none'; ");
}
});
Hope my Javascript is correct. The idea here is to use Javascript to hide the div after the page has finished loading.
Post a Comment for "Remove Div From A Webpage For Webview Android Using Jsoup"