Utilisatrice:Meylody/common.js

De Poképédia

Note : après avoir publié vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou appuyez sur Ctrl + F5 ou Ctrl + R (⌘ + R sur un Mac).
  • Google Chrome : appuyez sur Ctrl + Maj + R (⌘ + Shift + R sur un Mac).
  • Internet Explorer / Edge : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl + F5.
  • Opera : appuyez sur Ctrl + F5.
$(document).ready(function() {
    // Vérifier si l'URL de la page actuelle correspond aux pages spécifiées
    const currentURL = window.location.href;
    const validURL = 'https://www.pokepedia.fr/Catégorie:';

    if (currentURL.includes(validURL)) {
        // Ajout de la barre de recherche en haut de la page
        var searchBar = '<div id="custom-search-bar" style="margin-bottom: 20px;">' +
                        '<input type="text" id="search-term" placeholder="Entrez un terme...">' +
                        '<button id="search-button">Cherche !</button>' +
                        '</div>';
        $('#content').prepend(searchBar);

        // Fonction de recherche et d'affichage
        $('#search-button').click(function() {
            var searchTerm = $('#search-term').val().toLowerCase();
            if (!searchTerm) {
                alert('Veuillez entrer un terme de recherche.');
                return;
            }

            // Fonction pour récupérer les fichiers MediaWiki via l'API
            function fetchFiles(term) {
                return $.ajax({
                    url: mw.util.wikiScript('api'),
                    data: {
                        action: 'query',
                        list: 'categorymembers',
                        cmtitle: currentURL.substring(currentUrl.lastIndexOf('/') + 1),
                        cmtype: 'file',
                        cmlimit: 'max',
                        format: 'json'
                    },
                    dataType: 'json'
                });
            }

            // Fonction pour filtrer et afficher les fichiers
            function displayFiles(files) {
                var galleryHtml = '<div class="mw-category-generated mw-category" style="margin-top: 20px;">';
                galleryHtml += '<div class="gallery mw-gallery-traditional" style="display: flex; flex-wrap: wrap;">';
                files.forEach(function(file) {
                    var fileName = file.title.toLowerCase();
                    if (fileName.includes('fichier:') && fileName.includes(searchTerm)) {
                        var fileUrl = mw.util.getUrl('Special:FilePath/' + file.title.replace('Fichier:', ''));
                        galleryHtml += '<div class="gallerybox" style="margin-right: 10px;">' +
                                       '<div class="thumb">' +
                                       '<div class="thumbinner" style="width: 180px;">' +
                                       '<a href="' + mw.util.getUrl(file.title) + '" class="image">' +
                                       '<img src="' + fileUrl + '" alt="' + file.title + '" width="180" height="180">' +
                                       '</a>' +
                                       '<div class="gallerytext">' +
                                       '<p class="galleryfilename galleryfilename-truncate">' + file.title.replace('Fichier:', '') + '</p>' +
                                       '</div>' +
                                       '</div>' +
                                       '</div>' +
                                       '</div>';
                    }
                });
                galleryHtml += '</div></div>';
                $('#mw-content-text').html(galleryHtml);
            }

            // Effacer les résultats précédents
            $('.mw-category-generated').remove();

            // Rechercher les fichiers et les afficher
            fetchFiles(searchTerm).done(function(data) {
                var files = data.query.categorymembers;
                displayFiles(files);
            }).fail(function() {
                alert('Une erreur est survenue lors de la recherche des fichiers.');
            });
        });
    }
});