使用以下代码获取 Javascript 文件
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);
}
我想将上述 javascript 的结果作为带有代码的 html 文件
<html>
<body>
<div id="pzt">
<iframe src="http://www.abc.com" width="100"> </iframe>
</div>
</body>
</html>
我尝试了spidermonkey,但没有用。有没有其他方法来处理或转换 javascript 文件以获取上面指定的 html。谢谢。
最佳答案
这是一个使用 Node.js 的解决方案,我无法告诉你一个使用 SpiderMonkey 的解决方案。与 Node.js 一样,SpiderMonkey 没有浏览器环境,因为它只是一个 JavaScript 引擎。但是 Node.js 有许多用于模拟浏览器环境的库。因此我建议您使用 Node.js 而不是 SpiderMonkey。
npm install domino
(您将需要 Node.js )- 将您的 js 源(例如
dom.js
)更改为:
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);
- 然后
node dom.js > out.html
关于Javascript 内容到 HTML 内容转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21877979/