1document.addEventListener('DOMContentLoaded', () => { 2 const titleField = document.getElementById('seo4wp_meta_title'); 3 const descriptionField = document.getElementById('seo4wp_meta_description'); 4 const titleCounter = document.getElementById('meta_title_counter'); 5 const descriptionCounter = document.getElementById('meta_description_counter'); 6 const serpTitle = document.getElementById('title'); 7 const serpDescription = document.getElementById('description'); 8 9 // Default values from data attributes 10 const defaultTitle = serpTitle.dataset.defaultTitle; 11 const defaultDescription = serpDescription.dataset.defaultDescription; 12 13 function getFieldTextOrDefault(field, defaultText) { 14 return field.value.trim() ? field.value : defaultText; 15 } 16 17 function updateCharacterCountAndColor(field, counter, greenLimit, orangeStart, orangeEnd, serpPreview, serpLimit, defaultText) { 18 const text = getFieldTextOrDefault(field, defaultText); 19 const charCount = text.length; 20 let color = 'red'; 21 22 // Determine color based on specified character ranges 23 if (charCount <= greenLimit) { 24 color = 'green'; 25 } else if (charCount > orangeStart && charCount <= orangeEnd) { 26 color = 'orange'; 27 } 28 29 // Update the counter's color and text 30 counter.style.color = color; 31 counter.textContent = `${charCount} characters`; 32 33 // Update SERP preview with truncation at first space after the specified limit 34 let truncatedText = text; 35 if (charCount > serpLimit) { 36 const firstSpaceAfterLimit = text.indexOf(' ', serpLimit); 37 truncatedText = text.slice(0, firstSpaceAfterLimit > 0 ? firstSpaceAfterLimit : serpLimit) + '...'; 38 } 39 serpPreview.textContent = truncatedText; 40 } 41 42 // Event listeners for title and description fields 43 titleField.addEventListener('input', () => { 44 updateCharacterCountAndColor(titleField, titleCounter, 60, 61, 80, serpTitle, 80, defaultTitle); 45 }); 46 47 descriptionField.addEventListener('input', () => { 48 updateCharacterCountAndColor(descriptionField, descriptionCounter, 140, 141, 165, serpDescription, 160, defaultDescription); 49 }); 50 51 // Initial call to set default counts and previews 52 updateCharacterCountAndColor(titleField, titleCounter, 60, 61, 80, serpTitle, 80, defaultTitle); 53 updateCharacterCountAndColor(descriptionField, descriptionCounter, 140, 141, 165, serpDescription, 160, defaultDescription); 54}); 55 56 57 58 59
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.