Skip to content

Instantly share code, notes, and snippets.

@davidcralph
Last active August 7, 2021 21:25
Show Gist options
  • Save davidcralph/3f11238b1b0d11be3fd0b85d3105349e to your computer and use it in GitHub Desktop.
Save davidcralph/3f11238b1b0d11be3fd0b85d3105349e to your computer and use it in GitHub Desktop.
MAL Redirect Worker
addEventListener('fetch', (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});
async function handleRequest(request) {
const { pathname } = new URL(request.url);
if (pathname.startsWith('/mal')) {
const name = pathname.split('/mal/')[1];
if (!name) {
return Response.redirect('https://myanimelist.net', 302);
}
const data = await (await fetch(`https://myanimelist.net/search/prefix.json?type=manga&keyword=${name}`)).json();
return Response.redirect(data.categories[0].items[0].url, 302);
} else {
return Response.redirect('https://davidjcralph.co.uk', 302);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment