-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (23 loc) · 1.11 KB
/
index.js
File metadata and controls
31 lines (23 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const apiUrl = 'https://api.redirect-checker.net/?url=https%3A%2F%2Feksisozluk.com&timeout=5&maxhops=10&meta-refresh=1&format=json';
// Fetch the JSON API
const response = await fetch(apiUrl)
if (response.ok) {
const json = await response.json()
if (json.data && json.data[1] && json.data[1].response && json.data[1].response.info && json.data[1].response.info.url) {
const redirectUrl = json.data[1].response.info.url
// Extract path and query parameters from the original request URL
const originalUrl = new URL(request.url)
const pathAndQuery = originalUrl.pathname + originalUrl.search
// Append path and query parameters to the redirect URL
const redirectedUrl = redirectUrl + pathAndQuery
// Redirect the visitor to the obtained URL with path and query parameters
return Response.redirect(redirectedUrl, 302)
}
}
// Return a response indicating an error or fallback action
return new Response('Hata.', { status: 500 })
}