MediaWiki:Common.js: Difference between revisions

From GladiusRO Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
/*
/*
  * ==========================================================
  * ==========================================================
  * GladiusRO Wiki
  * GladiusRO Wiki - Permanent Sidebar Configuration
  *
  *
  * 1. Move the Table of Contents below Tools.
* Functions:
  * 1. Keep Contents permanently in the left sidebar.
  * 2. Hide technical links from logged-out players.
  * 2. Hide technical links from logged-out players.
  * 3. Keep technical links visible for logged-in administrators.
  * 3. Show technical links to logged-in administrators/editors.
  * ==========================================================
  * ==========================================================
  */
  */
Line 12: Line 13:
     'use strict';
     'use strict';


     var attempts = 0;
     var observerRunning = false;
     var maximumAttempts = 40;
     var refreshTimer = null;


     /*
     /*
     * Move Contents below the Tools section.
     * Find the left sidebar used by the current wiki skin.
     */
     */
     function moveContentsBelowTools() {
     function getGladiusSidebar() {
         var tools = document.getElementById('p-tb');
         return document.getElementById('mw-panel') ||
        var toc = document.getElementById('toc');
            document.getElementById('mw-navigation') ||
        var tocContainer =
             document.querySelector('.sidebar');
             document.getElementById('gladius-sidebar-toc');
    }
 
    /*
    * Check whether the current visitor is logged in.
    *
    * Logged-out player:
    * wgUserName is null.
    *
    * Logged-in administrator/editor:
    * wgUserName contains the account name.
    */
    function isLoggedIn() {
        return mw.config.get('wgUserName') !== null;
    }


        if (!tools || !tools.parentNode) {
    /*
            return false;
    * Create or retrieve the permanent TOC container.
        }
    */
    function getTOCContainer() {*        var container =
          * document.getElementById('gladius-sidebar-toc');


        /*
         if (!container) {
        * If no Gladius container exists yet,
             container = document.createElement('div');
        * create one and move the original TOC into it.
        */
         if (!tocContainer && toc) {
             tocContainer = document.createElement('div');


             tocContainer.id = 'gladius-sidebar-toc';
             container.id = 'gladius-sidebar-toc';
             tocContainer.className = 'portal mw-portlet';
             container.className = 'portal mw-portlet';
             tocContainer.setAttribute(
             container.setAttribute(
                 'role',
                 'role',
                 'navigation'
                 'navigation'
             );
             );
             tocContainer.setAttribute(
             container.setAttribute(
                 'aria-label',
                 'aria-label',
                 'Page contents'
                 'Page contents'
             );
             );
        }
        return container;
    }
    /*
    * Keep Contents permanently in the left sidebar.
    */
    function keepContentsInSidebar() {
        var sidebar = getGladiusSidebar();
        var toc = document.getElementById('toc');


            tocContainer.appendChild(toc);
        if (!sidebar) {
            return false;
         }
         }


         if (!tocContainer) {
        /*
        * Do not create an empty Contents panel on pages
        * without a TOC.
        */
        var existingContainer =
            document.getElementById('gladius-sidebar-toc');
 
         if (!toc && !existingContainer) {
             return false;
             return false;
         }
         }
        var container =
            existingContainer || getTOCContainer();


         /*
         /*
         * Always place the container immediately after Tools.
         * Attach the Contents container directly to
        * the left sidebar.
         *
         *
         * This also corrects the position when an older script
         * This makes placement independent of whether
         * accidentally leaves Contents inside the article.
        * Tools is visible or hidden.
        */
        if (container.parentNode !== sidebar) {
            sidebar.appendChild(container);
        }
 
        /*
        * Move the original article TOC into the
        * permanent sidebar container.
        */
        if (toc && toc.parentNode !== container) {
            container.appendChild(toc);
        }
 
        /*
        * Show the container when it contains a TOC.
         * Hide it on pages without a TOC.
         */
         */
         tools.parentNode.insertBefore(
         if (container.querySelector('#toc')) {
             tocContainer,
            container.style.removeProperty('display');
             tools.nextSibling
        } else {
         );
             container.style.setProperty(
                'display',
                'none',
                'important'
             );
         }


         return true;
         return true;
Line 68: Line 124:


     /*
     /*
     * Hide technical sidebar items from players
     * Hide one element completely.
    * who are not logged in.
     */
     */
     function hidePlayerTechnicalLinks() {
     function hideElement(element) {
         var userName = mw.config.get('wgUserName');
         if (!element) {
            return;
        }
 
        element.style.setProperty(
            'display',
            'none',
            'important'
        );
    }


        /*
    /*
        * A non-null username means the visitor is logged in.
    * Restore an element when an administrator/editor
        * Administrators and logged-in users keep the links.
    * is logged in.
        */
    */
         if (userName !== null) {
    function restoreElement(element) {
         if (!element) {
             return;
             return;
         }
         }


         var elementsToHide = [
        element.style.removeProperty('display');
    }
 
    /*
    * Hide or show technical navigation links.
    */
    function updatePlayerNavigation() {
         var loggedIn = isLoggedIn();
 
        /*
        * IDs commonly used by MediaWiki sidebar items.
        */
        var technicalElementIds * [
             'n-recentchanges',
             'n-recentchanges',
             'n-randompage',
             'n-randompage',
             'n-help',
             'n-help',
            'n-help-mediawiki',
            'n-helppage',
             'p-tb'
             'p-tb'
         ];
         ];


         elementsToHide.forEach(function (elementId) {
         technicalElementIds.forEach(function (id) {
             var element = document.getElementById(elementId);
             var element = document.getElementById(id);


             if (element) {
             if (loggedIn) {
                 element.style.setProperty(
                 restoreElement(element);
                    'display',
            } else {
                    'none',
                 hideElement(element);
                    'important'
                 );
             }
             }
         });
         });
    }
    /*
    * Run both functions in the correct order.
    *
    * Contents must be moved first because the script
    * uses the Tools panel as its placement reference.
    * Tools can then safely be hidden from players.
    */
    function initialiseGladiusSidebar() {
        attempts++;


         var moved = moveContentsBelowTools();
         var sidebar = getGladiusSidebar();


         if (moved) {
         if (!sidebar) {
            hidePlayerTechnicalLinks();
             return;
             return;
         }
         }


         /*
         /*
         * Retry while MediaWiki finishes creating the sidebar.
         * Fallback for older/custom skins where the
        * navigation items use different IDs.
        */
        var hiddenTexts = [
            'recent changes',
            'random page',
            'help about mediawiki',
            'what links here',
            'related changes',
            'special pages',
            'printable version',
            'permanent link',
            'page information'
        ];
 
        var links = sidebar.q*erySelectorAll('a');
 
        Arra*.prototype.forEach.call(
        *  links,
            function (lin*) {
                var text = (li*k.textContent || '')
                    .replace(/\s+/g, ' ')
                    .trim()
                    .toLowerCase();
 
                var href =
                    link.getAttribute('href') || '';
 
                var hideThisLink =
                    hiddenTexts.indexOf(text) !== -1 ||
                    href.indexOf(
                        'mediawiki.org/wiki/Help'
                    ) !== -1;
 
                if (!hideThisLink) {
                    return;
                }
 
                /*
                * Hide or restore the complete list row,
                * avoiding empty spaces in the sidebar.
                */
                var row =
                    link.closest('li') ||
                    link.parentElement;
 
                if (loggedIn) {
                    restoreElement(row);
                    restoreElement(link);
                } else {
                    hideElement(row || link);
                }
            }
        );
 
        /*
        * Fallback: find a section with the visible
        * heading "Tools" if #p-tb is unavailable.
         */
         */
         if (attempts < maximumAttempts) {
         var sidebarElements =
            window.setTimeout(
            sidebar.querySelectorAll('div, h3, h4');
                 initialiseGladiusSidebar,
 
                 200
        Array.prototype.forEach.call(
            );
            sidebarElements,
         }
            function (element) {
                var headingText =
                    (element.textContent || '')
                        .replace(/\s+/g, ' ')
                        .trim()
                        .toLowerCase();
 
                 /*
                * Avoid matching the whole sidebar, whose
                * text may include the word "Tools".
                */
                 if (
                    headingText === 'tools' &&
                    !loggedIn
                ) {
                    var toolsSection =
                        element.closest(
                            '.portal, .mw-portlet'
                        );
 
                    if (toolsSection) {
                        hideElement(toolsSection);
                    }
                }
            }
         );
     }
     }


     /*
     /*
     * Start after the HTML is ready.
     * Apply all sidebar corrections.
    *
    * Contents is moved first. Technical links are hidden
    * afterward, ensuring that hiding Tools does not prevent
    * the TOC from being moved.
     */
     */
     if (document.readyState === 'loading') {
     function applyGladiusSidebar() {
         document.addEventListener(
         keepContentsInSidebar();
            'DOMContentLoaded',
         updatePlayerNavigation();
            initialiseGladiusSidebar
        );
    } else {
         initialiseGladiusSidebar();
     }
     }


     /*
     /*
     * Run again after images and skin resources load.
     * Watch for MediaWiki or skin changes.
    *
    * If MediaWiki places Contents inside the article again,
    * the observer moves Contents back to the sidebar.
     */
     */
     window.addEventListener(
     function startGladiusObserve*() {
         'load',
         if (
         function () {
            obse*verRunning ||
             attempts = 0;
            !document.body ||
            initialiseGladiusSidebar();
            typeof MutationObserver === 'undefined'
         ) {
             return;
         }
         }
    );


    /*
        observerRunning = true;
    * Run again if MediaWiki updates article content.
 
    */
         var observer = new MutationObserver(function () {
    if (mw.hook) {
             window.clearTimeout(refreshTimer);
         mw.hook('wikipage.content').add(function () {
             attempts = 0;


             window.setTimeout(
             refreshTimer = window.setTimeout(
                 initialiseGladiusSidebar,
                 applyGladiusSidebar,
                 100
                 100
             );
             );
        });
        observer.observe(document.body, {
            childList: true,
            subtree: true
         });
         });
     }
     }


}(mediaWiki));
    /*
    * Initial setup.
    */
    function initialiseGladiusSidebar() {
        applyGladiusSidebar();
        startGladiusObserver();


        /*
        * Additional checks for older skins and
        * delayed ResourceLoader components.
        */
        window.setTimeout(
            applyGladiusSidebar,
            250
        );


/*
         window.setTimeout(
* Remove "Help about MediaWiki" from the sidebar.
             applyGladiusSidebar,
*/
            800
(function () {
    'use strict';*
    function removeMediaWikiHelpL*nk() {
         var links = documen*.querySelectorAll(
             '#m*-panel a, #mw-navigation a, .sideb*r a'
         );
         );


         links.for*ach(function (link) {
         window.setTimeout(
             var linkText = link.textContent
             applyGladiusSidebar,
                .replace(/\s+/g, ' ')
             1600
                .trim()
         );
                .toLowerCase();
 
            var linkAddress = link.getAttribute('href') || '';
 
            if (
                linkText === 'help about mediawiki' ||
                linkAddress.indexOf('mediawiki.org/wiki/Help') !== -1
            ) {
                /*
                * Remove the complete list item so that
                * no empty space remains in the sidebar.
                */
                var listItem = l*nk.closest('li');
 
              *if (listItem) {
                  * listItem.remove();
              * } else {
                    link.remove();
                }
             }
         });
     }
     }


    /*
    * Start when the document is ready.
    */
     if (document.readyState === 'loading') {
     if (document.readyState === 'loading') {
         document.addEventListener(
         document.addEventListener(
             'DOMContentLoaded',
             'DOMContentLoaded',
             removeMediaWikiHelpLink
             initialiseGladiusSidebar
         );
         );
     } else {
     } else {
         removeMediaWikiHelpLink();
         initialiseGladiusSidebar();
     }
     }


     /*
     /*
     * Run again after the skin finishes loading.
     * Run again after images and skin assets finish loading.
     */
     */
     window.addEventListener(
     window.addEventListener(
         'load',
         'load',
         removeMediaWikiHelpLink
         applyGladiusSidebar
     );
     );


     /*
     /*
     * Run again if MediaWiki refreshes page content.
     * Run after MediaWiki dynamically updates article content.
     */
     */
     if (
     if (mw.hook) {
        typeof mediaWik* !== 'undefined' &&
         mw.ho*k('wikipage.content').add(function*() {
        mediaW*ki.hook
             window.setTimeout*
    ) {
                 applyGladiusSideb*r,
         mediaWiki.*ook('wikipage.content').add(functi*n () {
             window.setTimeo*t(
                 removeMediaWiki*elpLink,
                 100
                 100
    *     );
          *);
         });
         });
     }
     }
}());


/* Possible IDs use* for the MediaWiki Help sidebar it*m */
}(mediaWiki)*;
#n-help,
#n-help-mediawiki,
#*-helppage,
li[id*="help"] {
    di*play: none !important;
}

Revision as of 03:23, 2 August 2026

/*
 * ==========================================================
 * GladiusRO Wiki - Permanent Sidebar Configuration
 *
 * Functions:
 * 1. Keep Contents permanently in the left sidebar.
 * 2. Hide technical links from logged-out players.
 * 3. Show technical links to logged-in administrators/editors.
 * ==========================================================
 */

(function (mw) {
    'use strict';

    var observerRunning = false;
    var refreshTimer = null;

    /*
     * Find the left sidebar used by the current wiki skin.
     */
    function getGladiusSidebar() {
        return document.getElementById('mw-panel') ||
            document.getElementById('mw-navigation') ||
            document.querySelector('.sidebar');
    }

    /*
     * Check whether the current visitor is logged in.
     *
     * Logged-out player:
     * wgUserName is null.
     *
     * Logged-in administrator/editor:
     * wgUserName contains the account name.
     */
    function isLoggedIn() {
        return mw.config.get('wgUserName') !== null;
    }

    /*
     * Create or retrieve the permanent TOC container.
     */
    function getTOCContainer() {*        var container =
          * document.getElementById('gladius-sidebar-toc');

        if (!container) {
            container = document.createElement('div');

            container.id = 'gladius-sidebar-toc';
            container.className = 'portal mw-portlet';
            container.setAttribute(
                'role',
                'navigation'
            );
            container.setAttribute(
                'aria-label',
                'Page contents'
            );
        }

        return container;
    }

    /*
     * Keep Contents permanently in the left sidebar.
     */
    function keepContentsInSidebar() {
        var sidebar = getGladiusSidebar();
        var toc = document.getElementById('toc');

        if (!sidebar) {
            return false;
        }

        /*
         * Do not create an empty Contents panel on pages
         * without a TOC.
         */
        var existingContainer =
            document.getElementById('gladius-sidebar-toc');

        if (!toc && !existingContainer) {
            return false;
        }

        var container =
            existingContainer || getTOCContainer();

        /*
         * Attach the Contents container directly to
         * the left sidebar.
         *
         * This makes placement independent of whether
         * Tools is visible or hidden.
         */
        if (container.parentNode !== sidebar) {
            sidebar.appendChild(container);
        }

        /*
         * Move the original article TOC into the
         * permanent sidebar container.
         */
        if (toc && toc.parentNode !== container) {
            container.appendChild(toc);
        }

        /*
         * Show the container when it contains a TOC.
         * Hide it on pages without a TOC.
         */
        if (container.querySelector('#toc')) {
            container.style.removeProperty('display');
        } else {
            container.style.setProperty(
                'display',
                'none',
                'important'
            );
        }

        return true;
    }

    /*
     * Hide one element completely.
     */
    function hideElement(element) {
        if (!element) {
            return;
        }

        element.style.setProperty(
            'display',
            'none',
            'important'
        );
    }

    /*
     * Restore an element when an administrator/editor
     * is logged in.
     */
    function restoreElement(element) {
        if (!element) {
            return;
        }

        element.style.removeProperty('display');
    }

    /*
     * Hide or show technical navigation links.
     */
    function updatePlayerNavigation() {
        var loggedIn = isLoggedIn();

        /*
         * IDs commonly used by MediaWiki sidebar items.
         */
        var technicalElementIds * [
            'n-recentchanges',
            'n-randompage',
            'n-help',
            'n-help-mediawiki',
            'n-helppage',
            'p-tb'
        ];

        technicalElementIds.forEach(function (id) {
            var element = document.getElementById(id);

            if (loggedIn) {
                restoreElement(element);
            } else {
                hideElement(element);
            }
        });

        var sidebar = getGladiusSidebar();

        if (!sidebar) {
            return;
        }

        /*
         * Fallback for older/custom skins where the
         * navigation items use different IDs.
         */
        var hiddenTexts = [
            'recent changes',
            'random page',
            'help about mediawiki',
            'what links here',
            'related changes',
            'special pages',
            'printable version',
            'permanent link',
            'page information'
        ];

        var links = sidebar.q*erySelectorAll('a');

        Arra*.prototype.forEach.call(
         *  links,
            function (lin*) {
                var text = (li*k.textContent || '')
                    .replace(/\s+/g, ' ')
                    .trim()
                    .toLowerCase();

                var href =
                    link.getAttribute('href') || '';

                var hideThisLink =
                    hiddenTexts.indexOf(text) !== -1 ||
                    href.indexOf(
                        'mediawiki.org/wiki/Help'
                    ) !== -1;

                if (!hideThisLink) {
                    return;
                }

                /*
                 * Hide or restore the complete list row,
                 * avoiding empty spaces in the sidebar.
                 */
                var row =
                    link.closest('li') ||
                    link.parentElement;

                if (loggedIn) {
                    restoreElement(row);
                    restoreElement(link);
                } else {
                    hideElement(row || link);
                }
            }
        );

        /*
         * Fallback: find a section with the visible
         * heading "Tools" if #p-tb is unavailable.
         */
        var sidebarElements =
            sidebar.querySelectorAll('div, h3, h4');

        Array.prototype.forEach.call(
            sidebarElements,
            function (element) {
                var headingText =
                    (element.textContent || '')
                        .replace(/\s+/g, ' ')
                        .trim()
                        .toLowerCase();

                /*
                 * Avoid matching the whole sidebar, whose
                 * text may include the word "Tools".
                 */
                if (
                    headingText === 'tools' &&
                    !loggedIn
                ) {
                    var toolsSection =
                        element.closest(
                            '.portal, .mw-portlet'
                        );

                    if (toolsSection) {
                        hideElement(toolsSection);
                    }
                }
            }
        );
    }

    /*
     * Apply all sidebar corrections.
     *
     * Contents is moved first. Technical links are hidden
     * afterward, ensuring that hiding Tools does not prevent
     * the TOC from being moved.
     */
    function applyGladiusSidebar() {
        keepContentsInSidebar();
        updatePlayerNavigation();
    }

    /*
     * Watch for MediaWiki or skin changes.
     *
     * If MediaWiki places Contents inside the article again,
     * the observer moves Contents back to the sidebar.
     */
    function startGladiusObserve*() {
        if (
            obse*verRunning ||
            !document.body ||
            typeof MutationObserver === 'undefined'
        ) {
            return;
        }

        observerRunning = true;

        var observer = new MutationObserver(function () {
            window.clearTimeout(refreshTimer);

            refreshTimer = window.setTimeout(
                applyGladiusSidebar,
                100
            );
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    /*
     * Initial setup.
     */
    function initialiseGladiusSidebar() {
        applyGladiusSidebar();
        startGladiusObserver();

        /*
         * Additional checks for older skins and
         * delayed ResourceLoader components.
         */
        window.setTimeout(
            applyGladiusSidebar,
            250
        );

        window.setTimeout(
            applyGladiusSidebar,
            800
        );

        window.setTimeout(
            applyGladiusSidebar,
            1600
        );
    }

    /*
     * Start when the document is ready.
     */
    if (document.readyState === 'loading') {
        document.addEventListener(
            'DOMContentLoaded',
            initialiseGladiusSidebar
        );
    } else {
        initialiseGladiusSidebar();
    }

    /*
     * Run again after images and skin assets finish loading.
     */
    window.addEventListener(
        'load',
        applyGladiusSidebar
    );

    /*
     * Run after MediaWiki dynamically updates article content.
     */
    if (mw.hook) {
        mw.ho*k('wikipage.content').add(function*() {
            window.setTimeout*
                applyGladiusSideb*r,
                100
           *);
        });
    }

}(mediaWiki)*;