User:Awesome Aasim/rollbacknoleave.js

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.
//<div class="mw-spinner mw-spinner-small mw-spinner-inline" title="..." id="mw-spinner-rollback-spinner"><div class="mw-spinner-container"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>
if (!RBNL) {
	var RBNL = {};
	$(document).ready(function() {
		$(".mw-rollback-link").click(function(e) {
			e.preventDefault();
			var link = $(this).find("a").attr("href");
			//stack overflow: https://stackoverflow.com/questions/25203124/how-to-get-base-url-with-jquery-or-javascript
			var getUrl = window.location;
			var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
			var urlparams = (new URL(baseUrl + link)).searchParams;
			var rollbackuser = urlparams.get('from');
			var rollbacktoken = urlparams.get('token');
			var rollbacktitle = urlparams.get('title');
			$(this).html('<div class="mw-spinner mw-spinner-small mw-spinner-inline" title="..."><div class="mw-spinner-container"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>');
			var that = this;
			$.post(mw.config.get("wgScriptPath") + "/api.php", {
				"action": "rollback",
				"user": rollbackuser,
				"token": rollbacktoken,
				"title": rollbacktitle,
				"format": "json"
			}).done(function(result) {
				console.log(result);
				if (result.error) {
					location.href = link;
				} else {
					$(that).html("Rollback complete");
					$(that).off("click");
				}
			}).fail(function(result) {
				location.href = link;
			});
		});
	});
}