1

I have followed the instruction on this page but I'm unable to solve the problem.

Here's my code: When file link is clicked it downloads the f.txt file. But I want to access the data without downloading through the url.

var file = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=hi&dt=t&dt=t&q=hello';
      function readTextFile(file) {
          var rawFile = new XMLHttpRequest();
          rawFile.open("GET",file,false);
          rawFile.onreadystatechange = function() {
              if(rawFile.readyState === 4) {
                  if(rawFile.status === 200 || rawFile.status === 0)
                  {
                      var allText = rawFile.responseText;
                      alert(allText);
                  }
              }
          }
          rawFile.send(null);
      }
Community
  • 1
  • 1
Pavan Boro
  • 99
  • 2
  • 8
  • Try to follow this link. [How to read /get data from url using javascript](http://stackoverflow.com/questions/814613/how-to-read-get-data-from-a-url-using-javascript) – pd1 Mar 17 '17 at 05:59
  • You have to change the anche to launch your javascript fumction or bind the click event. Removed the URL to the service, that you currently have. – Mario Santini Mar 17 '17 at 06:02
  • @pd1 that url provides me the instruction on how to extract text from url but I wanted to get data from the url when it is loaded. On clicking this url: https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=hi&dt=t&dt=t&q=hello it downloads an f.txt file. So I wanted to access the end data. Thnks for the help – Pavan Boro Mar 17 '17 at 06:18
  • This is a dynamically constructed page. You'll need to open it in a tab or iframe to allow its scripts do the work. The only other alternative is to use translation API directly. – wOxxOm Mar 17 '17 at 06:22
  • You have tagged the question with chrome extension - are you trying to load or parse the text from within an extension? –  Mar 17 '17 at 08:03

1 Answers1

1
  <body>
      <a id="myLink" href="https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=hi&dt=t&dt=t&q=hello"  ">file link</a>
  </body>

 <script>
     document.querySelector("#myLink").addEventListener("click", function(event){ 
        event.preventDefault(); 
        var file = document.getElementById("myLink").getAttribute("href");
        console.log(file)
        var rawFile = new XMLHttpRequest();
        rawFile.open("GET",file,false);
          rawFile.onreadystatechange = function() {
              if(rawFile.readyState === 4) {
                  if(rawFile.status === 200 || rawFile.status === 0)
                  {
                      var allText = rawFile.responseText;
                      console.log(allText);
                  }
              }
          }
          rawFile.send(null);
    }, false); 

  </script>

This approach will get you the read the content of the file.. This way will be helps to you.

Senthil Kumar
  • 562
  • 1
  • 6
  • 14