views:

3157

answers:

3

There are lots of widgets provided by sites that are effectively bits of JavaScript that generate HTML through DOM manipulation or document.write(). Rather than slow the browser down even more with additional requests and trust yet another provider to be fast, reliable and not change the widget output, I want to execute* the JavaScript to generate the rendered HTML, and then save that HTML source.

Things I've looked into that seem unworkable or way too difficult:

  1. The Links Browser (not lynx!)
  2. Headless use of Xvfb plus Firefox plus Greasemonkey (yikes)
  3. The all-Java browser toolkit Cobra (the best bet!)

Any ideas?

(*) Obviously you can't really execute the JavaScript completely, as it doesn't necessarily have an exit path, but you get the idea.

+2  A: 

Wikipedia's "Server-side JavaScript" article lists numerous implementations, many of which are based on Mozilla's Rhino JavaScript-to-Java converter, or its cousin SpiderMonkey (the same engine as found in Firefox and other Gecko-based browsers). In particular, something simple like mod_js for Apache may suit your needs.

Sören Kuklau
+1  A: 

If you're just using plain JS, Rhino should do the trick. But if the JS code is actually calling DOM methods and so on, you're going to need a full-blown browser. Crowbar might help you.

Is this really going to make things faster for users without causing compatibility issues?

Jon Bright
+2  A: 

You could also use Jaxer to run JavaScript server side.

There's also John Resig's project Bringing the Browser to the Server: "browser/DOM environment, written in JavaScript, that runs on top of Rhino; capable of running jQuery, Prototype, and MochiKit (at the very least)."

Sam Hasler
Resig's thing is exactly what I'm looking for.
matt