javascripthtmlcommand-line-interfacehtml-generation

JavaScript to Html


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>

Solution

  • 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();
    });