$('[data-act=save]').onclick = async () => {
const next = {};
for (const el of container.querySelectorAll('[data-k]')) next[el.dataset.k] = el.value.trim();
// Folder URLs need a trailing slash, or many servers answer with a redirect
// (a PROPFIND request should not rely on redirects).
if (next.url && !next.url.endsWith('/')) next.url += '/';
next.auth = b64(`${next.user}:${next.pass}`);
out('Testing...');
try {
const r = await api.relay('PROPFIND', next.url,
{ Authorization: 'Basic ' + next.auth, Depth: '1', 'Content-Type': 'application/xml' }, PROPFIND_BODY);
if (r.status === 401) throw new Error('wrong username or password');
if (r.status !== 207) throw new Error('expected HTTP 207, got ' + r.status);
const entries = (r.body.match(/<(\w+:)?response[\s>]/g) || []).length - 1; // minus the folder itself
await write('config.json', next);
out(`OK: ${entries} entries in this folder. Saved.`);
} catch (e) {
// relay throws (502) when the listing is over 64 KB. The reader itself streams
// listings up to 1 MB, so save anyway in that case.
if (e.status === 502) {
await write('config.json', next);
return out('Listing too big to preview here, but saved. Try it on the reader.');
}
out('Test failed: ' + e.message, false);
}
};
});