|
28 | 28 | width: 3rem;
|
29 | 29 | height: 3rem;
|
30 | 30 | }
|
| 31 | + |
| 32 | + .favicon { |
| 33 | + width: 24px; |
| 34 | + height: 24px; |
| 35 | + margin-right: 10px; |
| 36 | + } |
31 | 37 | </style>
|
32 | 38 | </head>
|
33 | 39 | <body>
|
|
37 | 43 | <div class="spinner-border text-primary" role="status" id="loader">
|
38 | 44 | <span class="sr-only">Loading...</span>
|
39 | 45 | </div>
|
40 |
| - <br> |
41 | 46 | <div class="message" id="message">Redirecting...</div>
|
42 | 47 | </div>
|
43 | 48 |
|
|
50 | 55 | return urlParams.get(param);
|
51 | 56 | }
|
52 | 57 |
|
| 58 | + // Function to get the base URL from a full URL |
| 59 | + function getBaseUrl(fullUrl) { |
| 60 | + const url = new URL(fullUrl); |
| 61 | + return url.hostname; |
| 62 | + } |
| 63 | + |
| 64 | + // Function to get favicon from the site |
| 65 | + function getFaviconUrl(baseUrl) { |
| 66 | + return `https://www.google.com/s2/favicons?domain=${baseUrl}`; |
| 67 | + } |
| 68 | + |
| 69 | + // Function to get the site title |
| 70 | + async function getSiteTitle(url) { |
| 71 | + try { |
| 72 | + const response = await fetch(url); |
| 73 | + const text = await response.text(); |
| 74 | + const titleMatch = text.match(/<title>(.*?)<\/title>/); |
| 75 | + return titleMatch ? titleMatch[1] : 'No Title Found'; |
| 76 | + } catch (error) { |
| 77 | + console.error('Error fetching site title:', error); |
| 78 | + return 'No Title Found'; |
| 79 | + } |
| 80 | + } |
| 81 | + |
53 | 82 | // Get the 'url' parameter from the querystring
|
54 | 83 | const targetUrl = getQueryParam('url');
|
55 | 84 |
|
|
58 | 87 | try {
|
59 | 88 | // Validate the URL
|
60 | 89 | const url = new URL(targetUrl);
|
61 |
| - document.getElementById('message').textContent = `Redirecting to ${url.toString()}...`; |
| 90 | + const baseUrl = getBaseUrl(targetUrl); |
| 91 | + const faviconUrl = getFaviconUrl(baseUrl); |
| 92 | + |
| 93 | + // Fetch the site title |
| 94 | + getSiteTitle(targetUrl).then(title => { |
| 95 | + document.getElementById('message').innerHTML = ` |
| 96 | + <img src="${faviconUrl}" alt="favicon" class="favicon"> |
| 97 | + Redirecting to <strong>${title}</strong> (${baseUrl}) |
| 98 | + `; |
| 99 | + |
| 100 | + // Show loader and redirect after a short delay |
| 101 | + setTimeout(() => { |
| 102 | + window.location.href = targetUrl; |
| 103 | + }, 1500); // Optional delay of 1.5 seconds to show the message and loader |
| 104 | + }); |
62 | 105 |
|
63 |
| - // Show loader and redirect after a short delay |
64 |
| - setTimeout(() => { |
65 |
| - window.location.href = url.toString(); |
66 |
| - }, 1500); // Optional delay of 1.5 seconds to show the message and loader |
67 | 106 | } catch (error) {
|
68 | 107 | // Invalid URL
|
69 | 108 | document.getElementById('message').textContent = "Invalid URL provided in the querystring.";
|
|
0 commit comments