exports.endpoint = async (req, res) => {
const [url, search] = req.url.split('?');
if(url === '/') {
res.writeHead(200, { 'Content-Type': 'text/html; charset=UTF-8' });
res.end(
'<!doctype html><html><head></head><body><script type="text/javascript">url=prompt("enter url");if(url)location.href = `/get?url=${encodeURIComponent(url)}`;</script></body><html>'
);
} else if(url === '/get') {
const knowUrl = new URLSearchParams(search || '').get('url');
if (!knowUrl || !/^https?:\/\/knowunity\.de\/knows\/.+$/.test(knowUrl)) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('you need to enter an url');
return;
}
const knowRes = await fetch(knowUrl);
const html = await knowRes.text();
const preview_url = html.match(/"og:image" content="(.+?)"/).at(1);
const [server, id] = preview_url.match(/https?:\/\/(.+?)\.knowunity\.com\/CONTENT\/(.+?)_PREVIEW(?:_SMALL)?\.webp/).slice(1);
const pdf_url = `https://${server}.knowunity.com/CONTENT/${id}.pdf`;
res.writeHead(302, { 'Location': pdf_url });
res.end();
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found.');
}
};