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.

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 6: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

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.

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.

-1
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- sadiq khatri -->
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-6007294001515858"
     data-ad-slot="2073708321"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
  • 2
    A little more effort in describing where to type this and how this works would be welcome. Can you please provide that? – Anthon Feb 20 '15 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.