06 Reading files

There is no api.readFile. Use one of these:

  // A file directly inside your plugin folder (no subfolders):
  const r = await fetch(api.pluginFile('config.json'));
  const cfg = r.ok ? await r.json() : {};          // 404 when it doesn't exist yet
  
  // Any file on the SD card (the last path component must not start with "."):
  const r2 = await fetch('/download?path=' + encodeURIComponent(api.dir + '/data/state.json'));

api.pluginFile(f) is just '/plugin?name=<you>&file=<f>'. f must be a single name (no /). It's also how you load extra scripts or images:

  await new Promise((ok, fail) => {
    const s = document.createElement('script');
    s.src = api.pluginFile('epub.js');
    s.onload = ok; s.onerror = fail;
    document.head.appendChild(s);
  });
  container.insertAdjacentHTML('beforeend', `<img src="${api.pluginFile('logo.svg')}" width="48">`);

Content types: .js .css .html .json .svg get proper types. Everything else is served as application/octet-stream.
‹ PreviousReader file (.txt)Next ›