使用JavaScript的评估为纯文本C#.NET 3.5 [英] Evaluate javascript to plain text using C#, .NET 3.5

查看:125
本文介绍了使用JavaScript的评估为纯文本C#.NET 3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何评估的文件撰写的JavaScript在C#中明文?我想,以评估这一点:

 <脚本类型=文/ JavaScript的>
一个= 2; B = 3;
文件撰写(A +_+ Y);
< / SCRIPT>
 

这样:

  2_3
 

解决方案

从你的评论,这是一个下载的HTML页面上的客户端功能,这听起来像你正在做某种屏幕刮/爬行,其中, HTML / JavaScript的都赚不到客户要求,你的应用程序?

如果我理解正确的话,这是你追求什么,那么你就需要一个跨preTER,可以说话的JavaScript。 C#不能做到这一点,所以下一个最好的事情是火了在你的C#应用​​程序的组成部分,它能够理解/跨preting(因此评估)的JavaScript。

我会建议寻找到WebBrowser控件和HtmlDocument.DomDocument,加载您下载的HTML页面中一个的HTMLDocument / WebBrowser控件,它可以运行,这将包括在HTML中的JavaScript函数(因为文件的结果。写操纵DOM和生成的HTML)。

如果您创建一个简单的表单应用程序,并将一个Web浏览器控件到它,这里是一个示例我只是写来测试这个理论了:

 使用系统;
使用System.Windows.Forms的;
//请务必添加COM引用到Microsoft HTML对象库

命名空间TheAnswer
{
    公共部分类Form1中:形态
    {
        公共Form1中()
        {
            的InitializeComponent();
        }

        私人无效Form1_Load的(对象发件人,EventArgs的)
        {
            webBrowser1.Url =新的URI(有关:空白);
        }


        私人无效webBrowser1_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
        {
            的MessageBox.show(装!);

            字符串testHtml = @
                < HTML>
                    < HEAD>
                        <脚本类型=文/ JavaScript的>
                            变种一个= 2;变种B = 3;
                            文件撰写(A + - + B);
                        < / SCRIPT>
                    < /头>
                    <身体GT;你好<!/身体GT;
                < / HTML>中;


            mshtml.IHTMLDocument2 HTMLDOC =(mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument; //的IHTMLDocument2有写能力(IHTMLDocument3不)
            htmlDoc.close();
            htmlDoc.open(有关:空白);

            反对HTML = testHtml;
            htmlDoc.write(HTML);
            HTML = NULL;

        }

    }
}
 

显然,从这里,你可以在已下载HTML插入到HTML文件,并执行它;你可能会在运行许多碰壁前进的道路上,如果你正在处理多种不同类型的网页等;如果你总是刮相似类型的网页,并有一定的预期某些行为或JavaScript函数,那么你或许可以取得一定的成效。这实在是很难说的更多考虑的信息,你就你的项目是什么所提供的最小量。

我希望这会有所帮助,是你试图完成的任务。让我知道!

编辑:哇,我还没有意识到这个问题,2岁!反正有一些有趣的回答吧!

How can I evaluate's document.write javascript to plaintext in C#? I'm trying to evaluate this:

<script type="text/javascript">
a=2;b=3;
document.write(a+"_"+y);
</script>

to this:

2_3

解决方案

From your comment, "it's a client side function on a downloaded HTML page", it sounds like you are doing some sort of screen scraping / crawling, where the HTML/JavaScript are not making a client request to your app?

If I understand correctly that this is what you are seeking, then you need an interpreter that can "speak" JavaScript. C# cannot do this, so the next best thing is to fire up a component within your C# app which is capable of understanding/interpreting (and therefore evaluating) JavaScript.

I would recommend to look into the WebBrowser control and HtmlDocument.DomDocument, load your downloaded HTML page in to an HtmlDocument / WebBrowser control, it will run and it will include the result of the JavaScript function in the HTML (since document.write manipulates the DOM and resulting HTML).

If you create a simple forms app and drag a web browser control onto it, here is a sample I just wrote to test this theory out:

using System;
using System.Windows.Forms;
// Make sure to add COM reference to "Microsoft HTML Object Library" 

namespace TheAnswer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri("about:blank");
        }


        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            MessageBox.Show("Loaded!");

            string testHtml = @"
                <html>
                    <head>
                        <script type=""text/javascript"">
                            var a=2;var b=3;
                            document.write(a+""_""+b);
                        </script>
                    </head>
                    <body>Hello there!</body>
                </html>";


            mshtml.IHTMLDocument2 htmlDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument; // IHTMLDocument2 has the write capability (IHTMLDocument3 does not)
            htmlDoc.close();
            htmlDoc.open("about:blank");

            object html = testHtml;
            htmlDoc.write(html);
            html = null;

        }

    }
}

Obviously from here, you can plug in your "downloaded" HTML into the HTML document and execute it; and you will likely run in to many snags along the way if you are dealing with a multitude of different types of pages, etc; if you are always scraping a similar type of page and are certain of some expected behaviors or javascript functions, then you may be able to achieve some results. It is really hard to say more considering the minimal amount of information you've provided regarding what your project is about.

I do hope this helps and is what you were trying to accomplish. Let me know!

EDIT: Wow I hadn't realized this question was 2 years old! anyway had some fun answering it!

这篇关于使用JavaScript的评估为纯文本C#.NET 3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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