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 04:49, 5 May 2020 (new script!). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
if( mw.config.get( "wgAction" ) === "history" ) {
    $.when( mw.loader.using( [ "mediawiki.api", "mediawiki.util" ] ), $.ready ).then( function () {
        var link = mw.util.addPortletLink( "p-cactions", "#",
            "Live reload (1 min)", "pt-livereload-one-minute",
            "Live reload this page every minute" );
        if( !link ) return;
        link.addEventListener( "click", function () {
            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+?/, "" );
                        $( "#pagehistory" ).prepend( revs.map( function ( rev, index ) {
                            var bytesDiff = ( index + 1 ) < revs.length ? ( rev.size - revs[ index + 1 ].size ) : ( rev.size - parseInt( $( "#pagehistory li" ).first().find( ".history-size" ).text().match( /^(\d+|,)+/ )[0].replace( /,/g, "" ) ) );
                            return "<li data-mw-revid='" + rev.revid + "' class='live-reload-new'>(<a href='" + mw.util.getUrl( "Special:Diff/" + rev.revid ) + "'>prev</a>)&emsp;<a href='" + mw.util.getUrl( mw.config.get( "wgPageName" ), { oldid: rev.revid } ) + "'>" + rev.timestamp.replace( /[TZ]/, " " ) + "</a> <span class='history-user'>" + rev.user + "</span> <span class='history-size mw-diff-bytes'>" + rev.size + " bytes</span> <span class='mw-diff-bytes mw-plusminus-" + ( bytesDiff > 0 ? "pos" : ( bytesDiff < 0 ? "neg" : "null" ) ) + "'>" + bytesDiff + "</span> <span class='comment'>(" + rev.comment + ")</span></li>";
                        } ).join( "" ) );
                        var content = $( revs.map( function ( rev ) { return "li[data-mw-revid=" + rev.revid + "]"; } ).join( "," ) );
                        mw.hook( "wikipage.content" ).fire( content );
                    }
                } );
            }
            var intervalId = window.setInterval( go, 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" } )
            );
        } );
    } );
}