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);
      }
5
  • Try to follow this link. How to read /get data from url using javascript Commented Mar 17, 2017 at 5: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. Commented Mar 17, 2017 at 6: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: translate.googleapis.com/translate_a/… it downloads an f.txt file. So I wanted to access the end data. Thnks for the help Commented Mar 17, 2017 at 6: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. Commented Mar 17, 2017 at 6:22
  • You have tagged the question with chrome extension - are you trying to load or parse the text from within an extension? Commented Mar 17, 2017 at 8:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.