执行 JavaScript 来渲染 HTML 以进行服务器端缓存
网站提供的许多小部件实际上是 JavaScript 代码,通过 DOM 操作或 document.write()
生成 HTML。 我不想通过额外的请求进一步减慢浏览器的速度,也不想相信另一个提供者能够快速、可靠且不会更改小部件输出,我想执行* JavaScript 来生成呈现的 HTML,然后保存该 HTML 源代码。
我研究过的事情似乎不可行或太困难:
- 链接浏览器(不是 lynx!)
- 无头使用 Xvfb 加 Firefox 加 Greasemonkey(yikes
- ) Java 浏览器工具包 Cobra(最好的选择!)
有什么想法吗?
** 显然你不能真正完全执行 JavaScript,因为它不一定有退出路径,但你明白了。
There are lots of widgets provided by sites that are effectively bits of JavaScript that generate HTML through DOM manipulation or document.write()
. Rather than slow the browser down even more with additional requests and trust yet another provider to be fast, reliable and not change the widget output, I want to execute* the JavaScript to generate the rendered HTML, and then save that HTML source.
Things I've looked into that seem unworkable or way too difficult:
- The Links Browser (not lynx!)
- Headless use of Xvfb plus Firefox plus Greasemonkey (yikes)
- The all-Java browser toolkit Cobra (the best bet!)
Any ideas?
** Obviously you can't really execute the JavaScript completely, as it doesn't necessarily have an exit path, but you get the idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
John Resig 的项目将浏览器连接到服务器: “浏览器/DOM 环境,用 JavaScript 编写,运行在 Rhino 之上;能够运行 jQuery 、Prototype 和 MochiKit(至少)。”
There's John Resig's project Bringing the Browser to the Server: "browser/DOM environment, written in JavaScript, that runs on top of Rhino; capable of running jQuery, Prototype, and MochiKit (at the very least)."
如果您只是使用纯 JS,Rhino 应该可以解决问题。 但如果 JS 代码实际上调用 DOM 方法等,您将需要一个成熟的浏览器。 Crowbar 可能会帮助你。
这真的会让用户的操作速度更快而不会引起兼容性问题吗?
If you're just using plain JS, Rhino should do the trick. But if the JS code is actually calling DOM methods and so on, you're going to need a full-blown browser. Crowbar might help you.
Is this really going to make things faster for users without causing compatibility issues?
Wikipedia 的“服务器端 JavaScript” 文章列出了众多实现,其中许多都基于Mozilla 的 Rhino JavaScript-to-Java 转换器,或其同类 SpiderMonkey(与 Firefox 和其他基于 Gecko 的浏览器中的引擎相同)。 特别是,像 Apache 的 mod_js 这样简单的东西可能会满足您的需求。
Wikipedia's "Server-side JavaScript" article lists numerous implementations, many of which are based on Mozilla's Rhino JavaScript-to-Java converter, or its cousin SpiderMonkey (the same engine as found in Firefox and other Gecko-based browsers). In particular, something simple like mod_js for Apache may suit your needs.