JavaScript to Html

74 Views Asked by At

Given a web page with JavaScript code, I would like to generate a resulting html automatically (either via CLI tool OR using some library in some language)

For example, given test.html

<!DOCTYPE html>
<html>
  <body>
    <p id="demo"></p>
    <script>
      document.getElementById("demo").innerHTML = "Hello JavaScript!";
    </script>
  </body>
</html>

I would like to get as a result

<html>
  <body>
    <p id="demo">Hello JavaScript!</p>
    <script>
      document.getElementById("demo").innerHTML = "Hello JavaScript!";
    </script> 
  </body>
</html>
2

There are 2 best solutions below

0
Timofey On BEST ANSWER

The answer is based on the comment of @torazaburo

In fact, the phantomjs is capable of evaluating javascript and producing html.

Here is how it could look like, executing phantomjs load_page.js path_to/test.html

var page = require('webpage').create(),
    system = require('system'),
    page_address;
var fs = require('fs');
if (system.args.length === 1){
  console.log('Usage: phantomjs ' + system.args[0] + ' <page_to_load:http://www.google.com>');
  phantom.exit();
}
page_address = system.args[1]

page.open(page_address, function(status){
    console.log('Status:' + status);
    if (status === 'success' ){
      fs.write('phantom_result.html', page.content, 'w')
    }
    phantom.exit();
});
2
Darren Gourley On

After a quick search, it looks like watin will do what you want.

It's aimed at automated testing, but when it hits a page it will execute all js as well as ajax calls etc. Looks like you can grab the resulting html from it too.