Search code examples
javascripthtmlspidermonkey

Javascript content to HTML content converter


Take a Javascript file with the following code

var pzt = document.createElement('iframe');
pzt.src = 'http://www.abc.com';
pzt.style.width = '100px';
if (!document.getElementById('pzt')) {
    document.write('<div id=\'pzt\'></div>');
    document.getElementById('pzt').appendChild(pzt);
}

I would like to get the result of the above javascript as an html file with the code

<html>
<body>
<div id="pzt">
<iframe src="http://www.abc.com" width="100"> </iframe>
</div>
</body>
</html>    

I tried spidermonkey but there is no use. is there any other way to process or convert the javascript file to get the html as specified above. Thanks.


Solution

  • This is a solution with Node.js, I can't tell you one with SpiderMonkey. Like Node.js, SpiderMonkey has no browser env as it's only a JavaScript engine. But Node.js has many library for simulating a browser env. Thus I suggest you to use Node.js instead of SpiderMonkey.

    1. npm install domino (you will need Node.js)
    2. Change your js source (let's say dom.js) to:
    var domino = require('domino');
    var window = domino.createWindow();
    var document = window.document;
    var pzt = document.createElement('iframe');
    pzt.src = 'http://www.abc.com';
    pzt.style.width = '100px';
    if (!document.getElementById('pzt')) {
        document.body.innerHTML = '<div id=\'pzt\'></div>';
        document.getElementById('pzt').appendChild(pzt);
    }
    console.log(document.documentElement.outerHTML);
    
    1. Then node dom.js > out.html