Jump to content

MediaWiki:Common.js: Difference between revisions

From Shark's Hypothetical Weather
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
mw.hook('wikipage.content').add(function() {
    if (mw.config.get('wgCanonicalSpecialPageName') === 'Search') {
        const searchResults = document.querySelectorAll('.mw-search-result-heading a');
        searchResults.forEach(function(link) {
            // Fetch the page linked
            fetch(link.href)
                .then(response => response.text())
                .then(html => {
                    const doc = new DOMParser().parseFromString(html, 'text/html');
                    const shortDescDiv = doc.querySelector('div.shortdescription');
                    if (shortDescDiv && shortDescDiv.textContent.trim() !== '') {
                        const desc = shortDescDiv.textContent.trim();
                        const resultContainer = link.closest('.mw-search-result');
                        if (resultContainer) {
                            const descElement = document.createElement('div');
                            descElement.className = 'search-shortdesc';
                            descElement.style.fontSize = '85%';
                            descElement.style.color = '#666';
                            descElement.style.marginTop = '0.2em';
                            descElement.textContent = desc;
                            resultContainer.appendChild(descElement);
                        }
                    }
                });
        });
    }
});
$(function() {
$(function() {
     var shortdescText = $('div.shortdescription').text();
     var shortdesc = $('div.shortdescription').text().trim();
     if (shortdescText.length > 0) {
 
         // Remove the existing meta description generated by MediaWiki
     if (shortdesc.length > 0) {
         // SHORTDESC exists: replace the old meta description
         $('meta[name="description"]').remove();
         $('meta[name="description"]').remove();


         // Create a new meta description element
         $('<meta>', {
         var meta = document.createElement('meta');
            name: 'description',
         meta.name = "description";
            content: shortdesc
        meta.content = shortdescText;
         }).appendTo('head');
        document.getElementsByTagName('head')[0].appendChild(meta);
    } else {
        // No SHORTDESC: delete any description tag
         $('meta[name="description"]').remove();
     }
     }
});
});

Latest revision as of 22:29, 25 April 2025

/* Any JavaScript here will be loaded for all users on every page load. */

mw.hook('wikipage.content').add(function() {
    if (mw.config.get('wgCanonicalSpecialPageName') === 'Search') {
        const searchResults = document.querySelectorAll('.mw-search-result-heading a');

        searchResults.forEach(function(link) {
            // Fetch the page linked
            fetch(link.href)
                .then(response => response.text())
                .then(html => {
                    const doc = new DOMParser().parseFromString(html, 'text/html');
                    const shortDescDiv = doc.querySelector('div.shortdescription');
                    if (shortDescDiv && shortDescDiv.textContent.trim() !== '') {
                        const desc = shortDescDiv.textContent.trim();
                        const resultContainer = link.closest('.mw-search-result');
                        if (resultContainer) {
                            const descElement = document.createElement('div');
                            descElement.className = 'search-shortdesc';
                            descElement.style.fontSize = '85%';
                            descElement.style.color = '#666';
                            descElement.style.marginTop = '0.2em';
                            descElement.textContent = desc;
                            resultContainer.appendChild(descElement);
                        }
                    }
                });
        });
    }
});

$(function() {
    var shortdesc = $('div.shortdescription').text().trim();

    if (shortdesc.length > 0) {
        // SHORTDESC exists: replace the old meta description
        $('meta[name="description"]').remove();

        $('<meta>', {
            name: 'description',
            content: shortdesc
        }).appendTo('head');
    } else {
        // No SHORTDESC: delete any description tag
        $('meta[name="description"]').remove();
    }
});