0

I have been trying to get my webpage to play up what it says in a text box when the user click on a link, but so far I haven't manage. I have tried with

        function listen(){
            var sound = new Audio();
            sound.src = "http://translate.google.com/translate_tts?ie=UTF-8&tl=sv&q=Testar";
            sound.play();
            alert(":D");
            return false;
        }

and

function listen(){
    var sound = document.createElement("audio");
    sound.setAttribute("src","http://translate.google.com/translate_tts?tl=sv&q=Testar");
    sound.load();
    sound.play();
    alert(":D");
    return false;
}

I have tried adding ie=UTF-8 to the link, and tried both with and without sound.play(); but nothing have worked. I get smiley face from the alert so I know the function runs. Can someone please help me get this to work.

EDIT: I did a work around by using and iframe which I hide by using display: none; and then simply using javascript to change the src, not the best solution but it works... for now.

Yemto
  • 613
  • 1
  • 7
  • 18

2 Answers2

0

This is an easy way to do it :

var sound = new Audio("http://translate.google.com/translate_tts?tl=sv&q=Testar");
sound.play();
Khalid
  • 4,730
  • 5
  • 27
  • 50
-1

Now it's a bit more complicated, but still possible:
you need set up an user-agent string like a common browser and, more difficult, you also must provide a token into the GET request. Some people managed to extract the token algorithm from the js code of the page, but it's quite a long work and at any time the algorithm changes you need to start again the reverse engineering of the cryptic code.
So it's much easier access to a particular url from the same site that generates an XHR that shows the token you need.
A simple script with phantomjs and grep will do the job for you, details here:

https://stackoverflow.com/a/37221340/6332793

Community
  • 1
  • 1