Share by:
// Execute immediately, don't wait for DOMContentLoaded (function() { // Hide H1 initially document.documentElement.style.visibility = 'hidden'; function preserveH1() { const h1 = document.querySelector('h1'); if (!h1) return; // Exit if H1 not found const originalH1 = h1.innerHTML; // Show the page now that we've captured the original H1 document.documentElement.style.visibility = 'visible'; const observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.target.tagName === 'H1' && mutation.target.innerHTML !== originalH1) { // Store SEO content in hidden span const seoSpan = document.createElement('span'); seoSpan.style.cssText = 'position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0;'; seoSpan.innerHTML = mutation.target.innerHTML; // Restore original H1 mutation.target.innerHTML = originalH1; // Add hidden SEO content mutation.target.appendChild(seoSpan); } }); }); observer.observe(h1, { characterData: true, childList: true, subtree: true }); } // Try to run immediately preserveH1(); // Also run on DOMContentLoaded as backup document.addEventListener('DOMContentLoaded', preserveH1); })();