2

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");  
Alexx Perez
  • 215
  • 2
  • 10
  • 19

2 Answers2

2

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

Community
  • 1
  • 1
gtiwari333
  • 24,554
  • 15
  • 75
  • 102
  • this is the same thing that in the example, replacing AlertDialog by Toast. I just want to have access to html string after webView.loadUrl in the main class not in class JavaScriptInterface.I've tried in main class: JavaScriptInterface script = new JavaScriptInterface(); script.loadhtml(); //// class JavaScriptInterface { public string loadhtml(){return html}} but doesn't work – Alexx Perez Nov 04 '11 at 12:54
0

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.

gosr
  • 4,593
  • 9
  • 46
  • 82