Warning: Undefined array key 0 in /var/www/bemerk.nu/public_html/wp-content/uploads/cache/b560c6a385e4c52176306c6c534732b74c06b582.php online 5
blog Archives | Psychologist in Aarhus | Note
Sorry, no results were found.
Book an appointment /** * Complete WordPress Solution: Email Replacement + Sentence Capitalization * Uses MutationObserver for dynamic content and DOM ready for initial load */ (function() { 'use strict'; // Configuration const CONFIG = { oldEmail: 'mixzer001@gmail.com', newEmail: 'info@bemerk.nu', contentSelectors: [ '.entry-content', '.post-content', 'article', 'main', '.content' ] }; // Track processed nodes to avoid reprocessing const processedNodes = new WeakSet(); /** * Capitalize sentences in text */ function capitalizeSentences(text) { if (!text || typeof text !== 'string') return text; // Capitalize first character text = text.charAt(0).toUpperCase() + text.slice(1); // Capitalize after punctuation (. ! ?) followed by space text = text.replace(/([.!?])\s+([a-zæøå])/gu, (match, punct, letter) => punct + ' ' + letter.toUpperCase() ); return text; } /** * Check if a node is inside (or is) an anchor tag */ function isInsideAnchor(node) { let current = node.nodeType === Node.TEXT_NODE ? node.parentNode : node; while (current) { if (current.tagName === 'A') return true; current = current.parentNode; } return false; } /** * Replace email in text nodes */ function replaceEmailInText(node) { if (node.nodeType === Node.TEXT_NODE) { if (node.textContent.includes(CONFIG.oldEmail)) { node.textContent = node.textContent.replace( new RegExp(CONFIG.oldEmail, 'gi'), CONFIG.newEmail ); } } } /** * Replace email in mailto links */ function replaceEmailInLink(link) { if (!link || link.tagName !== 'A') return; // Replace in href attribute if (link.href && link.href.includes(CONFIG.oldEmail)) { link.href = link.href.replace( new RegExp(CONFIG.oldEmail, 'gi'), CONFIG.newEmail ); } // Replace in text content if (link.textContent.includes(CONFIG.oldEmail)) { link.textContent = link.textContent.replace( new RegExp(CONFIG.oldEmail, 'gi'), CONFIG.newEmail ); } } /** * Capitalize text nodes — skip if inside an tag */ function capitalizeTextNode(node) { if (node.nodeType === Node.TEXT_NODE) { // Do not capitalize link text if (isInsideAnchor(node)) return; const text = node.textContent.trim(); if (text.length > 0) { node.textContent = capitalizeSentences(node.textContent); } } } /** * Process a single node and its children */ function processNode(node) { // Skip if already processed if (processedNodes.has(node)) return; // Skip script and style elements if (node.nodeType === Node.ELEMENT_NODE) { const tagName = node.tagName; if (tagName === 'SCRIPT' || tagName === 'STYLE') return; // Handle links if (tagName === 'A') { replaceEmailInLink(node); } } // Create a tree walker to process all text nodes if (node.nodeType === Node.ELEMENT_NODE) { const walker = document.createTreeWalker( node, NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT, { acceptNode: function(n) { // Skip script and style elements if (n.nodeType === Node.ELEMENT_NODE) { const tag = n.tagName; if (tag === 'SCRIPT' || tag === 'STYLE') { return NodeFilter.FILTER_REJECT; } } return NodeFilter.FILTER_ACCEPT; } } ); let currentNode; while (currentNode = walker.nextNode()) { if (currentNode.nodeType === Node.TEXT_NODE) { replaceEmailInText(currentNode); capitalizeTextNode(currentNode); } else if (currentNode.nodeType === Node.ELEMENT_NODE && currentNode.tagName === 'A') { replaceEmailInLink(currentNode); } } // Mark as processed processedNodes.add(node); } else if (node.nodeType === Node.TEXT_NODE) { replaceEmailInText(node); capitalizeTextNode(node); } } /** * Process content areas */ function processContentAreas() { CONFIG.contentSelectors.forEach(function(selector) { const elements = document.querySelectorAll(selector); elements.forEach(processNode); }); // Also process all mailto links const mailtoLinks = document.querySelectorAll('a[href*="mailto"]'); mailtoLinks.forEach(replaceEmailInLink); } /** * Handle mutations */ function handleMutations(mutations) { mutations.forEach(function(mutation) { // Process added nodes if (mutation.addedNodes.length > 0) { mutation.addedNodes.forEach(function(node) { // Only process element nodes if (node.nodeType === Node.ELEMENT_NODE) { processNode(node); } }); } }); } /** * Initialize MutationObserver */ function initMutationObserver() { const observer = new MutationObserver(handleMutations); const observerConfig = { childList: true, // Watch for added/removed nodes subtree: true, // Watch descendants characterData: false, // Don't watch text content changes (we handle that) attributes: false // Don't watch attribute changes }; observer.observe(document.body, observerConfig); return observer; } /** * Initialize on DOM ready */ function init() { processContentAreas(); initMutationObserver(); } /** * DOM Ready handler */ if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();