5

How I get the web page's source from WebView?

I want to only enter www.google.com in my webview and When I entered this site, I want to get the source for example

String a=........;(source) 
Graham Smith
  • 25,627
  • 10
  • 46
  • 69
CompEng
  • 7,161
  • 16
  • 68
  • 122
  • Do you want the whole source code or just the HTML tags? – Graham Smith Apr 01 '12 at 17:55
  • I want to whole source code . (for example when I paste it in .txt and open it with browser I want to see the site ) – CompEng Apr 01 '12 at 17:57
  • 1
    Yes I do and I have submitted an edit and I am now flagging this as a exact duplicate of http://stackoverflow.com/questions/4543663/is-there-a-way-to-get-the-html-in-a-webview-in-an-android-app – Graham Smith Apr 01 '12 at 18:08
  • I m sorry.I do not Understand really.What you mean addJavascriptInterface(). it is static where can ı find this – CompEng Apr 01 '12 at 18:14

3 Answers3

13

I am not sure how far this is going to be helpful. But I have used the below snippet to fetch a small html page's data. I hope it helps you.

Create a class like the one below,

  class MyJavaScriptInterface
  {
      @SuppressWarnings("unused")
      public void processHTML(final String html)
      {
          Log.i("processed html",html);

            Thread OauthFetcher=new Thread(new Runnable() { 

                @Override
                public void run() {

                    String oAuthDetails=null;
                      oAuthDetails=Html.fromHtml(html).toString();
                      Log.i("oAuthDetails",oAuthDetails);

                }
            });OauthFetcher.start();
        }
      } 

Now in your onCreate(),

 webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

     webview.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageFinished(WebView view, final String url) {


                String oAuthUrl=getString("www.google.com");

                if(url.contains(oAuthUrl))
                {
                    Log.i("Contains","Auth URL");

                    twitter_webview.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
                }
            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                progressDialog.show();
            }
      });

And now what happens is that, when your page finishes loading, the JavaScript class will be called, which would retrieve the page source and store it in a String as your requirement.

TomTasche
  • 5,448
  • 7
  • 41
  • 67
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • Bitmap and progressDialogit does not seen and does not import,I change twitter_webview to webvie ? what can ı do – CompEng Apr 02 '12 at 16:10
  • isn't that the question is all about? To fetch the page source? That's why I read the page source as a String and store it.. – Andro Selva Aug 16 '13 at 04:16
  • sorry. Got your question wrong. The thread is to avoid any interference to the Main UI thread. if the web page, then to download its content into a string will take a long time. If we dont do this in a secondary thread, then the main thread gets blocked and you might get ANR dialog. – Andro Selva Aug 19 '13 at 04:20
  • @JavascriptInterface annotation should be there in processHTML() method. – Smeet Aug 26 '22 at 08:19
7

If your minSdkVersion is 19 or greater you can use the following.


override fun onPageFinished(view: WebView?, url: String?) {
    super.onPageFinished(view, url)
    view?.evaluateJavascript("""(function() {
        return "<html>" + document.getElementsByTagName('html')[0].innerHTML + "</html>";
    })()""".trimMargin()) {
        console.log(it)
    }
}

Jeeva
  • 3,975
  • 3
  • 23
  • 47
6

And For API 17

import android.webkit.JavascriptInterface;

public class MainActivity extends Activity {

final Context myApp = this;

@JavascriptInterface
public void processHTML(String html) {
    if (html == null)
        return;

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final WebView browser = (WebView) findViewById(R.id.webview);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.addJavascriptInterface(this, "HTMLOUT");
    browser.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            browser.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
        }
    });
    browser.loadUrl("http://www.google.co.il");
}

}
TomTasche
  • 5,448
  • 7
  • 41
  • 67
shai levi
  • 61
  • 1
  • 1