User:Danski454/undo-move.js

From Wikipedia, the free encyclopedia
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.
( function( mw, $ ) {
	var re = /<abbr class="minoredit".+<span.+class="mw-plusminus-null mw-diff-bytes".*>\D*0\D*<\/span>.+<span class="comment">\(.+ moved page <a.+>(.+?)<\/a> to .+/;
	function convertLinks(){
		var curPage = encodeURIComponent(mw.config.get('wgPageName'));
		$("#pagehistory li").each(function(index){
			var match = $(this).html().match(re);
			if (match === null) {
				return true;//not a move
			}
			var oldPage = match[1].replace(/ /g, "_");
			oldPage = encodeURIComponent(oldPage);
			var url = "https://en.wikipedia.org/w/index.php?title=Special:MovePage&wpOldTitle=" + curPage + "&wpNewTitle=" + oldPage + "&wpReason=revert";
			$(this).find(".mw-history-undo a").attr('href', url);
		});
	}
	
	$(document).ready(function(){
		if (mw.config.get('wgAction') === 'history') {
			convertLinks();
		}
	});
	
} )( mediaWiki, jQuery );