2

If I use:

String plain = Html.fromHtml(html).toString;

to render simple 'html' that contains:

<!doctype html>
<html>
  <head><meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Google</title>
  </head>
  <body>any plain vanila HTML goes here
  </body>

All is nice and dandy.

But what if that page contains tons of Javascript code that is nicely rendered by all web browsers but isn't available to me?

Is there a renderer that takes care of the Javascript as well, to output HTML or plaintext, that isn't necessarily going to a visual display?

(I know about WebView but my understanding that I can't really access its output. Or can I?)

Regex Rookie
  • 10,432
  • 15
  • 54
  • 88
  • And what if that tons of JS means the page could pull up any one of 10,000+ different videos or documentation files (or whatever)? Would you expect the output to include static HTML variants for all 10,000+ pages? – Andrew Thompson Mar 08 '11 at 13:45
  • @Andrew Go to http://youtu.be/FuXEXBg2giI, select all (Ctrl+A), Copy (Ctrl+C), then paste it to an HTML editor (e.g. Komposer). Did you receive HTML variants for *all* possible outputs of the tons of Javascript code that you see when you view the page source? – Regex Rookie Mar 08 '11 at 20:40

1 Answers1

1

Is there a renderer that takes care of the Javascript as well, to output HTML or plaintext, that isn't necessarily going to a visual display?

WebView or bust.

(I know about WebView but my understanding that I can't really access its output. Or can I?)

  1. Create a Java object to receive your output
  2. Add that Java object to the WebView via addJavascriptInterface()
  3. Use loadUrl("javascript:...") on the WebView to invoke a hunk of Javascript that gathers your information and calls a method on your Java object
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @CommonsWare Thank you so much. Do you happen to know of a sample or an example that demonstrates what you described? – Regex Rookie Mar 09 '11 at 15:25
  • @Regex Rookie: Well, this sample shows the Android portions of it: https://github.com/commonsguy/cw-advandroid/tree/master/WebView/GeoWeb2 However, figuring out the "hunk of Javascript that gathers your information" is up to you. – CommonsWare Mar 09 '11 at 15:44
  • Note to self: In this sample, **Locater** is the Java object serving as the Javascript interface. Also note the use of a JSON object. – Regex Rookie Mar 10 '11 at 16:55
  • 1
    @Regex Rookie: The JSON object is for atomicity of the data. I had an earlier version of `Locater` that had `getLatitude()` and `getLongitude()` methods. An astute reader of the book pointed out that a GPS fix might arrive between those two calls, resulting in an invalid latitude/longitude pair. The JSON structure allows `Locater` to return both latitude and longitude at one time. – CommonsWare Mar 10 '11 at 18:19
  • @CommonsWare What do you mean by "hunk of Javascript that gathers your information"? – Regex Rookie Mar 11 '11 at 17:02
  • @Regex Rookie: Here's a fully-worked-out solution: http://stackoverflow.com/questions/2376471/how-do-i-get-the-web-page-contents-from-a-web-view/4892013#4892013 – CommonsWare Mar 11 '11 at 17:03