From 92ae96a09666bc550c996125d1cf1928b5f43eb8 Mon Sep 17 00:00:00 2001
From: Deepak Khatri <lorforlinux@beagleboard.org>
Date: Mon, 25 Nov 2024 02:08:44 +0530
Subject: [PATCH] Fix 404 sudden text update

---
 _templates/404.html | 171 ++++++++++++++++++++++----------------------
 1 file changed, 86 insertions(+), 85 deletions(-)

diff --git a/_templates/404.html b/_templates/404.html
index ebac32c6..303ee3a3 100644
--- a/_templates/404.html
+++ b/_templates/404.html
@@ -3,109 +3,110 @@
 {% block extrahead %}
 
 <script>
-    // The redirect_folders dictionary is passed from html_context
-    const redirectFolders = JSON.parse('{{ redirect_folders | tojson | safe }}');
-
-    // Get the current URL
-    let currentUrl = window.location.href;
+    document.addEventListener("DOMContentLoaded", function () {
+        // The redirect_folders dictionary is passed from html_context
+        const redirectFolders = JSON.parse('{{ redirect_folders | tojson | safe }}');
 
-    // Flag to check if valid redirect is found
-    let redirectFound = false;
+        // Get the current URL
+        let currentUrl = window.location.href;
 
-    // Iterate over the redirect_folders key-value pairs
-    for (const [key, value] of Object.entries(redirectFolders)) {
-        if (currentUrl.includes("/" + key)) {
+        // Flag to check if valid redirect is found
+        let redirectFound = false;
 
-            // Determine the replacement value
-            let replacement = value === "" ? "" : "/" + value;
+        // Iterate over the redirect_folders key-value pairs
+        for (const [key, value] of Object.entries(redirectFolders)) {
+            if (currentUrl.includes("/" + key)) {
 
-            // Create a new URL by replacing "/key" with the replacement value
-            let newUrl = currentUrl.replace("/" + key, replacement);
+                // Determine the replacement value
+                let replacement = value === "" ? "" : "/" + value;
 
-            // Make an HTTP request to check if the new URL exists
-            fetch(newUrl, { method: "HEAD" })
-                .then(response => {
-                    if (response.ok) {
-                        // Valid redirect is found
-                        redirectFound = true;
+                // Create a new URL by replacing "/key" with the replacement value
+                let newUrl = currentUrl.replace("/" + key, replacement);
 
-                        // Update the heading with the correct information
-                        const headingElement = document.getElementById('pageHeading');
-                        if (headingElement) {
-                            headingElement.textContent = "Page Moved!";
-                        }
-
-                        // Update the message with the correct link
-                        const redirectMessageElement = document.getElementById('redirectMessage');
-                        if (redirectMessageElement) {
-                            redirectMessageElement.innerHTML = `We found the correct page you are looking for <a href="${newUrl}" id="correctPageLink">here</a>.`;
-                        }
+                // Make an HTTP request to check if the new URL exists
+                fetch(newUrl, { method: "HEAD" })
+                    .then(response => {
+                        if (response.ok) {
+                            // Valid redirect is found
+                            redirectFound = true;
 
-                        document.getElementById('redirectInfo').style.display = "block";
+                            // Update the heading with the correct information
+                            const headingElement = document.getElementById('pageHeading');
+                            if (headingElement) {
+                                headingElement.textContent = "Page Moved!";
+                            }
 
-                        // If the response is OK, show the modal and initiate the countdown
-                        const redirectModal = new bootstrap.Modal(document.getElementById('redirectModal'), {
-                            backdrop: 'static',  // Prevents closing the modal by clicking outside
-                            keyboard: false      // Disables closing the modal with the keyboard
-                        });
-                        redirectModal.show();
+                            // Update the message with the correct link
+                            const redirectMessageElement = document.getElementById('redirectMessage');
+                            if (redirectMessageElement) {
+                                redirectMessageElement.innerHTML = `We found the correct page you are looking for <a href="${newUrl}" id="correctPageLink">here</a>.`;
+                            }
 
-                        let countdownValue = 5;
-                        const countdownElement = document.getElementById('countdownNumber');
-                        if (countdownElement) {
-                            countdownElement.textContent = countdownValue;
-                        }
+                            document.getElementById('redirectInfo').style.display = "block";
 
-                        // Variable to track if countdown should continue
-                        let countdownActive = true;
+                            // If the response is OK, show the modal and initiate the countdown
+                            const redirectModal = new bootstrap.Modal(document.getElementById('redirectModal'), {
+                                backdrop: 'static',  // Prevents closing the modal by clicking outside
+                                keyboard: false      // Disables closing the modal with the keyboard
+                            });
+                            redirectModal.show();
 
-                        // Countdown from 5 to 1
-                        const countdownInterval = setInterval(() => {
-                            if (!countdownActive) {
-                                clearInterval(countdownInterval);
-                                return;
-                            }
-                            countdownValue--;
+                            let countdownValue = 5;
+                            const countdownElement = document.getElementById('countdownNumber');
                             if (countdownElement) {
                                 countdownElement.textContent = countdownValue;
                             }
 
-                            // When countdown reaches 1, redirect
-                            if (countdownValue <= 1) {
-                                clearInterval(countdownInterval);
+                            // Variable to track if countdown should continue
+                            let countdownActive = true;
+
+                            // Countdown from 5 to 1
+                            const countdownInterval = setInterval(() => {
+                                if (!countdownActive) {
+                                    clearInterval(countdownInterval);
+                                    return;
+                                }
+                                countdownValue--;
+                                if (countdownElement) {
+                                    countdownElement.textContent = countdownValue;
+                                }
+
+                                // When countdown reaches 1, redirect
+                                if (countdownValue <= 1) {
+                                    clearInterval(countdownInterval);
+                                    window.location.href = newUrl;
+                                }
+                            }, 1000);
+
+                            // Event listener for "Redirect Now" button
+                            document.getElementById('redirectNow').addEventListener('click', () => {
                                 window.location.href = newUrl;
-                            }
-                        }, 1000);
-
-                        // Event listener for "Redirect Now" button
-                        document.getElementById('redirectNow').addEventListener('click', () => {
-                            window.location.href = newUrl;
-                        });
-
-                        // Event listener for "Cancel" button
-                        document.getElementById('cancelRedirect').addEventListener('click', () => {
-                            countdownActive = false;
-                            clearInterval(countdownInterval);
-                        });
-
-                        // Event listener for "Close" button in the modal header
-                        document.getElementById('closeRedirect').addEventListener('click', () => {
-                            countdownActive = false;
-                            clearInterval(countdownInterval);
-                        });
-                    } else {
-                        // If the response is not OK, proceed to check other redirects
-                        console.log(`No valid page found at ${newUrl}`);
-                    }
-                })
-                .catch(error => {
-                    console.error("Error checking the URL:", error);
-                });
-            break;
+                            });
+
+                            // Event listener for "Cancel" button
+                            document.getElementById('cancelRedirect').addEventListener('click', () => {
+                                countdownActive = false;
+                                clearInterval(countdownInterval);
+                            });
+
+                            // Event listener for "Close" button in the modal header
+                            document.getElementById('closeRedirect').addEventListener('click', () => {
+                                countdownActive = false;
+                                clearInterval(countdownInterval);
+                            });
+                        } else {
+                            // If the response is not OK, proceed to check other redirects
+                            console.log(`No valid page found at ${newUrl}`);
+                        }
+                    })
+                    .catch(error => {
+                        console.error("Error checking the URL:", error);
+                    });
+                break;
+            }
         }
-    }
 
-    document.addEventListener("DOMContentLoaded", function () {
+
         if (!redirectFound) {
             // In case the link test doesn't include redirectFolders
             const headingElement = document.getElementById('pageHeading');
-- 
GitLab