0

My browser will display HTML files stored on my computer, and allow them to link to local stylesheets/scripts. It will also display text files. However, it seems like I can't use the Fetch API to access these. Is there any workaround for this?

This is what I have:

fetch('file.md')
    .then(response => response.text()
    .then(text => {
        let target = document.getElementsByClassName('content')[0];

        let converter = new showdown.Converter();

        converter.setOption('customizedHeaderId', 'true');
        converter.setOption('tables', 'true');

        let html = converter.makeHtml(text);
        target.innerHTML = html;
    })

It works fine accessing a normal Internet file.

Stonks
  • 3
  • 1
  • 1
  • Files opened using the file protocol have the origin null so you cannot use fetch when doing that – kelsny Apr 06 '22 at 22:25
  • 2
    You might be able to get away with something like `fetch('//localhost/file.md')` but you would have to be running a web server on localhost to do that. Otherwise, you might want to read the question [How to read a local text file](https://stackoverflow.com/q/14446447/17300) – Stephen P Apr 06 '22 at 22:28
  • It doesn't seem like this will work, since I can't run a localhost server (administrated Chromebook). I think I will just store the markdown in a textarea and have a button to edit it. – Stonks Apr 06 '22 at 23:30
  • Is there a way to access content loaded using a link or embed tag, since those work in my case? – Stonks Apr 07 '22 at 00:13

0 Answers0