Jump to content

MediaWiki:Common.js

From Yusupov's House
Revision as of 19:02, 2 October 2025 by Mvuijlst (talk | contribs)

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.
/* Promote infobox image to a 10:7 hero above the title (touch devices only) */
mw.hook('wikipage.content').add(function ($content) {
  // Only on touch devices
  if (!window.matchMedia || !window.matchMedia('(pointer: coarse)').matches) return;

  // Avoid duplicates
  if (document.querySelector('.article-hero')) return;

  // First infobox image
  var $img = $content.find('table.infobox .infobox-image-cell img').first();
  if (!$img.length) return;

  // Page title (Vector 2022 still uses #firstHeading)
  var $heading = $('#firstHeading');
  if (!$heading.length) return;

  // Build hero container and clone the image
  var $hero = $('<div>', { 'class': 'article-hero', 'aria-hidden': 'true' });
  var $clone = $('<img>', {
    src: $img.attr('src'),
    srcset: $img.attr('srcset') || '',
    sizes: '100vw',
    alt: '',                         // decorative hero; real alt stays in infobox
    loading: 'eager',
    decoding: 'async',
    fetchpriority: 'high'
  });

  $hero.append($clone);
  $hero.insertBefore($heading);

  // Hide the original infobox image on touch devices (via CSS class)
  $img.closest('table.infobox').addClass('infobox--image-promoted');
});

/* Quick facts: collapse infobox under first paragraph on touch devices */
mw.hook('wikipage.content').add(function ($content) {
  // Only on touch devices
  if (!window.matchMedia || !window.matchMedia('(pointer: coarse)').matches) return;

  var $ibox = $content.find('table.infobox').first();
  if (!$ibox.length) return;

  // First real paragraph in article content
  var $firstP = $content
    .find('.mw-parser-output > p')
    .filter(function () { return $(this).text().trim().length > 0; })
    .first();
  if (!$firstP.length) return;

  // Build <details> wrapper
  var $details = $('<details>', { 'class': 'quickfacts', 'aria-label': 'Quick facts' });
  var $summary = $('<summary>', { 'class': 'quickfacts-summary', text: 'Quick facts' });

  $details.append($summary);

  // Mark infobox so CSS can remove float/width inside quickfacts
  $ibox.addClass('infobox--in-quickfacts');

  // Move the infobox inside the details (not cloning; avoids duplicate content for SR)
  $details.append($ibox);

  // Insert directly after the first paragraph
  $details.insertAfter($firstP);
});