Skip to content

Commit

Permalink
Update index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasvanleeuwen19 authored Sep 27, 2024
1 parent daa0968 commit 9265e3f
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions redirect/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
width: 3rem;
height: 3rem;
}

.favicon {
width: 24px;
height: 24px;
margin-right: 10px;
}
</style>
</head>
<body>
Expand All @@ -37,7 +43,6 @@
<div class="spinner-border text-primary" role="status" id="loader">
<span class="sr-only">Loading...</span>
</div>
<br>
<div class="message" id="message">Redirecting...</div>
</div>

Expand All @@ -50,6 +55,30 @@
return urlParams.get(param);
}

// Function to get the base URL from a full URL
function getBaseUrl(fullUrl) {
const url = new URL(fullUrl);
return url.hostname;
}

// Function to get favicon from the site
function getFaviconUrl(baseUrl) {
return `https://www.google.com/s2/favicons?domain=${baseUrl}`;
}

// Function to get the site title
async function getSiteTitle(url) {
try {
const response = await fetch(url);
const text = await response.text();
const titleMatch = text.match(/<title>(.*?)<\/title>/);
return titleMatch ? titleMatch[1] : 'No Title Found';
} catch (error) {
console.error('Error fetching site title:', error);
return 'No Title Found';
}
}

// Get the 'url' parameter from the querystring
const targetUrl = getQueryParam('url');

Expand All @@ -58,12 +87,22 @@
try {
// Validate the URL
const url = new URL(targetUrl);
document.getElementById('message').textContent = `Redirecting to ${url.toString()}...`;
const baseUrl = getBaseUrl(targetUrl);
const faviconUrl = getFaviconUrl(baseUrl);

// Fetch the site title
getSiteTitle(targetUrl).then(title => {
document.getElementById('message').innerHTML = `
<img src="${faviconUrl}" alt="favicon" class="favicon">
Redirecting to <strong>${title}</strong> (${baseUrl})
`;

// Show loader and redirect after a short delay
setTimeout(() => {
window.location.href = targetUrl;
}, 1500); // Optional delay of 1.5 seconds to show the message and loader
});

// Show loader and redirect after a short delay
setTimeout(() => {
window.location.href = url.toString();
}, 1500); // Optional delay of 1.5 seconds to show the message and loader
} catch (error) {
// Invalid URL
document.getElementById('message').textContent = "Invalid URL provided in the querystring.";
Expand Down

0 comments on commit 9265e3f

Please sign in to comment.