渲染HTML +的Javascript服务器端 [英] Rendering HTML+Javascript server-side

查看:115
本文介绍了渲染HTML +的Javascript服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要渲染HTML页面的服务器端和提取canvas元素的原始字节,所以我可以将它保存为PNG。问题是,canvas元素可以从JavaScript(我使用jQuery的海军报以生成图表,基本上)创建的。所以我想我需要一种方法来主持从浏览器的DOM + JavaScript功能,而无需实际使用的浏览器。我在MSHTML(但开放的任何及所有建议)结转似乎它应该能够到这一点。这是一个ASP.NET MVC项目。

I need to render an HTML page server-side and "extract" the raw bytes of a canvas element so I can save it to a PNG. Problem is, the canvas element is created from javascript (I'm using jquery's Flot to generate a chart, basically). So I guess I need a way to "host" the DOM+Javascript functionality from a browser without actually using the browser. I settled on mshtml (but open to any and all suggestions) as it seems that it should be able to to exactly that. This is an ASP.NET MVC project.

我已经搜查甚广,并没有看到任何确凿的。

I've searched far and wide and haven't seen anything conclusive.

所以我有这个简单的HTML - 例如尽可能的简单说明问题 -

So I have this simple HTML - example kept as simple as possible to demonstrate the problem -

<!DOCTYPE html>
<html>
<head>
    <title>Wow</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js" type="text/javascript"></script>
</head>
<body>
    <div id="hello">
    </div>
    <script type="text/javascript">
        function simple() 
        {
            $("#hello").append("<p>Hello</p>");
        }                    
    </script>
</body>
</html>

从浏览器中运行时产生预期的输出。

which produces the expected output when run from a browser.

我希望能够以原始的HTML加载到内存中,执行JavaScript函数,然后操纵最终的DOM树。我不能使用任何System.Windows.WebBrowser状类,如我的code需要在服务环境中运行。

I want to be able to load the original HTML into memory, execute the javascript function, then manipulate the final DOM tree. I cannot use any System.Windows.WebBrowser-like class, as my code needs to run in a service environment.

因此​​,这里是我的code:

So here's my code:

IHTMLDocument2 domRoot = (IHTMLDocument2)new HTMLDocument();

        using (WebClient wc = new WebClient())
        {
            using (var stream = new StreamReader(wc.OpenRead((string)url)))
            {
                string html = stream.ReadToEnd();
                domRoot.write(html);
                domRoot.close();
            }
        }

        while (domRoot.readyState != "complete")
            Thread.Sleep(SleepTime);

        string beforeScript = domRoot.body.outerHTML;

        IHTMLWindow2 parentWin = domRoot.parentWindow;            
        parentWin.execScript("simple");

        while (domRoot.readyState != "complete")
            Thread.Sleep(SleepTime);


        string afterScript = domRoot.body.outerHTML;

        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(domRoot);
        domRoot = null;

的问题是,beforeScript和afterScript是完全相同的。该实例的IHTMLDocument2经过正常的初始化,加载,完整的循环,没有错误抛出,什么都没有。

The problem is, "beforeScript" and "afterScript" are exactly the same. The IHTMLDocument2 instance goes through the normal "uninitialized", "loading", "complete" cycle, no errors are thrown, nothing.

任何人有我做错了任何想法?这里完全丧失。

Anybody have any ideas on what I'm doing wrong? Completely lost here.

推荐答案

我发现 Awesomium 确实的究竟的我所需要的! 窗口的网页浏览器框架。辉煌。

I found Awesomium Does exactly what I need! "Windowless web-browser framework". Brilliant.

这篇关于渲染HTML +的Javascript服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆