admin管理员组文章数量:1323707
I am trying to execute JavaScript using Winforms & I would like to fetch text from JavaScript code. I need to translate few lines using Google Translator service. I came across this nice Javascript code which translates given message & display it in the alert box:
<html>
<head>
<script type='text/javascript' src=''></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>
Is there any way so that I can pass any string instead of 'How are you?' & if I can fetch the translated text (from alert box or using any var
) in the C# WinForms context.
I am trying to execute JavaScript using Winforms & I would like to fetch text from JavaScript code. I need to translate few lines using Google Translator service. I came across this nice Javascript code which translates given message & display it in the alert box:
<html>
<head>
<script type='text/javascript' src='http://www.google./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>
Is there any way so that I can pass any string instead of 'How are you?' & if I can fetch the translated text (from alert box or using any var
) in the C# WinForms context.
- 1 Please read the following URL, Google will remove the free translation API in December: URL – Niels Commented Nov 23, 2011 at 10:41
- so you mean this code wont work anymore ? – Sangram Nandkhile Commented Nov 23, 2011 at 10:44
- Yes exactly, the full translation API V1.0 will be offline and for the V2.0 API you need to pay. 20 dollar for 20M characters. See this url – Niels Commented Nov 23, 2011 at 10:48
- 1 Bing offers a free api. msdn.microsoft./en-us/library/ff512387.aspx – Ash Burlaczenko Commented Nov 23, 2011 at 10:52
- I updated my answer. You'll just have to replace 'myResults from callback' with your return variable with your response, and update the html string to include your request – Jeff Lauder Commented Nov 24, 2011 at 17:43
1 Answer
Reset to default 5Ok I did a little research. So add a webbrowser to your form, then I bet this will work well for you:
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./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
}
}
本文标签: Executing JavaScript code from C WinformsStack Overflow
版权声明:本文标题:Executing JavaScript code from C# Winforms - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742130454a2422134.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论