Jump to content

MediaWiki:Common.js

From Adaris
Revision as of 03:30, 6 April 2025 by Oros (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.
$(document).ready(function() {
    $('.infoboxTable th').each(function() {
        if ($(this).text().trim() === "Image") {
            $(this).text(""); // Removes the text but keeps the <th> element
        }
    });
});

$(document).ready(function() {
    $('h2').each(function() {
        if ($(this).text().trim() === "Introduction") {
            $(this).remove(); // Removes the Introduction header
        }
    });
});

$(document).ready(function() {
    // Remove first empty paragraph (<p><br></p>) if it exists
    $('p').first().each(function() {
        if ($(this).html().trim() === "<br>") {
            $(this).remove();
        }
    });
});

mw.loader.using(['jquery'], function() {
    function processPopup($popup) {
        // 1. Update any <strong> title tags that are not already links.
        $popup.find('strong').each(function() {
            var titleText = $(this).text().trim();
            if (titleText.length > 0 && $(this).find('a').length === 0) {
                var link = $('<a>')
                    .attr('href', '/' + encodeURIComponent(titleText.replace(/ /g, '_')))
                    .attr('target', '_blank')
                    .text(titleText);
                $(this).html(link);
            }
        });

        // 2. Get the popup's HTML content.
        var htmlContent = $popup.html();

        // 3. Replace wiki-style link markup [PageName] with clickable links,
        //    skipping any that start with 'img:' so they can be handled separately.
        var newContent = htmlContent.replace(/\[([^\]]+)\]/g, function(match, p1) {
            if (p1.trim().toLowerCase().startsWith('img:')) {
                return match;
            }
            var pageName = p1.replace(/ /g, '_');
            return '<a href="/' + encodeURIComponent(pageName) + '" target="_blank">' + p1 + '</a>';
        });

        // 4. Replace custom image markup [img:...] with an actual image element.
        newContent = newContent.replace(/\[img:([^\]]+)\]/gi, function(match, p1) {
            var imageFile = p1.trim();
            var src;
            // If the markup is a full URL, use it as is.
            if (/^https?:\/\//.test(imageFile)) {
                src = imageFile;
            } else {
                src = '/images/' + encodeURIComponent(imageFile);
            }
            return '<img src="' + src + '" alt="' + imageFile + '" style="max-width:100%; display:block; margin-top:5px;">';
        });

        // 5. Update the popup content.
        $popup.html(newContent);
    }

    // Use a MutationObserver to detect when new popup content is added.
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.addedNodes) {
                $(mutation.addedNodes).each(function() {
                    var $node = $(this);
                    if ($node.hasClass('leaflet-popup-content')) {
                        processPopup($node);
                    } else {
                        // Also check within any child nodes.
                        $node.find('.leaflet-popup-content').each(function() {
                            processPopup($(this));
                        });
                    }
                });
            }
        });
    });

    // Observe the document body for new nodes (popups are added dynamically).
    observer.observe(document.body, { childList: true, subtree: true });
});