tags:

views:

82

answers:

1

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'; " +
// "})()");

+2  A: 

If it is your own page, you could use addJavascriptInterface() on WebView to make a Java object available to Javascript, and have Javascript call a method on the object to provide string representations of the DOM (or pieces of it).

Beyond that, there is no getPageSource() method or the equivalent -- sorry!

CommonsWare
Would you be so kind as to suggest some javascript to use?
gregm
I'm not much of a Javascript coder, so I can't. I know there are ways of getting at the DOM, but I don't know the scope of what can be achieved.
CommonsWare
Does anyone out there in StackOverflow land know the answer?
gregm