/** * Advanced Taxonomy Reengineering - SJB Final Interceptor * Optimized for AcademicCareers.com */ (function() { const reengineerCategories = () => { const selectNodes = document.querySelectorAll('select[name="categories[]"]'); selectNodes.forEach(selectElement => { if (selectElement.dataset.reengineered === 'true') return; const $select = window.jQuery ? jQuery(selectElement) : null; // 1. Destroy existing widget to modify underlying DOM structure if ($select && $select.data("ech-multiselect")) { $select.multiselect('destroy'); } const optionsArray = Array.from(selectElement.options); let currentOptGroup = null; optionsArray.forEach(option => { const text = option.text; // 2. Identify "Header" markers and create if (text.includes('(Do NOT select this one')) { const cleanLabel = text.split(':')[0].trim(); currentOptGroup = document.createElement('optgroup'); currentOptGroup.label = cleanLabel; selectElement.insertBefore(currentOptGroup, option); option.remove(); } // 3. Remove legacy visual separators else if (text.includes('---')) { option.remove(); } // 4. Nest valid options else if (currentOptGroup && option.value !== "") { currentOptGroup.appendChild(option); } }); selectElement.dataset.reengineered = 'true'; // 5. Re-initialize Widget and Sanitize visible headers if ($select) { $select.multiselect({ selectedText: "# selected", noneSelectedText: "Categories", header: false, menuHeight: 'auto', buttonWidth: 'auto' }); const widget = $select.multiselect("widget"); if (widget) { widget.find(".ui-multiselect-optgroup a").each(function() { const $header = jQuery(this); const cleanText = $header.text().split(':')[0].replace('(Do NOT select this one', '').trim(); $header.text(cleanText); }); } } }); }; // Delay execution to account for SJB's bottom-of-page scripts window.addEventListener('load', () => { setTimeout(reengineerCategories, 250); const observer = new MutationObserver(() => reengineerCategories()); observer.observe(document.body, { childList: true, subtree: true }); }); })();