User:Silikonz/or.js

mw.loader.using(["mediawiki.util", "mediawiki.notification"], => {

function pause(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }

function visibleErr(ttx, body) { mw.notify(body, { tag: "error", title: ttx, type: "error" }); throw new Error(`${ttx}: ${body}`); }

function mainN(ttx, body) { mw.notify(body, { tag: "status", title: ttx }); return true; }

function getToken(callback, passthrough) { $.ajax({			type: "GET",			url: "/w/api.php?action=query&meta=tokens&format=json",

success: function(result) { callback(result["query"]["tokens"]["csrftoken"], passthrough); },			error: function(result) { visibleErr("Request error", "Error retrieving token"); },			dataType: "json" });	}

function submitMove(token, mData) { var details = { "wpOldTitle": mData["from"], "wpNewTitle": mData["to"], "wpReasonList": "other", "wpReason": mData["reason"], "wpDeleteAndMove": "1", "wpMove": "Move+page", "wpEditToken": token, };

var formBody = []; for (var property in details) { var encodedKey = encodeURIComponent(property); var encodedValue = encodeURIComponent(details[property]); formBody.push(encodedKey + "=" + encodedValue); }		formBody = formBody.join("&");

fetch("/w/index.php?title=Special:MovePage&action=submit", {			method: "POST",			headers: {				"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"			},			body: formBody,		}).then((response) => {			if (response.ok) {				return response;			} else {				visibleErr("Request error", "Invalid network status");			}		}); }

function movePage(params) { return getToken(submitMove, params); }

function rmRedir(page, reason) { var theSponge = "User:Silikonz/sponge";

//step 1 mainN("Overwrite status", "Performing redirect overwrite step 1..."); movePage({			"from": theSponge,			"to": page,			"reason": reason+" (step 1)",		});

//step 2 pause(2000).then( => { 			mainN("Overwrite status", "Performing redirect overwrite step 2...");			new mw.Api.postWithToken( "csrf", { action: "move", from: page, to: theSponge, reason: reason+" (step 2)",

movetalk: "0", noredirect: "1", format: "json", } ).done( function ( data ) { mw.notify("Finished performing overwrite actions. Reloading...", { tag: "status", title: "Overwrite finished", type: "success" }); } );			pause(1000).then( => { location.reload; });		});		return true; }

var portletLink = mw.util.addPortletLink("p-cactions", "#", "Overwrite",		"ca-overredir", "Overwrite a single-revision redirect"); $( portletLink ).click(function(e) {		e.preventDefault;		//set		var toOver = mw.config.get("wgPageName");

var summary = prompt("Enter a summary for overwriting:"); if (summary === null) { visibleErr("Overwrite cancelled", "No reason entered."); }		return (confirm(`Warning!\n\nYou are about to overwrite the following redirect:\n${toOver}\nwith this summary specified:\n${summary}\n\nPlease confirm overwriting is possible before continuing. Proceed?`) ? rmRedir(toOver, summary) : visibleErr("Overwrite cancelled", "Confirmation aborted.")); });

});