MediaWiki:Common.js: Difference between revisions

From MB Wiki
Jump to navigation Jump to search
mNo edit summary
Tag: Reverted
m (fix title set.)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
var config = mw.config.values;
/* ============================================
window.dev = window.dev || {};
  THEME TOGGLER
window.dev.waitFor = function(query, callback, extraDelay) {
  ============================================ */
if ('function' == typeof callback && 'string' == typeof query) {
extraDelay = extraDelay || 0;
if (document.querySelector(query)) {
setTimeout(callback, extraDelay);
} else {
// set up the mutation observer
var observer = new MutationObserver(function (mutations, me) {
// mutations is an array of mutations that occurred
// me is the MutationObserver instance
var targetNode = document.querySelector(query);
if (targetNode) {
setTimeout(callback, extraDelay);
me.disconnect(); // stop observing
return;
}
});
// start observing
observer.observe(document, {
  childList: true,
  subtree: true
});
}
}
};


// Load site JS
// 1. INSTANT EXECUTION (PREVENTS FLASH OF WRONG THEME)
[
// This runs the millisecond the script loads, before the body renders.
'ThemeToggle.js', // Add link button on sidebar for mass renaming files and pages
(function() {
'MassRename.js', // Add link button on sidebar for mass renaming files and pages
    var savedTheme = localStorage.getItem('mb-wiki-theme');
'BetterUpload.js', // Improvements to Special:Upload
    if (savedTheme === 'break') {
'BetterDiff.js' // Aids with patrolling and with diff viewing on RC, page history and user contribs
        document.documentElement.classList.add('theme-break');
]
    }
.forEach(function(src){
})();
importScript('MediaWiki:'+src);
 
});
// 2. DOM READY EXECUTION (UI BINDING)
$(function() {
    var isBreak = document.documentElement.classList.contains('theme-break');
   
    var $btn = $('<button>', {
        id: 'theme-toggle',
        class: 'floating-theme-toggle',
        text: isBreak ? '✦ Switch to Mana' : '✦ Switch to Break',
        title: 'Toggle Light/Dark Theme'
    });
   
    $('body').append($btn);


// Run when page content is added and loaded
    $btn.on('click', function() {
mw.hook('wikipage.content').add(function(){
        var $html = $('html');
// Add navigational keybinds
       
if (config.wgAction=='view') {
        if ($html.hasClass('theme-break')) {
var actions = {
            // Switching to Mana (Light)
e: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=edit'; },
            $html.removeClass('theme-break');
h: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=history'; },
            localStorage.setItem('mb-wiki-theme', 'mana');
m: function(page){ return config.wgServer+mw.util.getUrl('Special:MovePage/'+page); },
            $(this).text('✦ Switch to Break');
d: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=delete'; },
        } else {
};
            // Switching to Break (Dark)
var url = new URL(document.location.href);
            $html.addClass('theme-break');
var title =
            localStorage.setItem('mb-wiki-theme', 'break');
url.searchParams.get('title') || // for `/w/index.php?title=XYZ` links
            $(this).text('✦ Switch to Mana');
url.pathname.replace(/^.*?wi?k?i?\//, ''); // for `/wiki/XYZ` links
        }
$(document).on('keydown', function(e) {
    });
var key = e.key.toLocaleLowerCase();
if (
actions[key] &&
!(e.target && ['INPUT', 'TEXTAREA'].includes(e.target.nodeName)) &&
config.wgCanonicalSpecialPageName===false
) {
document.location.href= actions[key](title);
}
});
}
// Run through sidebar to make sure classes align with collapse status
window.dev.waitFor('#mw-navigation > .collapsible-nav > nav', function() {
$('#mw-navigation > .collapsible-nav > nav').each(function(_, nav) {
if (getComputedStyle(nav).display!=='none') {
if (
nav.classList.contains('collapsed') &&
getComputedStyle(nav.querySelector('.vector-menu-content')).display!=='none'
) {
nav.classList.remove('collapsed');
nav.classList.add('expanded');
} else if (
nav.classList.contains('expanded') &&
getComputedStyle(nav.querySelector('.vector-menu-content')).display=='none'
) {
nav.classList.add('collapsed');
nav.classList.remove('expanded');
}
}
});
});
});
});
 
/* ============================================
// Load personal JS & CSS if logged in user
  TITTLE FIX
if (config.wgUserName && config.wgUserName.length>0) {
  ============================================ */
importScript('User:'+config.wgUserName+'/common.js');
importStylesheet('User:'+config.wgUserName+'/common.css');
}
// === EMERGENCY TITLE OVERRIDE ===
(function() {
(function() {
     // Only on main page
     // Only on main page
Line 150: Line 101:
      
      
     console.log('Emergency title fix applied');
     console.log('Emergency title fix applied');
})();/* ============================================
  THEME TOGGLER
  ============================================ */
// 1. INSTANT EXECUTION (PREVENTS FLASH OF WRONG THEME)
// This runs the millisecond the script loads, before the body renders.
(function() {
    var savedTheme = localStorage.getItem('mb-wiki-theme');
    if (savedTheme === 'break') {
        document.documentElement.classList.add('theme-break');
    }
})();
})();
/* Navigation Fix */
// Fix navigation to match bottom code structure
$(document).ready(function() {
    // Wait a bit to ensure DOM is fully loaded
    setTimeout(function() {
        // 1. Remove webfonts-changed class from dropdown checkboxes
        $('#p-cactions-checkbox.webfonts-changed').removeClass('webfonts-changed');
        $('#p-variants-checkbox.webfonts-changed').removeClass('webfonts-changed');
       
        // 2. Add vectorTabs class to navigation menus (like bottom code)
        $('#p-namespaces, #p-views').addClass('vectorTabs');
       
        // 3. Add 'menu' class to content lists (like bottom code)
        $('#p-namespaces .vector-menu-content-list, #p-views .vector-menu-content-list')
            .addClass('menu');
       
        // 4. Fix dropdown functionality if needed
        fixDropdownFunctionality();
       
        // 5. Optional: Hide personal tools if you want to match bottom structure
        // $('#p-personal').hide();
       
        console.log('Navigation structure fixed to match target design');
    }, 100);
});


function fixDropdownFunctionality() {
// 2. DOM READY EXECUTION (UI BINDING)
    // Make sure dropdowns work with checkbox system
$(function() {
    $('.vector-menu-dropdown .vector-menu-heading').each(function() {
    var isBreak = document.documentElement.classList.contains('theme-break');
        var $heading = $(this);
   
        var $checkbox = $heading.prev('.vector-menu-checkbox');
    var $btn = $('<button>', {
        var $content = $heading.next('.vector-menu-content');
        id: 'theme-toggle',
       
        class: 'floating-theme-toggle',
        if ($checkbox.length && $content.length) {
        text: isBreak ? '✦ Switch to Mana' : '✦ Switch to Break',
            // Toggle dropdown on click
         title: 'Toggle Light/Dark Theme'
            $heading.on('click', function(e) {
                e.preventDefault();
                e.stopPropagation();
               
                var isChecked = $checkbox.prop('checked');
                $checkbox.prop('checked', !isChecked);
               
                // Close other open dropdowns
                $('.vector-menu-checkbox').not($checkbox).prop('checked', false);
                updateDropdowns();
            });
         }
     });
     });
      
      
    // Close dropdowns when clicking outside
     $('body').append($btn);
     $(document).on('click', function(e) {
        if (!$(e.target).closest('.vector-menu-dropdown').length) {
            $('.vector-menu-checkbox').prop('checked', false);
            updateDropdowns();
        }
    });
}


function updateDropdowns() {
     $btn.on('click', function() {
     $('.vector-menu-checkbox').each(function() {
         var $html = $('html');
         var $checkbox = $(this);
        var $content = $checkbox.nextAll('.vector-menu-content').first();
          
          
         if ($checkbox.prop('checked')) {
         if ($html.hasClass('theme-break')) {
             $content.css({
            // Switching to Mana (Light)
                'display': 'block',
             $html.removeClass('theme-break');
                'opacity': '1',
            localStorage.setItem('mb-wiki-theme', 'mana');
                'visibility': 'visible'
            $(this).text('✦ Switch to Break');
            });
         } else {
         } else {
             $content.css({
            // Switching to Break (Dark)
                'display': 'none',
             $html.addClass('theme-break');
                'opacity': '0',
            localStorage.setItem('mb-wiki-theme', 'break');
                'visibility': 'hidden'
            $(this).text('✦ Switch to Mana');
            });
         }
         }
     });
     });
}
});
// Mobile support for race hover infobox
/* ============================================
$(document).ready(function() {
  TITTLE FIX
     if ($(window).width() <= 768) {
  ============================================ */
         $('.race-hover-trigger').on('click', function(e) {
(function() {
            e.preventDefault();
    // Only on main page
            e.stopPropagation();
     if (mw.config.get('wgPageName') !== 'Main_Page' &&
           
         !window.location.href.includes('Main_Page')) return;
            var $container = $(this).closest('.race-hover-container');
   
            var $overlay = $('.race-hover-overlay');
    console.log('=== APPLYING EMERGENCY TITLE FIX ===');
           
   
            // Close other open infoboxes
    // 1. IMMEDIATELY set the title
            $('.race-hover-container').not($container).removeClass('mobile-active');
    document.title = 'MB Wiki - The Official Wiki';
              
   
             // Toggle current infobox
    // 2. Override the heading
            $container.toggleClass('mobile-active');
    function fixHeading() {
           
        var heading = document.querySelector('#firstHeading, h1.firstHeading, .mw-page-title-main');
             // Create overlay if it doesn't exist
        if (heading) {
             if ($overlay.length === 0) {
            heading.textContent = 'MB Wiki - The Official Wiki';
                $overlay = $('<div class="race-hover-overlay"></div>');
             heading.style.display = 'none';
                $('body').append($overlay);
             heading.style.visibility = 'hidden';
            }
        } else {
           
             // Create heading if it doesn't exist
            // Close on overlay click
             var newHeading = document.createElement('h1');
            $overlay.off('click').on('click', function() {
            newHeading.id = 'firstHeading';
                $container.removeClass('mobile-active');
            newHeading.className = 'firstHeading';
                $(this).remove();
            newHeading.textContent = 'MB Wiki - The Official Wiki';
            });
            var content = document.querySelector('#content, .mw-body');
           
            if (content) content.prepend(newHeading);
            // Close on escape key
        }
             $(document).off('keyup.raceHover').on('keyup.raceHover', function(e) {
    }
                if (e.keyCode === 27) { // ESC key
   
                    $container.removeClass('mobile-active');
    // 3. Run now and keep running
                    $overlay.remove();
    fixHeading();
                }
    setTimeout(fixHeading, 100);
             });
    setTimeout(fixHeading, 500);
    setTimeout(fixHeading, 1000);
    setTimeout(fixHeading, 2000);
   
    // 4. Monitor and fix any time title changes
    var lastTitle = document.title;
    setInterval(function() {
        if (document.title !== lastTitle &&
             document.title.includes('Coral Island')) {
            console.log('Title changed to wrong value, fixing...');
            document.title = 'MB Wiki - The Official Wiki';
            lastTitle = document.title;
            fixHeading();
        }
    }, 500);
   
    // 5. Also fix meta tags
    function fixMetaTags() {
        var metaTags = document.querySelectorAll('meta[property="og:title"], meta[name="twitter:title"]');
        metaTags.forEach(function(tag) {
             tag.setAttribute('content', 'MB Wiki - The Official Wiki');
         });
         });
     }
     }
});
    fixMetaTags();
   
    console.log('Emergency title fix applied');
})();

Latest revision as of 04:08, 19 May 2026

/* ============================================
   THEME TOGGLER
   ============================================ */

// 1. INSTANT EXECUTION (PREVENTS FLASH OF WRONG THEME)
// This runs the millisecond the script loads, before the body renders.
(function() {
    var savedTheme = localStorage.getItem('mb-wiki-theme');
    if (savedTheme === 'break') {
        document.documentElement.classList.add('theme-break');
    }
})();

// 2. DOM READY EXECUTION (UI BINDING)
$(function() {
    var isBreak = document.documentElement.classList.contains('theme-break');
    
    var $btn = $('<button>', {
        id: 'theme-toggle',
        class: 'floating-theme-toggle',
        text: isBreak ? '✦ Switch to Mana' : '✦ Switch to Break',
        title: 'Toggle Light/Dark Theme'
    });
    
    $('body').append($btn);

    $btn.on('click', function() {
        var $html = $('html');
        
        if ($html.hasClass('theme-break')) {
            // Switching to Mana (Light)
            $html.removeClass('theme-break');
            localStorage.setItem('mb-wiki-theme', 'mana');
            $(this).text('✦ Switch to Break');
        } else {
            // Switching to Break (Dark)
            $html.addClass('theme-break');
            localStorage.setItem('mb-wiki-theme', 'break');
            $(this).text('✦ Switch to Mana');
        }
    });
});
/* ============================================
   TITTLE FIX
   ============================================ */
(function() {
    // Only on main page
    if (mw.config.get('wgPageName') !== 'Main_Page' && 
        !window.location.href.includes('Main_Page')) return;
    
    console.log('=== APPLYING EMERGENCY TITLE FIX ===');
    
    // 1. IMMEDIATELY set the title
    document.title = 'MB Wiki - The Official Wiki';
    
    // 2. Override the heading
    function fixHeading() {
        var heading = document.querySelector('#firstHeading, h1.firstHeading, .mw-page-title-main');
        if (heading) {
            heading.textContent = 'MB Wiki - The Official Wiki';
            heading.style.display = 'none';
            heading.style.visibility = 'hidden';
        } else {
            // Create heading if it doesn't exist
            var newHeading = document.createElement('h1');
            newHeading.id = 'firstHeading';
            newHeading.className = 'firstHeading';
            newHeading.textContent = 'MB Wiki - The Official Wiki';
            var content = document.querySelector('#content, .mw-body');
            if (content) content.prepend(newHeading);
        }
    }
    
    // 3. Run now and keep running
    fixHeading();
    setTimeout(fixHeading, 100);
    setTimeout(fixHeading, 500);
    setTimeout(fixHeading, 1000);
    setTimeout(fixHeading, 2000);
    
    // 4. Monitor and fix any time title changes
    var lastTitle = document.title;
    setInterval(function() {
        if (document.title !== lastTitle && 
            document.title.includes('Coral Island')) {
            console.log('Title changed to wrong value, fixing...');
            document.title = 'MB Wiki - The Official Wiki';
            lastTitle = document.title;
            fixHeading();
        }
    }, 500);
    
    // 5. Also fix meta tags
    function fixMetaTags() {
        var metaTags = document.querySelectorAll('meta[property="og:title"], meta[name="twitter:title"]');
        metaTags.forEach(function(tag) {
            tag.setAttribute('content', 'MB Wiki - The Official Wiki');
        });
    }
    fixMetaTags();
    
    console.log('Emergency title fix applied');
})();/* ============================================
   THEME TOGGLER
   ============================================ */

// 1. INSTANT EXECUTION (PREVENTS FLASH OF WRONG THEME)
// This runs the millisecond the script loads, before the body renders.
(function() {
    var savedTheme = localStorage.getItem('mb-wiki-theme');
    if (savedTheme === 'break') {
        document.documentElement.classList.add('theme-break');
    }
})();

// 2. DOM READY EXECUTION (UI BINDING)
$(function() {
    var isBreak = document.documentElement.classList.contains('theme-break');
    
    var $btn = $('<button>', {
        id: 'theme-toggle',
        class: 'floating-theme-toggle',
        text: isBreak ? '✦ Switch to Mana' : '✦ Switch to Break',
        title: 'Toggle Light/Dark Theme'
    });
    
    $('body').append($btn);

    $btn.on('click', function() {
        var $html = $('html');
        
        if ($html.hasClass('theme-break')) {
            // Switching to Mana (Light)
            $html.removeClass('theme-break');
            localStorage.setItem('mb-wiki-theme', 'mana');
            $(this).text('✦ Switch to Break');
        } else {
            // Switching to Break (Dark)
            $html.addClass('theme-break');
            localStorage.setItem('mb-wiki-theme', 'break');
            $(this).text('✦ Switch to Mana');
        }
    });
});
/* ============================================
   TITTLE FIX
   ============================================ */
(function() {
    // Only on main page
    if (mw.config.get('wgPageName') !== 'Main_Page' && 
        !window.location.href.includes('Main_Page')) return;
    
    console.log('=== APPLYING EMERGENCY TITLE FIX ===');
    
    // 1. IMMEDIATELY set the title
    document.title = 'MB Wiki - The Official Wiki';
    
    // 2. Override the heading
    function fixHeading() {
        var heading = document.querySelector('#firstHeading, h1.firstHeading, .mw-page-title-main');
        if (heading) {
            heading.textContent = 'MB Wiki - The Official Wiki';
            heading.style.display = 'none';
            heading.style.visibility = 'hidden';
        } else {
            // Create heading if it doesn't exist
            var newHeading = document.createElement('h1');
            newHeading.id = 'firstHeading';
            newHeading.className = 'firstHeading';
            newHeading.textContent = 'MB Wiki - The Official Wiki';
            var content = document.querySelector('#content, .mw-body');
            if (content) content.prepend(newHeading);
        }
    }
    
    // 3. Run now and keep running
    fixHeading();
    setTimeout(fixHeading, 100);
    setTimeout(fixHeading, 500);
    setTimeout(fixHeading, 1000);
    setTimeout(fixHeading, 2000);
    
    // 4. Monitor and fix any time title changes
    var lastTitle = document.title;
    setInterval(function() {
        if (document.title !== lastTitle && 
            document.title.includes('Coral Island')) {
            console.log('Title changed to wrong value, fixing...');
            document.title = 'MB Wiki - The Official Wiki';
            lastTitle = document.title;
            fixHeading();
        }
    }, 500);
    
    // 5. Also fix meta tags
    function fixMetaTags() {
        var metaTags = document.querySelectorAll('meta[property="og:title"], meta[name="twitter:title"]');
        metaTags.forEach(function(tag) {
            tag.setAttribute('content', 'MB Wiki - The Official Wiki');
        });
    }
    fixMetaTags();
    
    console.log('Emergency title fix applied');
})();