User:DeanHinnen/Restorer.js

/*** Restorer ***/

// Easily restore an older version of a page // Documentation at User:BrandonXLF/Restorer // By User:BrandonXLF

$(function{	function restore (user, revid) {		$.post( mw.config.get('wgScriptPath') + '/api.php', { action: 'edit', pageid: mw.config.get('wgArticleId'), undo: mw.config.get('wgCurRevisionId'), undoafter: revid, summary: 'Restored revision ' + revid + ' by ' + user + ' (Restorer)', token: mw.user.tokens.get('csrfToken'), format: 'json' })		.fail(function (a,b,c,d,e) { console.log(a,b,c,d,e); mw.notify('An error occured while restoring the revision.', {type:'error'}); })		.done(function (result) { console.log(result); if (result.error) { mw.notify(result.error.info, {type:'error'}); } else { mw.notify('Restored revision sucessfully.'); location.reload; }		});	}	function addLink (item) {		var revid = item.getAttribute('data-mw-revid'), user, links, ele, parent;		if (revid != mw.config.get('wgCurRevisionId')) {			user = item.getElementsByClassName('mw-userlink')[0].textContent.replace('User:','');			links = item.getElementsByClassName('mw-changeslist-links');			links = links[links.length - 1];			parent = document.createElement('span');			ele = document.createElement('a');			ele.addEventListener('click', function { restore(user, revid); });			ele.innerHTML = 'restore';			parent.appendChild(ele);			links.appendChild(parent);		}	}	(function { if (location.search.includes('action=history')) { var i, parents = document.querySelectorAll('li[data-mw-revid]'); console.log(parents); for (i = 0; i < parents.length; i++) { addLink(parents[i]); }		}	}); });