Jump to content

User:Bradv/Scripts/ExpandDiffs.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Bradv (talk | contribs) at 22:36, 6 September 2020 (.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
(function( $, mw ) {
    'use strict';
    
    if (mw.config.get('wgAction')==='history' || mw.config.get('wgCanonicalSpecialPageName')=='Contributions') {
        const app = {
            loading: false,
            styleSheet: mw.util.addCSS(`
                body.hideExpandedDiffs table.diff {
                    display:none
                }
                .diff-addedline, .diff-deletedline, .diff-context {
                    font-size: 0.9em;
                }
                .diff > tr.hidden {
                    display:none;
                }
            `),
            init: function () {
                mw.loader.using('mediawiki.util').then(function () {
                    var $link = $(mw.util.addPortletLink('p-views', '', 'Expand diffs', 'ca-expand', '', '', '#ca-history'))
                    .click(function(e) {
                        e.preventDefault();
                        $link.toggleClass('selected');
                        if (!app.loading) {
                            app.loading=true;
                            app.load();
                        } else {
                            $(document.body).toggleClass('hideExpandedDiffs');
                        }
                    })
                });
            },
            load: function () {
                var api = new mw.Api();
                var counter = 0;

                function getDiff($li) {
                    var revid = $li.attr('data-mw-revid');
                    api.get({
                        action: 'compare',
                        format: 'json',
                        fromrev: revid,
                        torelative: 'prev',
                        prop: 'diff'
                    }).done(function (response, data) {
                        var diff = data.responseJSON.compare['*'];
                        var tbl = $('<table>', {'class':'diff diff-contentalign-left diff-editfont-monospace'})
                            .append($('<colgroup><col class="diff-marker"/><col class="diff-content"/><col class="diff-marker"/><col class="diff-content"/></colgroup>'))
                            .append(diff)
                            .appendTo($li);
                        tbl.find('tr:has(td.diff-context), tr:has(td.diff-lineno)').addClass('hidden');
                        counter++;
                        var $next = $li.next();
                        if (counter<50 && $next) {
                            setTimeout(function() {getDiff($next)}, 100)
                        }
                    });


                }
                getDiff($('ul#pagehistory > li:first-of-type, ul.mw-contributions-list > li:first-of-type'));
            }
        }
        app.init();
    }
}(jQuery, mediaWiki ));