User:Blue-Haired Lawyer/watchlist undo.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.
/* add undo links to my watchlist */
function transFormAddress(url) {
	var arr = url.split('?');
	var base = arr.shift();
	var rest = arr.join('?')
	var query = rest.split('&');

	var params = {};
	for(var h=0; h < query.length; h++) {
		var x = query[h].split('=');	

		params[x[0]] = x[1];
	}
	
	return base + '?title=' + params['title'] + '&action=edit&undoafter=' + params['oldid'] + '&undo=' + params['diff'];

}

function cycleThroughLinks() {
	var lis = document.querySelectorAll("UL.special LI");
	
	for(var i=0; i < lis.length; i++) {
		var url = lis[i].getElementsByTagName('A')[0].href.replace('&amp;', '&')
		
		var nlink = document.createElement('A');
		nlink.href = transFormAddress(url);	
		nlink.appendChild(document.createTextNode('undo'));
		
		var nspan = document.createElement('SPAN');
		nspan.appendChild(document.createTextNode(' ('));
		nspan.appendChild(nlink);
		nspan.appendChild(document.createTextNode(')'));
		
		lis[i].appendChild(nspan);
	}
}


if(location.pathname == '/wiki/Special:Watchlist') {
	cycleThroughLinks();
}