Take the 2-minute tour ×

On Android, I have a WebView that is displaying a page.

How do I get the page source without requesting the page again?

It seems WebView should have some kind of getPageSource() method that returns a string, but alas it does not.

If I enable JavaScript, what is the appropriate JavaScript to put in this call to get the contents?

webview.loadUrl("javascript:(function() { " +  
    "document.getElementsByTagName('body')[0].style.color = 'red'; " +  
share|improve this question
use jquery script and js interface to get html content from webview window.interface.processHTML($(\"body\").html()); –  DroidBot Jun 21 '13 at 12:17
add comment

4 Answers

up vote 64 down vote accepted

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;

class MyJavaScriptInterface
    public void processHTML(String html)
        // process the html as needed by the app

final WebView browser = (WebView)findViewById(R.id.browser);

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

/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url)

/* load a web page */
share|improve this answer
Beware that this might not be the raw HTML of the page; the page content may have changed dynamically through JavaScript before onPageFinished() was executed. –  Paul Lammertsma Dec 13 '11 at 18:43
It's great, but calling the method browser.loadUrl in onPageFinished will cause onPageFinished to be called again. You might want to check whether it is the first call of onPageFinished or not before calling browser.loadUrl. –  Yi Huang Dec 30 '12 at 8:57
Thanks @Blundell It worked to me. I'd like to know how could this be implemented as a service . Since is a service without a layout and webview to store the results. Is there a way to put the data in some other object different from the webView so we can put the javascript to get the resulting html code? –  Totalys Feb 12 at 3:19
@Totalys that's even easier String html = new Scanner(new DefaultHttpClient().execute(new HttpGet("www.the url")).getEntity().getContent(), "UTF-8").useDelimiter("\\A").next(); (abbreviated to fit in a comment :-) ) –  Blundell Feb 12 at 9:31
Thanks @Blundell , but I put this line and then: MyJavaScriptInterface mji = new MyJavaScriptInterface(); mji.showHTML(html); and comment everything related to the webview (since I don't have this component) but this way, I just get the html without the javascript processed... Is anything missing? –  Totalys Feb 13 at 2:59
show 1 more comment

Per issue 12987, Blundell's answer crashes (at least on my 2.3 VM). Instead, I intercept a call to console.log with a special prefix:

// intercept calls to console.log
web.setWebChromeClient(new WebChromeClient() {
    public boolean onConsoleMessage(ConsoleMessage cmsg)
        // check secret prefix
        if (cmsg.message().startsWith("MAGIC"))
            String msg = cmsg.message().substring(5); // strip off prefix

            /* process HTML */

            return true;

        return false;

// inject the JavaScript on page load
web.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String address)
        // have the page spill its guts, with a secret prefix

share|improve this answer
Thx, this works on 2.3.6 –  Snicolas Apr 2 '12 at 10:13
I know I'm late to the party but this works very well, and I reused this code to "upgrade" the ChildBrowser Phonegap plugin to send an update msg back to the host app with the (computed) html. This will make oAuth on various APIs quite easier. Yes Google API oAuth2, I am looking at you. –  dda May 19 '12 at 6:43
add comment

This is an answer based on jluckyiv's, but I think it is better and simpler to change Javascript as follows.

share|improve this answer
add comment

Have you considered fetching the HTML separately, and then loading it into a webview?

String fetchContent(WebView view, String url) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);
    HttpResponse response = httpClient.execute(get);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    HttpEntity entity = response.getEntity();
    String html = EntityUtils.toString(entity); // assume html for simplicity
    view.loadDataWithBaseURL(url, html, "text/html", "utf-8", url); // todo: get mime, charset from entity
    if (statusCode != 200) {
        // handle fail
    return html;
share|improve this answer
add comment

Your Answer


