User:Mr Pie 5/global.js

mw.loader.load('//internal-soap.fandom.com/index.php?title=User:Mr_Pie_5/scripts-ucp.js&action=raw&ctype=text/javascript');

// Turn the page title into a link to the page { const title = document.getElementById("firstHeading"); // MediaWiki doesn't support backticks so piecemeal this :/ link = document.createElement('a'); link.href = window.location.pathname; link.style.color = "inherit"; link.textContent = title.textContent; title.textContent = ""; title.append(link); }

// Creates links to automatically revert a page to a specific revision with summary mw.loader.using('mediawiki.api').then(function {	const api = new mw.Api;	function revert(id){		// get the information about the desired revision		api.get({ "action": "query", "format": "json", "prop": "revisions", "revids": id, "formatversion": "2", "rvprop": "timestamp|user|content" }).then(function(data) { // parse and format the desired revision const revision = data.query.pages[0].revisions[0]; const revisionLink = `${id}`; const user = (revision.anon) ? `${revision.user}` : `${revision.user}`; const date = new Date(revision.timestamp); const timestamp = `${date.getUTCHours}:${date.getUTCMinutes}, ${date.getUTCDate} ${date.toLocaleString('en-us', {month:'long', timeZone:'UTC'})} ${date.getUTCFullYear} (UTC)`;

// post the edit reverting the page to the desired revision api.post({				"action": "edit",				"format": "json",				"pageid": data.query.pages[0].pageid,				"text": revision.content,				"summary": `Reverted to revision ${revisionLink} by ${user} at ${timestamp}.`,				"formatversion": "2",				"token": mw.user.tokens.get('csrfToken'),			}).then(function(data){				if(data.edit.result == "Success"){					location.reload;				}				else {					console.log("Something went wrong. Response follows:");					console.log(data);				}			}); });	}

function appendRevertLink(element, id){ let revertLink = document.createElement('a'); revertLink.textContent = 'revert'; revertLink.setAttribute('href', 'javascript:;'); revertLink.addEventListener('click', function{revert(id)});

let revertContainer = document.createElement('span'); revertContainer.classList.add('pie-revert'); revertContainer.append(' (');		revertContainer.append(revertLink)		revertContainer.append(')');

element.append(revertContainer); }

// Adds the link on history pages if(document.getElementById('pagehistory')){ for(element of document.getElementById('pagehistory').children) { const id = element.getAttribute('data-mw-revid');

appendRevertLink(element, id); }	}

// Adds the link on diff pages if(document.getElementsByClassName('diff-title')[0]){ for(element of document.getElementsByClassName('diff-title')[0].children) { const id = element.querySelector('a[href*="oldid="]').href.match(/oldid=(\d+)/)[1]; appendRevertLink(element.getElementsByTagName('strong')[0], id); }	}

// Adds the link on revision pages if(document.getElementsByClassName('mw-revision')[0]){ const element = document.getElementsByClassName('mw-revision')[0]; const id = element.querySelector('a[href*="oldid="]').href.match(/oldid=(\d+)/)[1]; appendRevertLink(element, id); }

});