How to Get the Web Page Contents from a Webview

How do I get the web page contents from a WebView?

I know this is a late answer, but I found this question because I had the same problem. I think I found the answer in this post on lexandera.com. The code below is basically a cut-and-paste from the site. It seems to do the trick.

final Context myApp = this;

/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(String html)
{
// process the html as needed by the app
}
}

final WebView browser = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);

/* Register a new JavaScript interface called HTMLOUT */
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
browser.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
});

/* load a web page */
browser.loadUrl("http://lexandera.com/files/jsexamples/gethtml.html");

Is there a way to access html data of a website using webview in android?

1) Implementation of a click detection on a button do the following:

Let me provide you a full example. For your the website http://store.nike.com/ch/de_de/pd/mercurial-superfly-v-tech-craft-2-herren-fussballschuh-fur-normalen-rasen/pid-11229711/pgid-11626158

  {
...

webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(this), "ButtonRecognizer");

webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
loadEvent(clickListener());
}

private void loadEvent(String javascript){
webview.loadUrl("javascript:"+javascript);
}

private String clickListener(){
return getButtons()+ "for(var i = 0; i < buttons.length; i++){\n" +
"\tbuttons[i].onclick = function(){ console.log('click worked.'); ButtonRecognizer.boundMethod('button clicked'); };\n" +
"}";
}

private String getButtons(){
return "var buttons = document.getElementsByClassName('add-to-cart'); console.log(buttons.length + ' buttons');\n";
}
});

webview.loadUrl("http://store.nike.com/ch/de_de/pd/mercurial-superfly-v-tech-cra\u200C\u200Bft-2-herren-fussballschuh-fur-normalen-rasen/pid-11229711/pgid-11626158");

...
}


class MyJavaScriptInterface {

private Context ctx;

MyJavaScriptInterface(Context ctx) {
this.ctx = ctx;
}

@JavascriptInterface
public void boundMethod(String html) {
new AlertDialog.Builder(ctx).setTitle("HTML").setMessage("It worked")
.setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
}
}

That'll change the onClick for the button to what you need.

Getting the elements by class (getElementsByClassName()) worked as expected, however not: getElementById(). Furthermore, it might be necessary to replace getElementsByClassName() by getElementsByName() such as for example on this website: https://www.digitec.ch/de/s1/product/lexon-flip-wecker-3522142. Where you would put getElementsByName('AddProductToCart')

Info: the \n and the weird combination of strings inside the clickListener method are set like this, because of the IDE. ( ";" are causing problems inside a string).


2) Implementation of a website data analysis such as detecting what a customer puts inside his shopping cart:

(The code takes website specific parts which are defined by tags/names etc and outputs them inside the Android Monitor). You have to define the tags and names for every website. To get them, simply have a look at the html code of the website.

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ShoppingCartAnalyzerActivity extends AppCompatActivity {


String adidas_tag = "product";
String nike_tag = "ch4_cartItem";
String zalando_tag = "z-coast-fjord_article";
String digitec_tag = "item-description";

String adidas_shoppingcart_url = "https://www.adidas.ch/on/demandware.store/Sites-adidas-CH-Site/de_CH/Cart-Show";
String nike_shoppingcart_url = "https://secure-store.nike.com/ch/checkout/html/cart.jsp?";
String zalando_shoppingcart_url = "https://www.zalando_tag.ch/cart/";
String digitec_shoppingcart_url = "https://www.digitec.ch/";

WebView webview;
private String[] shoppingCartItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webview = new WebView(this);
setContentView(webview);


webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(this), "ShoppingCartAnalyser");

webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
loadEvent(clickListener());
}

private void loadEvent(String javascript) {
webview.loadUrl("javascript:" + javascript);
}

private String clickListener() {
return getProducts() + "for(var i = 0; i < products.length; i++){\n" +
"\tconsole.log(products[i].innerText); ShoppingCartAnalyser.boundMethod(products[i].innerText,i,products.length); \n" +
"}";
}

private String getProducts() {
return "var products = document.getElementsByClassName('" + zalando_tag + "'); console.log(products.length + ' products');\n";
}
});

webview.loadUrl(zalando_shoppingcart_url);
}

private void displayItems() {
for (String shoppingCartItem : shoppingCartItems) {
Log.d("ShoppingCart", cleanString(shoppingCartItem));
}
}

private String cleanString(String shoppingCartItem) {
shoppingCartItem = shoppingCartItem.replace("Ändern", "");
shoppingCartItem = shoppingCartItem.replace("Entfernen", "");
shoppingCartItem = shoppingCartItem.replace("Bearbeiten", "");
shoppingCartItem = shoppingCartItem.replace("Löschen", "");
shoppingCartItem = shoppingCartItem.replace("icon-cart-minus", "");
shoppingCartItem = shoppingCartItem.replace("icon-cart-plus", "");
shoppingCartItem = shoppingCartItem.replace("Service + Zubehör", "");
shoppingCartItem = shoppingCartItem.replaceAll("(?m)^[ \t]*\r?\n", "");
return shoppingCartItem;
}

class MyJavaScriptInterface {

private Context ctx;

MyJavaScriptInterface(Context ctx) {
this.ctx = ctx;
}

@JavascriptInterface
public void boundMethod(String decodedShoppingCart, int i, int size) {
if (i == 0) {
shoppingCartItems = new String[size];
}
shoppingCartItems[i] = decodedShoppingCart;
if (i == size - 1) {
displayItems();
}
}
}
}

How to retrieve HTML content from WebView (as a string)

Unfortunately there is not easy way to do this.

See How do I get the web page contents from a WebView?

You could just make a HttpRequest to the same page as your WebView and get the response.

How to get the whole content text of html page displayed in webview

I got the content by this way

mainContent.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = mainContent.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
mainContent.requestFocusFromTouch();
mainContent.setWebChromeClient(new WebChromeClient());
mainContent.loadUrl("file:///android_asset/"+filename.get(position));
mainContent.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mainContent.setWebViewClient(null);
mainContent.loadUrl("javascript:window.HTMLOUT.processHTML('<div>'+document.getElementsByTagName('div')[0].innerHTML+'</div>');");

and in my activity

class MyJavaScriptInterface {
@SuppressWarnings("unused")
public void processHTML(final String html) {
runOnUiThread(new Runnable() {
public void run() {
Spanned page = Html.fromHtml(html);
System.out.println("content"+page);

}
});
}
}

How to display a part of the webpage on the webview android

Your are loading your html code without the proper structure (so all definitions in head are lost, like CSS references) and without the initial document (or loading with base url) all relative paths are broken.

<div class="darewod"> <a title="Workout of the Day" href="/workouts/lower-abs-workout.html" rel="alternate"><img src="/images/grid/wod/2016/wod_nov8.jpg" alt="Workout of the Day"></a> </div>

What you could do: replace the body of your document with your selected element, therby preserving the structure and information regarding base:

Example Code

WebView wv;
Handler uiHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

wv = (WebView)findViewById(R.id.webView);
wv.setWebViewClient(new MyWebViewClient());

new BackgroundWorker().execute();

}

// load links in WebView instead of default browser
private class MyWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}

@RequiresApi(21)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return false;
}
}

private class BackgroundWorker extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... arg0) {
getDarewod();
return null;
}

public void getDarewod(){

try {
Document htmlDocument = Jsoup.connect("http://darebee.com/").get();
Element element = htmlDocument.select("#gkHeaderMod > div.darewod").first();

// replace body with selected element
htmlDocument.body().empty().append(element.toString());
final String html = htmlDocument.toString();

uiHandler.post(new Runnable() {
@Override
public void run() {
wv.loadData(html, "text/html", "UTF-8");
}
});
} catch (IOException e) {
e.printStackTrace();
}
}

}

How to display some part of webpage in android webview?

I think what you want to do is remove some content from your HTML page and then display it in the WebView. This is possible via javascript so just before you display the page add some javascript code that will remove the elements you don't want.

LIKE

final WebView mWebView = (WebView) findViewById(R.id.mWebViewId);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
mWebView.loadUrl("javascript:(function() { " +
"document.getElementById('tableid')[0].style.display='none'; " +
"})()");
}
});
mWebView.loadUrl(youUrl);

Just replace document.getElementsByTagName('tableid') with document.getElementsByTagName('theElementYouWantToRemove') for every element and you're set. Original solution can be found at Display a part of the webpage on the webview android



Related Topics



Leave a reply



Submit