// static/header/headerLoader.js (function () { "use strict"; // Configuration const CONFIG = { headerPath: "/static/header/header.html", insertPosition: "afterbegin", containerSelector: "body", activeClass: "active", version: "1.0", }; // Styles const styles = ` `; // Load header function async function loadHeader() { try { // Fetch header HTML const response = await fetch(`${CONFIG.headerPath}?v=${CONFIG.version}`); if (!response.ok) { throw new Error(`Failed to load header: ${response.status}`); } const headerHTML = await response.text(); // Inject styles if not already present if (!document.getElementById("header-component-styles")) { document.head.insertAdjacentHTML("beforeend", styles); } // Insert header const container = document.querySelector(CONFIG.containerSelector); container.insertAdjacentHTML(CONFIG.insertPosition, headerHTML); // Highlight active link highlightActiveLink(); console.log("✅ Header component loaded v" + CONFIG.version); } catch (error) { console.error("❌ Error loading header:", error); createFallbackHeader(); } } // Highlight current page link function highlightActiveLink() { const currentPath = window.location.pathname; const links = document.querySelectorAll(".nav-link"); links.forEach((link) => { const href = link.getAttribute("href"); // Check if current path matches if ( currentPath === href || (href !== "/" && currentPath.startsWith(href)) ) { link.classList.add(CONFIG.activeClass); } // Special case for root if (currentPath === "/" && href === "/transcription") { link.classList.add(CONFIG.activeClass); } }); } // Fallback header in case of error function createFallbackHeader() { const fallback = document.createElement("div"); fallback.style.cssText = ` padding: 15px 20px; background: #fff; border-bottom: 1px solid #e0e0e0; position: fixed; top: 0; left: 0; right: 0; z-index: 1000; `; fallback.innerHTML = `
📝 Phiên Âm 📚 Z-Library Scraper
`; document.body.insertAdjacentElement("afterbegin", fallback); document.body.style.paddingTop = "50px"; } // Initialize when DOM is ready if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", loadHeader); } else { loadHeader(); } })();