Jump to content

User:FunIsOptional/RestoreButton.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by FunIsOptional (talk | contribs) at 05:37, 8 August 2022 (RestoreButton no longer shows the first edit as restorable). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
$(document).ready( function () {
	if (mw.config.get('wgAction') === 'history') {
   		var links = document.getElementsByClassName('mw-changeslist-links');
   		for (var i = 3; i < links.length; i++) {
   			// This stops anything other than the end links (undo & thank) from having a restore button, as the others have other classes
   			if (links[i].className != 'mw-changeslist-links') {
   				continue;
   			}
   			
			var restoreSpan = document.createElement('span');
			var restoreA = document.createElement('a');

			var revisionId = links[i].parentNode.getAttribute('data-mw-revid');
			var pageTitle = mw.config.get('wgPageName');
			var revisionAuthor = links[i].parentNode.querySelector('.history-user').firstChild.firstChild.textContent;
			
			var editSummary = "Restored revision " + revisionId + " by [[User:" + revisionAuthor + "|" + revisionAuthor + "]] ([[User:FunIsOptional/RestoreButton|RestoreButton]])";

			restoreA.setAttribute('href', '//en.wikipedia.org/w/index.php?title=' + pageTitle + '&oldid=' + revisionId + '&action=edit&summary=' + editSummary);
			restoreA.textContent = 'restore';

			restoreSpan.appendChild(restoreA);
       		links[i].appendChild(restoreSpan);
   		}
	}	
});