07 plugin.js (4/4)

  $('[data-act=save]').onclick = async () => {
    const server = $('[data-k=server]').value.trim().replace(/\/+$/, '');
    const session = $('[data-k=session]').value;
    if (!/^https?:\/\/[^/]+$/.test(server)) {
      return out('Enter the server as http://host:port, without a path (row links are relative to the server root).', false);
    }
    out('Testing ' + esc(server) + ' ...');
    try {
      // api.relay goes through the reader, so this also proves the reader can reach the server.
      const r = await api.relay('GET', server + '/games', { Accept: 'application/json' });
      if (r.status !== 200) return out('The server answered HTTP ' + r.status + '.', false);
      const lists = (JSON.parse(r.body).lists || []);
      await writeConfig({ server, session });
      cfg.server = server;
      cfg.session = session;
      out(lists.length
        ? 'Saved. Games on the server: ' + lists.map((l) => esc(l.title)).join(', ') + '.'
        : 'Saved, but the server has no games yet. Put .z5/.z8 files into its games/ folder.', lists.length > 0);
    } catch (e) {
      out('The reader could not reach the server: ' + esc(e.message), false);
    }
  };
});
‹ PreviousReader file (.txt)Next ›