Search code examples
javascripturlfile-read

How to read online file line by line?


I'm sorry for this very basic question, but I can't find it out. Basically I'm trying to read this file line-by-line, then in another HTML page whenever there is a string that match one of these lines, then it will be wrapped within the <span class="hello">...</span> tag. For the latter I can use the findAndReplaceDOMText library, and I thought that the former should be easy too. But I can only find answers to read an uploaded file. That is, using

<input type="file" name="file" id="file">

to upload it. For example: How to use Javascript to read local text file and read line by line?, Reading line-by-line file in JavaScript on client side. Googling js read line by line only returns results for Node.js


Solution

  • You can use the Fetch API and read the response as text and iterate line by line

    (async () => {
      const response = await fetch("https://xn--qucu-hr5aza.cc/files/M%E1%BB%99t%20h%E1%BB%87%20th%E1%BB%91ng%20ni%E1%BB%81m%20tin/Node%20list");
      const data = await response.text();
      const lines = data.split("\n");
      console.log(lines)
    })();