Jump to content

User:Novem Linguae/Scripts/UserRightsDiff.js: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
debug
debug
Line 46: Line 46:
to = permStringToArray(matches[2]);
to = permStringToArray(matches[2]);
} catch(err) {
} catch(err) {
console.log(err);
throw new Error("UserRightsDiff.js parsing error. Input text was: " + $(this).text());
throw new Error("UserRightsDiff.js parsing error. Input text was: " + $(this).text());
}
}

Revision as of 02:36, 1 December 2021

// <nowiki>

/*

A typical user rights log entry might look like this:

	11:29, August 24, 2021 ExampleUser1 talk contribs changed group membership for ExampleUser2 from edit filter helper, autopatrolled, extended confirmed user, page mover, new page reviewer, pending changes reviewer, rollbacker and template editor to autopatrolled, extended confirmed user, pending changes reviewer and rollbacker (inactive 1+ years. should you return and require access again please see WP:PERM) (thank)

What the heck perms were removed? Hard to tell right? This user script adds a "DIFF" of the perms that were added or removed, on its own line, and highlights it green for added, yellow for removed.

	[ADDED template editor] [REMOVED edit filter helper, patroller]

This script works both in Special:UserRights, and when clicking "rights" in the user script User:BradV/Scripts/SuperLinks.js

*/

$(function() {
	function permStringToArray(string) {
		string = string.replace(/^(.*) and (.*?$)/, '$1, $2');
		if ( string === '(none)') {
			return [];
		}
		let array = string.split(', ').map(str => str.trim());
		return array;
	}

	function permArrayToString(array) {
		array = array.join(', ');
		return array;
	}

	/** Delete: (temporary...) (thank) (change visibility) (reason) (reason (nested)) (
		Keep: (none) */
	function deleteAllParenthesesExceptNone(text) {
		text = text.replace(/(?!\(none\))\(.*?\)\)?\)?\)?/gs, '');
		return text;
	}

	function checkLine() {
		let text = $(this).text();
		let from, to;
		try {
			text = deleteReasonAndThanks(text);
			let matches = / from (.*?) to (.*?)(?: \(.*)?$/.exec(text);
			from = permStringToArray(matches[1]);
			to = permStringToArray(matches[2]);
		} catch(err) {
			console.log(err);
			throw new Error("UserRightsDiff.js parsing error. Input text was: " + $(this).text());
		}
		let added = to.filter(x => !from.includes(x));
		let removed = from.filter(x => !to.includes(x));
		added = added.length > 0 ? '[ADDED: ' + permArrayToString(added) + ']' : '';
		removed = removed.length > 0 ? '[REMOVED: ' + permArrayToString(removed) + ']' : '';
		$(this).append(`<br /><span style="background-color:lawngreen">${added}</span> <span style="background-color:yellow">${removed}</span>`);
	}

	function checkLog() {
		$('body').off('DOMNodeInserted'); // prevent infinite loop
		$('.mw-logevent-loglines .mw-logline-rights').each( checkLine );
		$('body').on('DOMNodeInserted', '.mw-logevent-loglines', checkLog);;
	};

	// Use DOMNodeInserted to detect User:BradV/Scripts/SuperLinks.js
	$('body').on('DOMNodeInserted', '.mw-logevent-loglines', checkLog);
	
	// Use a regular check to detect if this page is Special:UserRights
	checkLog();
});

// </nowiki>