MediaWiki:Common.js

From GladiusRO Wiki
Revision as of 02:46, 2 August 2026 by Ikhwan (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/*
 * GladiusRO Wiki
 * Move Contents below the Tools section.
 */

(function () {
    'use strict';

    var attempts = 0;
    var maximumAttempts = 30;

    function moveGladiusTOC() {
        var toc = document.getElementById('toc');
        var tools = document.getElementById('p-tb');
        var existingContainer =
            document.getElementById('gladius-sidebar-toc');

        /*
         * Stop if Contents was already moved.
         */
        if (existingContainer) {
            return true;
        }

        /*
         * Wait until both the article TOC
         * and the Tools sidebar panel exist.
         */
        if (!toc || !tools || !tools.parentNode) {
            return false;
        }

        /*
         * Create a new container.
         */
        var tocContainer = document.createElement('div');

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

        /*
         * Move the original TOC out of the article.
         */
        tocContainer.appendChild(toc);

        /*
         * Insert Contents immediately after Tools.
         */
        tools.insertAdjacentElement(
            'afterend',
            tocContainer
        );

        return true;
    }

    function retryMoveGladiusTOC() {
        attempts++;

        if (moveGladiusTOC()) {
            return;
        }

        if (attempts < maximumAttempts) {
            window.setTimeout(
                retryMoveGladiusTOC,
                200
            );
        }
    }

    /*
     * Start after the complete page has loaded.
     */
    if (document.readyState === 'loading') {
        document.addEventListener(
            'DOMContentLoaded',
            retryMoveGladiusTOC
        );
    } else {
        retryMoveGladiusTOC();
    }

    /*
     * Try once more after all images and skin
     * components have loaded.
     */
    window.addEventListener(
        'load',
        retryMoveGladiusTOC
    );
}());