Utilisateur:Od1n/Rapport gadgets.js

Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
// pour chargement manuel sur un autre wiki :
// mw.loader.load('//fr.wikipedia.org/w/index.php?title=Utilisateur:Od1n/Rapport_gadgets.js&action=raw&ctype=text/javascript');

/* jshint esversion: 6, laxbreak: true */
/* globals mw, $ */

mw.loader.using(['mediawiki.util', 'user.options'], function () {

    $.get(mw.util.getUrl('MediaWiki:Gadgets-definition', {action: 'render'}), function (data) {

        const store = [];

        const $content = $(data); // avec le "action=render", on obtient directement l'élément "#mw-content-text > .mw-parser-output"
        $content.find('#toc').remove();

        $content.find('.mw-heading h2, .mw-heading h3, ul li').each(function () {

            const text = this.textContent;

            if (this.matches('.mw-heading h2, .mw-heading h3')) {
                store.push({
                    title: text,
                    gadgets: []
                });
            } else if (store.length > 0) {
                const gadget = {};
                gadget.name = text.match(/[^|[]+/)[0].trim();
                gadget.isHidden = /[[|] *hidden *[\]|]/.test(text);
                gadget.isDefault = /[[|] *default *[\]|]/.test(text);
                gadget.isEnabled = !!mw.user.options.get('gadget-'+gadget.name);

                store[store.length-1].gadgets.push(gadget);
            }
        });

        $(function ($) {

            const $table = $('<table class="wikitable"><tr>'
                + '<th>Section</th>'
                + '<th>Gadget</th>'
                + '<th>Default</th>'
                + '<th>Enabled</th>'
                + '</tr></table>');

            for (const section of store) {

                let nbRows = 0;

                for (const gadget of section.gadgets) {
                    if (gadget.isHidden) {
                        continue;
                    }
                    if (!gadget.isDefault && !gadget.isEnabled) {
                        continue;
                    }

                    ++nbRows;
                }

                if (nbRows === 0) {
                    continue;
                }

                let firstRow = true;

                for (const gadget of section.gadgets) {
                    if (gadget.isHidden) {
                        continue;
                    }
                    if (!gadget.isDefault && !gadget.isEnabled) {
                        continue;
                    }

                    const $tr = $('<tr></tr>');

                    if (firstRow) {
                        $tr.append('<th rowspan="'+nbRows+'">' + section.title + '</th>');
                        firstRow = false;
                    }

                    $tr.append('<td>' + gadget.name + '</td>'
                        + '<td>' + (gadget.isDefault ? 'default' : '') + '</td>'
                        + '<td>' + (gadget.isEnabled ? 'enabled' : '') + '</td>');

                    if (!gadget.isDefault && gadget.isEnabled) {
                        $tr.css('background', '#9BD398');
                    } else if (gadget.isDefault && !gadget.isEnabled) {
                        $tr.css('background', '#FDB6B8');
                    }

                    $table.append($tr);
                }
            }

            const $placeholder = $('#rapport-gadgets');

            if ($placeholder.length) {
                $placeholder.append($table);
            } else {
                $('#mw-content-text').prepend($table);
            }

        });
    });
});