Alexx Perez
Alexx Perez

Reputation: 215

android html code from webview

hello i'm using this example http://lexandera.com/2009/01/extracting-html-from-a-webview/ to get the HTML from a webview. But i need to use it in my superclass and i dont know how to do this. I just can see the html on a AlertDialog but i cant use it. How can I return it to my main class as String?

final Context myApp = this;  

/* An instance of this class will be registered as a JavaScript interface */  
class MyJavaScriptInterface  
{  
    @SuppressWarnings("unused")  
    public void showHTML(String html)  
    {  
        new AlertDialog.Builder(myApp)  
            .setTitle("HTML")  
            .setMessage(html)  
            .setPositiveButton(android.R.string.ok, null)  
        .setCancelable(false)  
        .create()  
        .show();  
    }  
}  

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.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");  
    }  
});  

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

Upvotes: 2

Views: 6170

Answers (2)

gtiwari333
gtiwari333

Reputation: 25156

Do the Followings :

Setting up WebView

First add JavaScriptInterface to webView as follows. Here "Android" will be used later to call functions in JavaScriptInterface later.

  webView = (WebView) findViewById(R.id.webView);
  WebSettings webSettings = webView.getSettings();
  webSettings.setJavaScriptEnabled(true);
  webView.addJavascriptInterface(new JavaScriptInterface(this), "MyAndroid");

The JavaScriptInterface Class:

 public class JavaScriptInterface {
     Context mContext;
     JavaScriptInterface(Context c) {
         mContext = c;
     }
}

HTML File:

  <html>
    <head>
    <script type="text/javascript">
    function getValue()
      {
        var x=document.getElementById("content").innerHTML;
        //  alert(x);
        MyAndroid.receiveValueFromJs(x);
      }
    </script>
    </head>
    <body>
    <div id="content">
    This is html content <hr/> 
    Other contents;
    <input type="button" onclick="getValue()" value="Get Content" />
    </div>
    </body>
    </html>

Calling Java Script Function

  //this calls javascript function
  webView.loadUrl("javascript:getValue()");

Adding Callback function in JavaScriptInterface for MyAndroid.receiveValueFromJs(val):

public void receiveValueFromJs(String str) {
  Toast.makeText(mContext, "Received Value from JS: " + str,Toast.LENGTH_SHORT).show();
}

Here you have returned HTML in str variable.

You can see my blog : http://ganeshtiwaridotcomdotnp.blogspot.com/2011/10/calling-javascript-function-from.html for details.

I found another solution : Is it possible to get the HTML code from WebView

Upvotes: 2

gosr
gosr

Reputation: 4708

Can't you just make a global variable, like

String globalHtml;

And then assign it in the showHTML method?

globalHtml = html;

You can delete the AlertDialog code if you don't need it.

Upvotes: 0

Related Questions