Skip to content

Commit 9407bf4

Browse files
Create worker.js
1 parent 378b6a0 commit 9407bf4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

worker.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
addEventListener('fetch', event => {
2+
event.respondWith(handleRequest(event.request))
3+
})
4+
5+
async function handleRequest(request) {
6+
// Get the unique ID from the request URL
7+
const url = new URL(request.url);
8+
const id = url.searchParams.get("id");
9+
// Get the list of URLs from the raw text file
10+
const urlList = await fetch('https://gist.githubusercontent.com/developeranaz/e3ed847f88459106498b615375c2b902/raw')
11+
.then(response => response.text())
12+
.then(text => text.split('\n'))
13+
.catch(error => {
14+
return new Response(error, { status: 500 })
15+
})
16+
// Find the URL associated with the unique ID
17+
const redirectURL = urlList.find(u => u.split(" ")[0] === id);
18+
if (!redirectURL) {
19+
return new Response('Invalid ID', { status: 404 });
20+
}
21+
// Redirect to the associated URL
22+
return new Response('', {
23+
status: 301,
24+
headers: { 'Location': redirectURL.split(" ")[1] }
25+
});
26+
}

0 commit comments

Comments
 (0)