User:The Voidwalker/histFilter.js

//

// While viewing the history of a page, one may now elect to only see revisions made by blocked users

if( mw.config.get('wgAction') === 'history' ) { mw.loader.using('mediawiki.util').then(function {		var button = document.createElement('input');		button.setAttribute('type', 'button');		button.setAttribute('value', 'Show blocked users only');		button.setAttribute('style', 'margin-left: 4px;');		button.setAttribute('id', 'history-blocked-only');		button.addEventListener('click', generateHistory);		$('.historysubmit.mw-history-compareselectedversions-button').after(button);		$('#history-blocked-only').click(function{ generateHistory; });	}); }

function getLimit { if( !/&limit=\d+/.test(window.location) ) { return 50; }	return /\d+/.exec(/&limit=\d+/.exec(window.location)[0])[0]; }

function generateHistory(c, lim) { var limit; if( !c ) { $('#pagehistory')[0].innerHTML = ''; //Poof, it's gone $('#pagehistory').append(' Getting relevant revisions '); limit = getLimit; } else { limit = lim; }	var pageID = mw.config.get('wgArticleId'); var query = { format: 'json', action: 'query', pageids: pageID, prop: 'revisions', rvprop: 'ids|timestamp|flags|parsedcomment|size|user', rvlimit: 500 };	if( c ) { query.continue = c.continue; query.rvcontinue = c.rvcontinue; }	$.getJSON( mw.util.wikiScript('api'), query).done(function(data){		var revisions = data.query.pages[pageID].revisions;		var users = [];		for(var i = 0; i < revisions.length; i++) {			var revuser = revisions[i].user;			if(revuser && !users.includes(revuser))				users.push(revuser);		}		var usersfull = [];		var count = 0;		while( users.length !== 0 ) {			usersfull[count] = users.slice(0,50);			users = users.slice(50);			count++;		}		var apireqs = 0;		var blocked = [];		for(i = 0; i < usersfull.length; i++) {			getBlockSet(usersfull[i]);			apireqs++;		}		function getBlockSet(userset) {			$.getJSON( mw.util.wikiScript('api'), { format: 'json', action: 'query', list: 'blocks', bkusers: userset.join('|'), bklimit: 50 // Only returns active blocks on bkusers, with max bkusers as 50 }).done(function(data){ var blocks = data.query.blocks; for(var i = 0; i < blocks.length; i++) { blocked.push(blocks[i].user); }				if( --apireqs === 0 ) // Last request showResults; });		}		function showResults {			var appended = 0;			for(var i = 0; i < revisions.length && appended < limit; i++) {				if(blocked.includes(revisions[i].user)) {					appendRev(revisions[i]);					appended++;				}			}			if(appended < limit && data.continue) {				generateHistory(data.continue, limit - appended);			} else {				$("#temp-while-fetching").remove;			}		}	}); function appendRev(revision) { var text = '(prev) ' + revision.timestamp + '&lrm; ' + revision.user + ' (talk | contribs)&lrm; ' + (revision.minor !== undefined ? 'm &lrm; ' : '') + ' . . (' + revision.size + ' bytes)' + (!revision.parsedcomment || revision.parsedcomment.length === 0 ?  : ' . . (' + revision.parsedcomment + ')''' ) + ''; $('#pagehistory').append(text); } } //