4

Take for example this snippet of code on a web page.

<html><body>
<script language="javascript">
document.write("The cat");
document.write(" sat on the mat");
</script>
</body></html>

If I were to retrieve this web page via CURL or WGET I would get that text because the Javascript has not been processed.

But I would like to retrieve this page, so I get the results of the Javascript output. So I would get just..

The cat sat on the mat

Is there some Linux Javascript sandbox/emulator/pre processor or something of that ilk that would allow me to process that text into html. I understand Javascript is complex and don't expect 100% conversion. But even to get some basic conversion would be helpful.

I know its possible as I'm sure Google does that when they index web pages to get the best results for the web pages they index.

Meer Borg
  • 916
  • 2
  • 8
  • 12

4 Answers4

7

There's no such thing as a “Javascript to HTML converter”. Javascript is a programming language, not a markup language. Browsers don't convert Javascript to HTML, they execute the Javascript code, and the effect of the Javascript code is to modify the HTML. What you're after is a Javascript interpreter that can process the HTML document.

One way to do this is to use a browser engine behind the scenes. Selenium and Watir (both web application testing engines) are popular choices to call a web browser and drive it with a script — see Are there any good tools besides SeleniumRC that can fetch webpages including content post-painted by JavaScript?.

There are also recent interfaces to Javascript and the DOM in several programming languages such as Jswebkit in Python (example).

Another possibility is to run node.js, which is a standalone JavaScript interpreter (example).

  • Thanks, it was difficult to state the question in a form where someone wouldn't try to explain to me the nature of javascript. But my example showed what I meant. The node.js looks promising, will try that out to see how well it does the job of "converting javascript to html" :) – Meer Borg Jul 23 '13 at 06:00
1

You can try elinks with Javascript support. Once it is build just type:

elinks --dump 1 http://www.example.com/my-js-page.html

and that should do it. Their documentation says that the Javascript support isn't great but this is another way of doing it.

0

Just for the record, there technically is a way to convert JavaScript to HTML.

I use inspect element in Chrome and FireFox a lot, which is the first place I learned HTML. If you paste a JavaScript in inspect element, it won't recognize it's function. I found a way around it. I taught myself everything I know which is what made it easier to figure this out. Here is an example:

Javascript:

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}
</script>

JavaScript to HTML:

getElementById('demo').innerHTML='Hello World';

HTML reads HTML codes differently than JavaScripts so I have to simplify the JavaScript to get the HTML code. Technically it is a JavaScript since it carries out the JavaScript function but it is still HTML because it is in HTML form.

jasonwryan
  • 73,126
0

I recommend using phantomJS (or casperJS, which sits on top of phantomJS) for doing such tasks. console.log() is used to log to stdout. You could even do screen captures.