Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Line 24: Line 24:
         var link = e.target.closest('a');
         var link = e.target.closest('a');
         if (!link) return;
         if (!link) return;
        if (!link.closest('.citizen-typeahead')) return;
         var href = link.getAttribute('href');
         var href = link.getAttribute('href');
         if (!href) return;
         if (!href) return;


         // Check if this is a redirect page link
         // Get the title from the link's href, not the text content
         var title = link.textContent.trim();
         var hrefTitle = decodeURIComponent(href.replace(/^\//, ''));
        var pageTitle = href.replace(/^\//, '').replace(/_/g, ' ');
       
        // Get the visible title from the dedicated title element
        var titleEl = link.querySelector('.citizen-typeahead-list-item-title, [class*="title"]');
        var displayTitle = titleEl ? titleEl.textContent.trim() : null;


        // If the link text doesn't match where it's going, it's probably a resolved redirect
         if (displayTitle && displayTitle !== hrefTitle) {
         if (title !== pageTitle && link.closest('.citizen-typeahead')) {
             e.preventDefault();
             e.preventDefault();
            // Navigate using the redirect so our observer can catch it
             window.location.href = '/index.php?title=' + encodeURIComponent(displayTitle) + '&redirect=yes';
             window.location.href = '/index.php?title=' + encodeURIComponent(title) + '&redirect=yes';
         }
         }
     }, true);
     }, true);

Revision as of 21:41, 3 April 2026

/* Any JavaScript here will be loaded for all users on every page load. */
$(function() {
    $('[accesskey="t"]').removeAttr('accesskey');
    $('[accesskey="n"]').removeAttr('accesskey');
    $('#pt-mytalk').remove();
    $('#ca-talk').remove();
    $('#ca-addsection').remove();
});

$(function() {
    // Handle redirects with tab switching
    var $redirect = $('span.mw-redirectedfrom a');
    if ($redirect.length) {
        waitAndSwitchTab($redirect.text().trim());
    }

    // Handle hash-based tab switching
    if (window.location.hash && window.location.hash.indexOf('tabber-tabpanel') !== -1) {
        waitAndSwitchTab(null, window.location.hash.substring(1));
    }

    // Intercept search result clicks
    document.addEventListener('click', function(e) {
        var link = e.target.closest('a');
        if (!link) return;
        if (!link.closest('.citizen-typeahead')) return;

        var href = link.getAttribute('href');
        if (!href) return;

        // Get the title from the link's href, not the text content
        var hrefTitle = decodeURIComponent(href.replace(/^\//, ''));
        
        // Get the visible title from the dedicated title element
        var titleEl = link.querySelector('.citizen-typeahead-list-item-title, [class*="title"]');
        var displayTitle = titleEl ? titleEl.textContent.trim() : null;

        if (displayTitle && displayTitle !== hrefTitle) {
            e.preventDefault();
            window.location.href = '/index.php?title=' + encodeURIComponent(displayTitle) + '&redirect=yes';
        }
    }, true);

    function waitAndSwitchTab(from, panelId) {
        var observer = new MutationObserver(function(mutations, obs) {
            var panels = document.querySelectorAll('[role="tabpanel"]');
            if (panels.length > 0) {
                if (panelId) {
                    var tab = document.querySelector('[aria-controls="' + panelId + '"]');
                    if (tab) tab.click();
                } else if (from) {
                    panels.forEach(function(panel) {
                        if (panel.textContent.indexOf(from) !== -1) {
                            var id = panel.getAttribute('id');
                            var tab = document.querySelector('[aria-controls="' + id + '"]');
                            if (tab) tab.click();
                        }
                    });
                }
                obs.disconnect();
            }
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }
});