User:Bradv/endlesscontribs.js

From Wikipedia, the free encyclopedia
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('wgCanonicalSpecialPageName')==='Contributions') {
        mw.loader.using("mediawiki.util").then(function () {
            const css = mw.util.addCSS(`
                body.endlesscontribs .mw-pager-navigation-bar:not(:first-of-type) {
                    display: none;
                }
                #endlesscontribs {
                    margin: 1em;
                }
            `)

            var nexturl = $('a[rel="next"]').attr('href');
            if (nexturl) {
                var loading = false;

                const button = $('<span>', {
                    'id': 'endlesscontribs',
                    'class': 'mw-ui-button mw-ui-progressive'})
                .text('Load more')
                .insertAfter($('.mw-pager-body'))
                .click(function () {
                    $('body').addClass('endlesscontribs');
                    if (!loading) {
                        loading = true;
                        $.get(nexturl, function(data) {
                            var $html = $(data);
                            const $ul = $('.mw-contributions-list');
                            const $newul = $html.find('.mw-contributions-list > li');
                            $newul.each(function (index, element) {
                                $ul.append($(element));
                            })

                            nexturl = $html.find('a[rel="next"]').attr('href');

                            if (!nexturl) {
                                button.remove();
                            }
                            loading = false;
                        })
                    }
                });                
            }
        });
    }
}(jQuery, mediaWiki ));