Javascript:如何从Google翻译页面的字段中获取翻译的字词? [英] Javascript: How to get a translated word out of the field of a Google Translate page?

查看:151
本文介绍了Javascript:如何从Google翻译页面的字段中获取翻译的字词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在,仍然可以通过JavaScript来进行询问。



首先,导航到 Google翻译页面并打开一个Web控制台;通过按 CTRL + SHIFT + K ,或者进入工具菜单并选择 Developer>然后输入以下命令:

  document.querySelector('#result_box')。textContent 

。至于第二步,支持在JavaScript中将数据复制到cilpboard中,这个问题是一个绝对的混乱: 如何复制到JavaScript的剪贴板?



我有点不熟悉JavaScript和HTML的新功能,所以他们可能有在最新版本中添加了正确的跨浏览器剪贴板支持,但是,这个问题的最佳答案实际上工作得很好。在这里,它被应用到我们现有的代码:

pre $ var $ transText = document.querySelector('#result_box')。textContent;
window.prompt(按CTRL + C将翻译的文本复制到剪贴板,然后按ENTER关闭对话框,translatedText);


In the Google Translate page I type in the word "milk" in English, and it gets instantly translated into Hindi:.

Now I need to somehow get that Hindi word out and place it into the clipboard.

I wonder how can I do it in Javascript?

I am using FireFox

解决方案

Although as already mentioned in the comments it is preferable to use the Google Translate API, it is still possible to do what you are asking via JavaScript.

First, navigate to the Google Translate page and open a web console; the console can be opened in FireFox by pressing CTRL+SHIFT+K, or by going to the tools menu and selecting Developer > Web Console).

Then type the following command:

document.querySelector('#result_box').textContent

That is how you retrieve the value. As for the second step, support for copying data to the cilpboard in JavaScript, is, well, an absolute mess: How to copy to the clipboard in JavaScript?

I'm a bit out of the loop with new features in JavaScript and HTML, so they may have added proper cross-browser clipboard support in the latest versions, however, the top answer in that question actually works quite well. Here it is applied to our existing code:

var translatedText = document.querySelector('#result_box').textContent;
window.prompt("Press CTRL+C to copy the translated text to the clipboard, then ENTER to close the dialog", translatedText);

这篇关于Javascript:如何从Google翻译页面的字段中获取翻译的字词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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