从C#Winforms执行JavaScript代码

我正在尝试使用
Winforms& amp;我想从
JavaScript代码中获取文本.我需要使用Google Translator服务翻译几行.遇到了这个很好的javascript代码,它翻译了给出的消息&将其显示在警告框中.

<html>
<head>
<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('language','1');
function init () {
google.language.translate('How are you?', 'en', 'es', function (translated) {
    alert(translated.translation);
});
}
google.setOnLoadCallback(init);
</script>
</head>
    <body>
    </body>
</html> 

有什么办法让我可以传递任何字符串而不是’你好吗?’ &安培;如果我可以在C#winfrom上下文中获取翻译文本(从警报框或使用任何var).

最佳答案 好的,我做了一点研究.所以在你的表单中添加一个webbrowser,然后我打赌这对你有用:

    public Form1()
    {
        InitializeComponent();
        webBrowser1.ObjectForScripting = new MyScript();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string myTranslatedText = "Hello, how are you?";
        webBrowser1.DocumentText = @"
            <html>
            <head>
                <script type='text/javascript' src='http://www.google.com/jsapi'></script>
                <script type='text/javascript'>
                    google.load('language','1');
                    function init () {
                    google.language.translate('" + myTranslatedText + @"', 'en', 'es', function (translated) {
                        window.external.CallServerSideCode(translated.translation);
                    });
                    }
                    google.setOnLoadCallback(init);                        
                </script>
            </head>
                <body>
                </body>
            </html>";
    }
    [ComVisible(true)]
    public class MyScript
    {
        public void CallServerSideCode(string myResponse)
        {
            Console.WriteLine(myResponse); //do stuff with response
        }
    }
点赞