/** * 2. Auto-Classify Text Content * Scans text nodes and wraps them safely. */ const processedNodes = new WeakSet(); function classifyText(node) { // Only process common content containers to avoid breaking layout structures const validContainers = '.entry-content, .post-content, .page-content, .article-section'; if (node.nodeType === 1 && !node.closest(validContainers) && !node.matches(validContainers)) return; const walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null, false); let textNode; const nodesToReplace = []; while (textNode = walker.nextNode()) { const parent = textNode.parentElement; if (!parent || processedNodes.has(textNode)) continue; // Skip structural and script elements if (/^(SCRIPT|STYLE|TEXTAREA|NOSCRIPT|IFRAME|BUTTON|INPUT)$/.test(parent.tagName)) continue; if (parent.classList.contains('en') || parent.classList.contains('gr')) continue; const text = textNode.nodeValue.trim(); if (text.length < 1) continue; const hasGreek = /[\u0370-\u03FF\u1F00-\u1FFF]/.test(text); const hasEnglish = /[a-zA-Z]/.test(text); if (hasGreek || hasEnglish) { nodesToReplace.push({ node: textNode, lang: hasGreek ? 'gr' : 'en' }); } } // Use requestAnimationFrame to avoid "Layout Thrashing" and recursive observer triggers requestAnimationFrame(() => { nodesToReplace.forEach(item => { if (item.node.parentNode) { const span = document.createElement('span'); span.className = item.lang; span.textContent = item.node.nodeValue; processedNodes.add(span); item.node.parentNode.replaceChild(span, item.node); } }); }); } document.addEventListener('DOMContentLoaded', function() { var savedLang = localStorage.getItem('ag_lang') || 'en'; document.body.classList.add('lang-' + savedLang); syncToggleButtons(savedLang); const content = document.body; classifyText(content); const observer = new MutationObserver(mutations => { for (const mutation of mutations) { for (const node of mutation.addedNodes) { if (node.nodeType === 1) classifyText(node); } } }); observer.observe(content, { childList: true, subtree: true }); }); https://ancient-greece.org/wp-sitemap-posts-post-1.xmlhttps://ancient-greece.org/wp-sitemap-posts-page-1.xmlhttps://ancient-greece.org/wp-sitemap-taxonomies-category-1.xmlhttps://ancient-greece.org/wp-sitemap-taxonomies-post_tag-1.xmlhttps://ancient-greece.org/wp-sitemap-users-1.xml