User:Enterprisey/live-reload.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Enterprisey/live-reload. |
// 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" ),
") ",
$( "<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" ) ) ); } );
}
} );
}