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");  

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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
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.

1 Comment

but i have 2 diferent class. html is in subclass. i need return it. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.