User:Gommeh/Scripts/ShowBlockLog.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
(function () {
  'use strict';

  // Namespace numbers on MediaWiki:
  // 2 = User, 3 = User talk
  var ns = mw.config.get('wgNamespaceNumber');
  if (ns !== 2 && ns !== 3) return;

  // On User/User talk pages, wgTitle is the part after the namespace,
  // e.g. "Gommeh" or "Gommeh/Archives"
  var fullTitle = mw.config.get('wgTitle');
  if (!fullTitle) return;

  // If we're on a subpage, only use the base username (before first "/")
  var username = fullTitle.split('/')[0];
  if (!username) return;

  // Build the Special:Log URL for blocks against User:USERNAME
  var pageParam = 'User:' + username;

  var params = new URLSearchParams({
    type: 'block',
    user: '',
    page: pageParam,
    excludetempacct: '1',
    wpdate: '',
    tagfilter: '',
    'wpfilters[]': 'newusers',
    wpFormIdentifier: 'logeventslist',
  });

  var logUrl = mw.util.getUrl('Special:Log') + '?' + params.toString();

  mw.loader.using(['jquery', 'mediawiki.util']).done(function () {
    $(function () {
      var link = mw.util.addPortletLink(
        'p-tb',                 // toolbox
        logUrl,                 // target URL
        'Block log',            // link text
        'ca-user-blocklog',     // id
        'View this user’s block log'
      );

      if (link) {
        link.target = '_blank';
        link.rel = 'noopener';
      }
    });
  });
})();
// </nowiki>