MediaWiki:Common.js: Difference between revisions

From MB Wiki
Jump to navigation Jump to search
mNo edit summary
Tag: Reverted
mNo edit summary
Tag: Reverted
Line 151: Line 151:
     console.log('Emergency title fix applied');
     console.log('Emergency title fix applied');
})();
})();
// ===== SIMPLE FONT FIX =====
// ===== WIKI FONT FIXER =====
(function() {
(function() {
     'use strict';
     'use strict';
      
      
     function forceWikiFonts() {
     console.log('MB Wiki: Loading Font Fixer...');
         console.log('MB Wiki: Forcing fonts...');
   
    // Configuration
    var fontConfig = {
        bodyFontFamily: 'Helvetica, Arial, sans-serif',
        bodyFontSize: '16px',
        codeFontFamily: 'monospace, monospace',
        codeFontSize: '13px',
        headingsFontFamily: 'Verdana, Helvetica, Arial, sans-serif',
        debug: false // Set to true to see detailed logs
    };
   
    // Utility function to apply styles with !important
    function applyFontStyle(element, property, value) {
         if (!element) return;
        element.style.setProperty(property, value, 'important');
    }
   
    // Apply styles to selector
    function applyToSelector(selector, property, value) {
        document.querySelectorAll(selector).forEach(function(el) {
            applyFontStyle(el, property, value);
        });
    }
   
    // Main font fixing function
    function fixFonts() {
        if (fontConfig.debug) console.log('MB Wiki: Fixing fonts...');
          
          
         // Force body text
         // 1. Fix body text
         document.body.style.fontFamily = 'Helvetica, Arial, sans-serif';
         applyFontStyle(document.body, 'font-family', fontConfig.bodyFontFamily);
         document.body.style.fontSize = '16px';
         applyFontStyle(document.body, 'font-size', fontConfig.bodyFontSize);
          
          
         // Nuclear approach for code editors
         // 2. Fix vector body classes
         var allElements = document.querySelectorAll('*');
        applyToSelector('.vector-body, .mw-body-content, .mw-parser-output', 'font-family', fontConfig.bodyFontFamily);
         allElements.forEach(function(el) {
        applyToSelector('.vector-body, .mw-body-content, .mw-parser-output', 'font-size', fontConfig.bodyFontSize);
             var tag = el.tagName.toLowerCase();
       
             var className = el.className || '';
        // 3. CRITICAL: Protect code editors
         var codeSelectors = [
            '.CodeMirror',
            '.CodeMirror *',
            '.ace_editor',
            '.ace_editor *',
            '.ve-ce-contentBranchNode',
            '.ve-ce-contentBranchNode *',
            '.wikiEditor-ui .ace_editor',
            '.wikiEditor-ui .ace_editor *',
            '.mw-editfont-monospace',
            '.mw-editfont-monospace *',
            'textarea',
            'input[type="text"]',
            'input[type="search"]',
            'input[type="password"]',
            '.mw-abusefilter-editor',
            '.mw-abusefilter-editor *'
        ];
       
        codeSelectors.forEach(function(selector) {
            applyToSelector(selector, 'font-family', fontConfig.codeFontFamily);
            applyToSelector(selector, 'font-size', fontConfig.codeFontSize);
            applyToSelector(selector, 'line-height', '1.5');
        });
       
        // 4. Fix inline code/pre
        applyToSelector('pre, code, .mw-code', 'font-family', fontConfig.codeFontFamily);
        applyToSelector('pre, code, .mw-code', 'font-size', fontConfig.codeFontSize);
       
        // 5. Fix headings
        applyToSelector('h1, .firstHeading', 'font-family', fontConfig.headingsFontFamily);
        applyToSelector('h1, .firstHeading', 'font-size', '32px');
        applyToSelector('h1, .firstHeading', 'font-weight', 'bold');
       
        applyToSelector('h2', 'font-family', fontConfig.headingsFontFamily);
        applyToSelector('h2', 'font-size', '26px');
        applyToSelector('h2', 'font-weight', 'bold');
       
        applyToSelector('h3', 'font-family', fontConfig.headingsFontFamily);
        applyToSelector('h3', 'font-size', '22px');
        applyToSelector('h3', 'font-weight', 'bold');
       
        // 6. Fix tables
        applyToSelector('.wikitable td, .wikitable th, .scheduletable td, .scheduletable th', 'font-family', fontConfig.bodyFontFamily);
        applyToSelector('.wikitable td, .wikitable th, .scheduletable td, .scheduletable th', 'font-size', '14px');
       
        if (fontConfig.debug) console.log('MB Wiki: Font fixes applied');
    }
   
    // Initialize font fixer
    function initFontFixer() {
        console.log('MB Wiki: Initializing Font Fixer');
          
        // Use your existing waitFor function for better integration
        window.dev.waitFor('body', function() {
             // Run immediately
            fixFonts();
           
            // Run with delays for dynamic content
            setTimeout(fixFonts, 100);
            setTimeout(fixFonts, 500);
            setTimeout(fixFonts, 1000);
            setTimeout(fixFonts, 3000);
           
            // Also run when content is added
            mw.hook('wikipage.content').add(fixFonts);
              
            // Monitor for new code editors
            var observer = new MutationObserver(function(mutations) {
                var needsFix = false;
                mutations.forEach(function(mutation) {
                    if (mutation.addedNodes.length) {
                        mutation.addedNodes.forEach(function(node) {
                            if (node.nodeType === 1) { // Element node
                                if (node.matches && (
                                    node.matches('.CodeMirror, .ace_editor, textarea, pre, code') ||
                                    node.querySelector('.CodeMirror, .ace_editor, textarea, pre, code')
                                )) {
                                    needsFix = true;
                                }
                            }
                        });
                    }
                });
                if (needsFix) {
                    setTimeout(fixFonts, 100);
                }
            });
              
              
             // Check if it's a code editor element
             observer.observe(document.body, {
            var isCodeElement =
                 childList: true,
                tag === 'pre' ||
                 subtree: true
                tag === 'code' ||
            });
                tag === 'textarea' ||
                className.includes('CodeMirror') ||
                className.includes('ace_editor') ||
                 className.includes('ve-ce') ||
                 className.includes('wikiEditor') ||
                className.includes('mw-editfont');
              
              
             if (isCodeElement) {
             console.log('MB Wiki: Font Fixer initialized successfully');
                el.style.fontFamily = 'monospace, monospace';
                el.style.fontSize = '13px';
                el.style.lineHeight = '1.5';
            }
         });
         });
     }
     }
      
      
     // Use your existing waitFor pattern
     // Start the font fixer
     window.dev.waitFor('body', function() {
     if (document.readyState === 'loading') {
         // Run multiple times
         document.addEventListener('DOMContentLoaded', initFontFixer);
        forceWikiFonts();
    } else {
        setTimeout(forceWikiFonts, 100);
         initFontFixer();
        setTimeout(forceWikiFonts, 500);
     }
        setTimeout(forceWikiFonts, 1000);
       
        // Also run when page content changes
        mw.hook('wikipage.content').add(function() {
            setTimeout(forceWikiFonts, 100);
         });
     });
      
      
    console.log('MB Wiki: Font fix loaded');
})();
})();

Revision as of 06:25, 15 December 2025

var config = mw.config.values;
window.dev = window.dev || {};
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
[
	'ThemeToggle.js',	// Add link button on sidebar for mass renaming files and pages
	'MassRename.js',	// Add link button on sidebar for mass renaming files and pages
	'BetterUpload.js',	// Improvements to Special:Upload
	'BetterDiff.js'		// Aids with patrolling and with diff viewing on RC, page history and user contribs
]
.forEach(function(src){
	importScript('MediaWiki:'+src);
});

// Run when page content is added and loaded
mw.hook('wikipage.content').add(function(){
	// Add navigational keybinds
	if (config.wgAction=='view') {
		var actions = {
			e: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=edit'; },
			h: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=history'; },
			m: function(page){ return config.wgServer+mw.util.getUrl('Special:MovePage/'+page); },
			d: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=delete'; },
		};
		var url = new URL(document.location.href);
		var title = 
			url.searchParams.get('title') ||			// for `/w/index.php?title=XYZ` links
			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
if (config.wgUserName && config.wgUserName.length>0) {
	importScript('User:'+config.wgUserName+'/common.js');
	importStylesheet('User:'+config.wgUserName+'/common.css');
}
// === EMERGENCY TITLE OVERRIDE ===
(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');
})();
// ===== WIKI FONT FIXER =====
(function() {
    'use strict';
    
    console.log('MB Wiki: Loading Font Fixer...');
    
    // Configuration
    var fontConfig = {
        bodyFontFamily: 'Helvetica, Arial, sans-serif',
        bodyFontSize: '16px',
        codeFontFamily: 'monospace, monospace',
        codeFontSize: '13px',
        headingsFontFamily: 'Verdana, Helvetica, Arial, sans-serif',
        debug: false // Set to true to see detailed logs
    };
    
    // Utility function to apply styles with !important
    function applyFontStyle(element, property, value) {
        if (!element) return;
        element.style.setProperty(property, value, 'important');
    }
    
    // Apply styles to selector
    function applyToSelector(selector, property, value) {
        document.querySelectorAll(selector).forEach(function(el) {
            applyFontStyle(el, property, value);
        });
    }
    
    // Main font fixing function
    function fixFonts() {
        if (fontConfig.debug) console.log('MB Wiki: Fixing fonts...');
        
        // 1. Fix body text
        applyFontStyle(document.body, 'font-family', fontConfig.bodyFontFamily);
        applyFontStyle(document.body, 'font-size', fontConfig.bodyFontSize);
        
        // 2. Fix vector body classes
        applyToSelector('.vector-body, .mw-body-content, .mw-parser-output', 'font-family', fontConfig.bodyFontFamily);
        applyToSelector('.vector-body, .mw-body-content, .mw-parser-output', 'font-size', fontConfig.bodyFontSize);
        
        // 3. CRITICAL: Protect code editors
        var codeSelectors = [
            '.CodeMirror',
            '.CodeMirror *',
            '.ace_editor',
            '.ace_editor *',
            '.ve-ce-contentBranchNode',
            '.ve-ce-contentBranchNode *',
            '.wikiEditor-ui .ace_editor',
            '.wikiEditor-ui .ace_editor *',
            '.mw-editfont-monospace',
            '.mw-editfont-monospace *',
            'textarea',
            'input[type="text"]',
            'input[type="search"]',
            'input[type="password"]',
            '.mw-abusefilter-editor',
            '.mw-abusefilter-editor *'
        ];
        
        codeSelectors.forEach(function(selector) {
            applyToSelector(selector, 'font-family', fontConfig.codeFontFamily);
            applyToSelector(selector, 'font-size', fontConfig.codeFontSize);
            applyToSelector(selector, 'line-height', '1.5');
        });
        
        // 4. Fix inline code/pre
        applyToSelector('pre, code, .mw-code', 'font-family', fontConfig.codeFontFamily);
        applyToSelector('pre, code, .mw-code', 'font-size', fontConfig.codeFontSize);
        
        // 5. Fix headings
        applyToSelector('h1, .firstHeading', 'font-family', fontConfig.headingsFontFamily);
        applyToSelector('h1, .firstHeading', 'font-size', '32px');
        applyToSelector('h1, .firstHeading', 'font-weight', 'bold');
        
        applyToSelector('h2', 'font-family', fontConfig.headingsFontFamily);
        applyToSelector('h2', 'font-size', '26px');
        applyToSelector('h2', 'font-weight', 'bold');
        
        applyToSelector('h3', 'font-family', fontConfig.headingsFontFamily);
        applyToSelector('h3', 'font-size', '22px');
        applyToSelector('h3', 'font-weight', 'bold');
        
        // 6. Fix tables
        applyToSelector('.wikitable td, .wikitable th, .scheduletable td, .scheduletable th', 'font-family', fontConfig.bodyFontFamily);
        applyToSelector('.wikitable td, .wikitable th, .scheduletable td, .scheduletable th', 'font-size', '14px');
        
        if (fontConfig.debug) console.log('MB Wiki: Font fixes applied');
    }
    
    // Initialize font fixer
    function initFontFixer() {
        console.log('MB Wiki: Initializing Font Fixer');
        
        // Use your existing waitFor function for better integration
        window.dev.waitFor('body', function() {
            // Run immediately
            fixFonts();
            
            // Run with delays for dynamic content
            setTimeout(fixFonts, 100);
            setTimeout(fixFonts, 500);
            setTimeout(fixFonts, 1000);
            setTimeout(fixFonts, 3000);
            
            // Also run when content is added
            mw.hook('wikipage.content').add(fixFonts);
            
            // Monitor for new code editors
            var observer = new MutationObserver(function(mutations) {
                var needsFix = false;
                mutations.forEach(function(mutation) {
                    if (mutation.addedNodes.length) {
                        mutation.addedNodes.forEach(function(node) {
                            if (node.nodeType === 1) { // Element node
                                if (node.matches && (
                                    node.matches('.CodeMirror, .ace_editor, textarea, pre, code') ||
                                    node.querySelector('.CodeMirror, .ace_editor, textarea, pre, code')
                                )) {
                                    needsFix = true;
                                }
                            }
                        });
                    }
                });
                if (needsFix) {
                    setTimeout(fixFonts, 100);
                }
            });
            
            observer.observe(document.body, {
                childList: true,
                subtree: true
            });
            
            console.log('MB Wiki: Font Fixer initialized successfully');
        });
    }
    
    // Start the font fixer
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initFontFixer);
    } else {
        initFontFixer();
    }
    
})();