0

I am a noob in JS.

I'm somewhat of a streamer and I wanted to create a html page for one of my scenes. I also have an application that is basically writes now-playing track to an txt file. How can I read and write down information from this file into a span tag without using a server? When I open my page outside of Bracket's Live Preview, it doesn't show anything in the span tag and I get a bunch of one-type errors in console

function loadTxt() {
  fetch('now playing.txt')
    .then(function(response) {
      return response.text();
    })
    .then(function(data) {
      console.log(data);
      document.getElementById('np').innerHTML = data;
    })
  var t = setTimeout(loadTxt, 500);
}
<body onload="loadTxt()">
  <span id="np"></span>
</body>

The errors I get from console without "Live Preview"(basically, a server):

Uncaught (in promise) TypeError: Failed to fetch
    at loadTxt (starting.html:62)
    at onload (starting.html:75)
starting.html:62 Fetch API cannot load file:///C:/Users/dimam/Documents/OBS/web/now%20playing.txt. URL scheme must be "http" or "https" for CORS request.
loadTxt @ starting.html:62

a screenshot from console with the errors above

Shmookoff
  • 146
  • 1
  • 10
  • You can't. Security restrictions prevent it. – Quentin Jul 02 '20 at 21:09
  • you could use local storage for front end. https://stackoverflow.com/questions/42743511/reading-writing-to-a-file-in-javascript. though, servers are for serving files. simple/fun in nodejs – Nathan Boaldin Jul 02 '20 at 22:00

0 Answers0