0

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

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

brilliant
  • 2,805
  • 11
  • 39
  • 57
  • Have you actually tried anything? – Etheryte Jul 10 '14 at 05:51
  • 1
    I suggest to use google translate api in your application https://developers.google.com/translate/v2/using_rest – chf Jul 10 '14 at 06:32
  • @Nit - "Have you actually tried anything?" - No, I haven't. I don't even know what to start here with. – brilliant Jul 10 '14 at 07:35
  • @chf - "I suggest to use google translate api in your application" - Thank you for this link, but it states there, that the google translate api is a paid service. I think I better try a free way - I am not translating a sentence, but only one separate word, and I can still do it manually absolutely free. – brilliant Jul 10 '14 at 07:42

1 Answers1

1

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);
Community
  • 1
  • 1
IQAndreas
  • 8,060
  • 8
  • 39
  • 74
  • Thank you very much for your answer. I have a small follow-up question: How did you find out that the parameter should be exactly "#result_box"? Is it like you have inspected the HTML page and somehow got that info from the page? Never could've even imagined that saving values into clipboard was so hard in Javascript. I wonder if at least outputting into a file on the computer is possible. Well, this is, of course, a separate question - I have just asked it here: http://stackoverflow.com/questions/24690384/no-saving-or-outputting-is-possible-in-javascript Thanks again for this answer. – brilliant Jul 11 '14 at 05:01
  • @brilliant I right-clicked the text box, chose _Inspect Element_, and then navigated around until I found what I was looking for: `` – IQAndreas Jul 11 '14 at 15:19
  • @brilliant Of course, one could also search by just digging through the HTML source, but FireFox and Chrome have some really nice graphical debugging tools which make these things easier. – IQAndreas Jul 11 '14 at 15:20