User:DannyS712/Pretty rights log.js

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.
// Install with:
// <code><nowiki>		{{subst:Iusc|User:DannyS712/Pretty righs log.js}}																	</nowiki></code>
// or with
// <code><nowiki>		importScript( 'User:DannyS712/Pretty righs log' ); // Backlink: [[User:DannyS712/Pretty righs log.js]] 				</nowiki></code> 
//
// If forking this script, please note my contributions / give me credit
//<nowiki>
$(function (){
mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () { 
    	if ( mw.config.get('wgCanonicalNamespace') === 'Special' && (mw.config.get('wgCanonicalSpecialPageName') === 'Log' || mw.config.get('wgCanonicalSpecialPageName') === 'Userrights') ){
    		$('[data-mw-logaction="rights/rights"]').each( function() {
				this.innerHTML = this.innerHTML.replace( /(changed group membership for <a.*?>.*?<\/a>) (.*?)( <span class="comment">|\s+\(<a class="mw-thanks-thank-link")/, replacer );
			});
    	}
    } );
} );
function replacer( match, p1, p2, p3, offset, string ){
	console.log( p2 );
	var np2 = filter( p2 );
	return( p1 + ': ' + np2 + p3 );
}
function filter( p2 ){
	var split = p2.split( " to " );
	from = split[0].replace( /^from /, '' ).replace( /, (until|\d)/g, ',_$1' ).replace( / and /, ', ');
	to = split[1].replace( /, (until|\d)/g, ',_$1' ).replace( / and /, ', ');
	
	var fromA = from.split( ', ' );
	var toA = to.split( ', ' );
	
	var fromAF = [];
	var toAF = [];
	
	for ( var iii = 0; iii < fromA.length; iii++ ){
		if ( toA.indexOf( fromA[iii] ) === -1 ){
			fromAF.push( fromA[iii] );
		}
	}
	for ( var jjj = 0; jjj < toA.length; jjj++ ){
		if ( fromA.indexOf( toA[jjj] ) === -1 ){
			toAF.push( toA[jjj] );
		}
	}
	var removed = fromAF.join( ', ' );
	if ( removed === '' ){
		removed = '(none)';
	}
	var added = toAF.join( ', ' );
	if ( added === '' ){
		added = '(none)';
	}
	var change = 'removed ' + removed + ', added ' + added;
	change = change.replace( /_/g, ' ' );
	
	console.log( split, from, to, fromA, toA, fromAF, toAF, removed, added, change );
	return change;
}
});
//</nowiki>