Last active
August 7, 2021 21:25
-
-
Save davidcralph/3f11238b1b0d11be3fd0b85d3105349e to your computer and use it in GitHub Desktop.
MAL Redirect Worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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