File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments