MediaWiki:Gadget-MinervaCustomizer.js

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.
// Change behavior of Mobile/Minerva: 1) Add "Request account", 2) Menu entry manipulation
mw.loader.using(['mediawiki.util'], function() {
  $(function() {
    if (mw.config.get('skin') !== 'minerva') return;

    // -- "Request account" menu entry injection
    if (!mw.user.isNamed()) {
      var waitForPersonalMenu = function(callback) {
        var $menu = $('#p-personal');
        if ($menu.length) {
          callback($menu);
        } else {
          setTimeout(function() { waitForPersonalMenu(callback); }, 100);
        }
      };
      waitForPersonalMenu(function($menu) {
        var regUrl = mw.util.getUrl('Special:UserLogin', { type: 'signup' });
        var regText = "Request account";
        var $regItem = $('<li>')
          .addClass('toggle-list-item menu__item--register')
          .append(
            $('<a>')
              .addClass('toggle-list-item__anchor')
              .attr('href', regUrl)
              .append($('<span class="minerva-icon minerva-icon--logIn"></span>').css('color', 'var(--general-link-color)'))
              .append($('<span class="toggle-list-item__label"></span>').text(regText).css('color', 'var(--general-link-color)'))
          );
        $menu.append($regItem);
      });
    }

    // -- Menu entries manipulation
    // Remove "Community portal"
    function pruneCommunityPortal() {
      var $nodes = $(
        '#mw-mf-page-left .menu__item--speechBubbles,' + // Class on anchor
        '#mw-mf-page-left [data-event-name="community-portal"],' + // Data attribute (if present)
        '#mw-mf-page-left a[href*="Community_portal"],' + // English URL
        '#mw-mf-page-left a[href*="Community%20portal"],' + // URL with space encoded
        '#mw-mf-page-left a[title="Community portal"]' // Exact title
      );
      if ($nodes.length) {
        $nodes.each(function() {
          var $el = $(this), $li = $el.closest('li');
          if ($li.length) $li.remove(); else $el.remove();
        });
      }
    }

    function applyOnceWhenDrawerReady() {
      var tries = 0, max = 60;
      (function tick() {
        if ($('#mw-mf-page-left').length) { pruneCommunityPortal(); return; }
        if (++tries < max) setTimeout(tick, 100);
      })();
    }

    // Init
    applyOnceWhenDrawerReady();
    $(document).on('click', '#mw-mf-main-menu-button', function() {
      setTimeout(pruneCommunityPortal, 0);
    });

    var drawer = document.getElementById('mw-mf-page-left');
    if (drawer) {
      var mo = new MutationObserver(function() { pruneCommunityPortal(); });
      mo.observe(drawer, { childList: true, subtree: true });
    }
  });
});