User:Weeklyd3/scripts/revert.js

(function {	const currentPageName = (mw.config.get("wgFormattedNamespaces")[mw.config.get("wgNamespaceNumber")].length ? (mw.config.get("wgFormattedNamespaces")[mw.config.get("wgNamespaceNumber")] + ":") : "")   + mw.config.get("wgTitle");    console.log(`Running on ${currentPageName}`);    globalThis.currentPageName = currentPageName; var apiURL; if (location.href.includes('/w')) apiURL = '/w/api.php'; else if (location.href.includes('/index.php')) apiURL = 'api.php'; globalThis.apiURL = apiURL; if (document.querySelector('table.diff')) {	// Probably a diff page	if (!document.querySelector('#mw-diff-ntitle1 strong a').textContent.startsWith('Latest revision')) return console.warn('Not looking at diff w/ latest version.');	const buttons = document.createElement('div');	buttons.classList.add('action-buttons');	const VANDAL = document.createElement('button');	VANDAL.textContent = 'Vandalism?';	VANDAL.addEventListener('click', function { roll('Vandalism', this); });	buttons.appendChild(VANDAL);	const TEST = document.createElement('button');	TEST.textContent = 'Test edit?';	TEST.addEventListener('click', function { roll('Test edits', this); });	buttons.appendChild(TEST);	const UNSOURCED = document.createElement('button');	UNSOURCED.textContent = 'Poorly sourced edit?';	UNSOURCED.addEventListener('click', function { roll('Not verifiable via reliable sources', this); });	buttons.appendChild(UNSOURCED);	const COPYVIO = document.createElement('button');	COPYVIO.textContent = 'Copyright violation?';	COPYVIO.addEventListener('click', function { this.parentNode.appendChild(document.createTextNode('Please be sure to ask for revision deletion later.')); roll('Copyright violation', this); });	buttons.appendChild(COPYVIO);	const custom = document.createElement('button');	custom.textContent = 'Something else';	custom.addEventListener('click', function { const reason = prompt(`Why are you rolling back this edit?\n[An empty reason or CANCEL aborts the rollback.]`); if (!reason) return; roll(reason, this); });	buttons.appendChild(custom);	const br = document.createElement('br');	buttons.appendChild(br);	document.querySelector('#mw-diff-ntitle1').after(buttons); } else {	console.warn('Not looking at diff page.'); } }); function disableAllActionButtons { const buttons = document.querySelectorAll('.action-buttons button'); for (var i = 0; i < buttons.length; i++) { buttons[i].disabled = 'disabled'; } } function enableAllActionButtons { const buttons = document.querySelectorAll('.action-buttons button'); for (var i = 0; i < buttons.length; i++) { buttons[i].disabled = ''; } } function roll(reason, t, defaultSummary = true) { t.disabled = 'disabled'; console.log(t.textContent); t.setAttribute('data-orig', t.textContent); t.textContent = 'Reverting (getting history of page)...'; disableAllActionButtons; fetch(`${apiURL}?action=query&format=json&prop=revisions&titles=${encodeURIComponent(currentPageName)}&rvprop=ids|user&rvlimit=1`) .then(function(js) {		return js.json;	}) .then(function(json) {		var page = json.query.pages[Object.keys(json.query.pages)[0]].revisions[0];		globalThis.p = page.revid;		globalThis.user = page.user;		t.textContent = `ID to revert: ${p}, getting token`;	}) .then(function {		return fetch(`${apiURL}?action=query&meta=tokens&format=json`, { credentials: 'include' });	})	.then(function(res) {		return res.json;	}) .then(function(json) {		globalThis.token = json.query.tokens.csrftoken;	}) .then(function {		var additional;		t.textContent = 'Reverting edit...';		if (defaultSummary) additional = encodeURIComponent(`Revert edits by ${user} (talk): `);		else additional = '';		const baudy = new FormData;		baudy.set('token', globalThis.token);	return fetch(`${apiURL}?action=edit&summary=${additional}${encodeURIComponent(reason + " (weeklyd3's revert script)")}&title=${currentPageName}&origin=${location.origin}&undo=${globalThis.p}&format=json`, { "credentials": 'include', 'method': 'POST', 'body': baudy });	})	.then(function(res) {		return res.json;	}) .then(function(js) {		console.log(js);		if (js.error) {			mw.notify(js.error.info, {title: `Aww man. Error:\n${js.error.code}`});			t.textContent = t.getAttribute('data-orig');			t.removeAttribute('data-orig');			enableAllActionButtons;			return;		}		t.textContent = 'Rollback completed, reloading page in a second...';		setTimeout(function { location.reload; }, 1000);	}); }