Jump to content

User:Enterprisey/live-reload.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Enterprisey (talk | contribs) at 01:06, 6 May 2020 (syntax error...). 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.
// vim: ts=4 sw=4 et ai
if( mw.config.get( "wgAction" ) === "history" ) {
    $.when( mw.loader.using( [ "mediawiki.api", "mediawiki.util" ] ), $.ready ).then( function () {
        function start( intervalInMinutes ) {
            var api = new mw.Api();
            function go() {
                api.get( {
                    action: "query",
                    prop: "revisions",
                    titles: mw.config.get( "wgPageName" ).replace( /_/g, " " ),
                    rvprop: "ids|timestamp|user|comment|size",
                    rvendid: parseInt( $( "#pagehistory li" ).get( 0 ).dataset.mwRevid ) + 1,
                    formatversion: "2",
                    rvlimit: 2
                } ).then( function ( data, jqXhr ) {
                    var revs = data.query.pages[0].revisions;
                    if( !revs ) {
                        return;
                    }
                    var i;
                    for( i = 0; i < revs.length; i++ ) {
                        if( $( "li[data-mw-revid=" + revs[i].revid + "]" ).length ) {
                            break;
                        }
                    }
                    revs = revs.slice( 0, i );
                    if( revs.length ) {
                        window.liveReloadCount += revs.length;
                        document.title = "(" + window.liveReloadCount + ") " + document.title.replace( /^\(.+?\)\s+?/, "" );

                        var previousTopEntry = $( "#pagehistory li" ).first();
                        $( "#pagehistory" ).prepend( revs.map( function ( rev, index ) {
                            var previousRevSize = ( index + 1 ) < revs.length ? revs[ index + 1 ].size : parseInt( previousTopEntry.find( ".history-size" ).text().match( /^(\d+|,)+/ )[0].replace( /,/g, "" ) );
                            var bytesDiff = rev.size - previousRevSize;
                            bytesDiff = ( ( bytesDiff > 0 ) ? "+" : "" ) + bytesDiff;
                            var previousRevid = ( index + 1 ) < revs.length ? revs[ index + 1 ].revid : previousTopEntry.get( 0 ).dataset.mwRevid;
                            return $( "<li>", { "data-mw-revid": rev.revid, "class": "live-reload-new" } ).append(
                                "(",
                                $( "<a>" )
                                    .attr( "href", mw.util.getUrl( "Special:Diff/" + rev.revid ) )
                                    .text( "prev" ),
                                ")&emsp;",
                                $( "<a>" )
                                    .attr( "href", mw.util.getUrl( mw.config.get( "wgPageName" ), { oldid: rev.revid } ) )
                                    .text( rev.timestamp.replace( /[TZ]/g, " " ) ),
                                $( "<span>", { "class": "history-user" } )
                                    .append( $( "<a>", { "class": "mw-userlink userlink" + rev.anon ? " mw-anonuserlink" : "" } )
                                        .attr( "href", mw.util.getUrl( rev.anon ? ( "Special:Contributions/" + rev.user ) : ( "User:" + rev.user ) ) )
                                        .text( rev.user ) ),
                                " ",
                                $( "<span>", { "class": "history-size mw-diff-bytes" } ).text( rev.size + " bytes" ),
                                " ",
                                $( "<span>", {
                                    "class": "mw-diff-bytes mw-plusminus-" + ( bytesDiff > 0 ? "pos" : ( bytesDiff < 0 ? "neg" : "null" ) )
                                } ).text( bytesDiff ),
                                " ",
                                $( "<span>", { "class": "comment" } ).text( "(" + rev.comment + ")" ),
                                " ",
                                $( "<span>", { "class": "mw-changeslist-links" } ).append(
                                    $( "<span>" ).append(
                                        $( "<span>", { "class": "mw-history-undo" } ).append(
                                            $( "<a>" )
                                                .attr( "href", mw.util.getUrl( mw.config.get( "wgPageName" ), {
                                                    action: "edit",
                                                    undo: rev.revid,
                                                    undoafter: previousRevid
                                                } ) )
                                                .text( "undo" ) ) ) ) 
                            );
                        } ) );

                        var content = $( revs.map( function ( rev ) { return "li[data-mw-revid='" + rev.revid + "']"; } ).join( "," ) );
                        mw.hook( "wikipage.content" ).fire( content );

                        // Calculate the number of consecutive edits made by the most recent editor
                        var numConsecutive = 1;
                        var firstEls = $( "#pagehistory li:lt(10)" );
                        var firstUser = firstEls[0].querySelector( ".mw-userlink" ).textContent;
                        for( var i = 1; i < 12; i++ ) {
                            if( firstEls[i] && firstEls[i].querySelector( ".mw-userlink" ).textContent === firstUser ) {
                                numConsecutive++;
                            } else {
                                break;
                            }
                        }
                        var consecutive = numConsecutive > 10 ? "more than 10" : numConsecutive;

                        // Move the rollback link to the first edit
                        var token = decodeURIComponent( $( ".mw-rollback-link a" ).attr( "href" ).match( /token=(.+)$/ )[1] );
                        $( ".mw-rollback-link" ).parent().remove();
                        $( "#pagehistory li" ).first().find( ".mw-changeslist-links" ).prepend(
                            $( "<span>" ).append(
                                $( "<span>", { "class": "mw-rollback-link" } ).append(
                                    $( "<a>" )
                                        .text( "rollback: " + consecutive + " edit" + ( numConsecutive === 1 ? "" : "s" ) )
                                        .attr( "href", mw.util.getUrl( mw.config.get( "wgPageName" ), {
                                            action: "rollback",
                                            from: firstUser,
                                            token: token
                                        } ) ) ) ) );
                    }
                } );
            }

            var intervalId = window.setInterval( go, intervalInMinutes * 60 * 1000 );
            window.liveReloadCount = 0;
            mw.util.addCSS( ".live-reload-new { background-color: #cfc; }" );
            $( "#mw-history-compare" ).before(
                $( "<div>" ).append(
                    $( "<button>" )
                        .addClass( "mw-ui-button" )
                        .text( "Reset live count" )
                        .click( function () {
                            window.liveReloadCount = 0;
                            document.title = document.title.replace( /^\(.+?\)\s+?/, "" );
                            $( ".live-reload-new" ).removeClass( "live-reload-new" );
                        } ),
                    " ",
                    $( "<button>" )
                        .addClass( "mw-ui-button" )
                        .text( "Disable live count" )
                        .click( function () {
                            window.clearInterval( intervalId );
                            $( this ).parent().remove();
                        } )
                )
                    .css( { "margin": "0.5em 0" } )
            );
        }

        var link = mw.util.addPortletLink( "p-cactions", "#",
            "Live reload (1 min)", "pt-livereload-one-minute",
            "live reload this page every minute" );
        if( link ) {
            link.addEventListener( "click", function () { start( 1 ); } );
        }

        var link2 = mw.util.addPortletLink( "p-cactions", "#",
            "Live reload (custom)", "pt-livereload-custom",
            "live reload this page every X minutes" );
        if( link2 ) {
            link2.addEventListener( "click", function () { start( parseFloat( window.prompt( "Interval, in minutes" ) ) ); } );
        }
    } );
}