Reputation: 2853
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
Upvotes: 0
Views: 595
Reputation: 8468
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);
Upvotes: 1