User:Endilyn/common.js

function loadScripts { "use strict"; var wait = wait || function(ms) { return new Promise(function(resolve) { setTimeout(resolve, ms); }); };	function apiWithEtiquette(api,apiFunc) { var apiFuncArgs = new Array(arguments.length-2); for (var i = 2; i < arguments.length; ++i) { apiFuncArgs[i-2] = arguments[i]; }		function f { var thisApi = api || new mw.Api; return (typeof apiFunc === "function" ? apiFunc : thisApi[apiFunc]).apply(thisApi, apiFuncArgs).catch( function(errorcode,data,_,jqXHR) {				if (errorcode === "maxlag" || errorcode === "ratelimited") {					return wait(parseInt(jqXHR.getResponseHeader("Retry-After")||5)*1000).then(f);				} else {					return jQuery.Deferred.rejectWith(this, arguments).promise;				}			} ); }		return f; } //Script to patrol the new revision plus all intermediate revisions on the edit diff screen, as opposed to clicking "Mark as patrolled" and patrolling only the new revision var patrolButton = document.getElementsByClassName("patrollink")[0]; if (patrolButton) { var multipatrol = function { var api = new mw.Api; return apiWithEtiquette(api,"get",{			action: 'query',			prop: 'revisions',			pageids: mw.config.get('wgArticleId'),			rvprop: 'ids',			rvstartid: mw.config.get('wgDiffNewId')||mw.config.get("wgCurRevisionId"),			rvendid: mw.config.get('wgDiffOldId')||mw.config.get("wgCurRevisionId"),			rvlimit: "max",			formatversion: '2',			maxlag: 5,			format: 'json'		}).then(function (data) {			var revisions = data.query.pages[0].revisions;			var promises = [];			for (var i = 0; i < revisions.length-1; i++) {				promises.push(apiWithEtiquette(api, api.postWithToken, 'patrol', {					formatversion: 2,					action: 'patrol',					maxlag: 5,					revid: revisions[i].revid				}));			}			return Promise.all(promises);		}); };

var newPatrolButton = document.createElement("button"); newPatrolButton.type = "button"; newPatrolButton.innerText = "Patrol this + intermediate edits"; patrolButton.parentElement.appendChild(document.createElement("br")); patrolButton.parentElement.appendChild(newPatrolButton); newPatrolButton.addEventListener("click", function {    	newPatrolButton.disabled = true;		multipatrol.then(function { patrolButton.remove; newPatrolButton.remove; window.close; // Only works if opened through a script, for use with rc new tab links });   });	$( window ).keypress(function(e) {		if (e.key === "p") {			$( newPatrolButton ).click;		}	}); }

//Updates from parameter in url for JS RecentChanges setTimeout(function {       var liveUpdateButton = document.getElementsByClassName("mw-rcfilters-ui-filterWrapperWidget-bottom")[0];        if (liveUpdateButton) {            var fromUpdateButton = document.createElement("button");            fromUpdateButton.type = "button";            fromUpdateButton.innerText = "Update from parameter";            fromUpdateButton.addEventListener("click", function  { try { var z = mw.Uri; z.extend({from: parseInt(document.getElementsByClassName("mw-changeslist-line")[0].attributes["data-mw-ts"].value)+1}); history.pushState(null, null, z.toString); document.getElementsByClassName("mw-changeslist")[0].innerHTML = null; } catch(e) { }           });            liveUpdateButton.parentElement.appendChild(fromUpdateButton);        }	}, 10000);

//Render html output in the lua scribunto console (function {	function defer {		var dependenciesLoaded = false;		try {			dependenciesLoaded = 			jQuery			&& (document.getElementsByClassName("mw-scribunto-console-fieldset").length === 1)			;		} catch (e) {}		if (dependenciesLoaded) {			var luaConsoleRenderLastButton = document.createElement("button");			luaConsoleRenderLastButton.type = "button";			luaConsoleRenderLastButton.innerText = "Render last result";			luaConsoleRenderLastButton.addEventListener("click", function { apiWithEtiquette(new mw.Api,"post",{					"action": "parse",					"format": "json",					"text": $(".mw-scribunto-normalOutput, .mw-scribunto-print").last.text,					"prop": "text",					"disablelimitreport": 1,					"disableeditsection": 1,					"preview": 1,					"contentmodel": "wikitext"				}).then(function (data) {					$("#mw-scribunto-output").append( data.parse.text["*"] );				}); });			$( "#mw-scribunto-console .mw-scribunto-console-fieldset").append( $(" ").append(luaConsoleRenderLastButton) );		} else {			setTimeout(defer, 1000);		}	}	defer; });

// https://en.wikipedia.org/wiki/User:Js/ajaxPreview, used so I can have both standard and ajax preview mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Js/ajaxPreview.js&action=raw&ctype=text/javascript' ); }

if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', loadScripts); } else { loadScripts; }

// Adds a button for cargo queries // https://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_customization var customizeToolbar = function { $('#wpTextbox1').wikiEditor('addToToolbar', {		section: 'advanced',		group: 'insert',		tools: {			"cargoquery": {				label: 'Cargo query',				labelMsg:'cargoquery',				type: 'button',				icon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAYCAMAAADJYP15AAABI1BMVEUAAAA/Klg+KFo+KFo/KVs+KFo+KFpPOmpnU4BSPW1/a5Z/a5VCLF1WQXB+apV6ZpGFcZtKNGVcR3ZgS3luWoZ9aZTy4v8/KVuLd6DWxeU+KFqdirGejLKUgamZhq6plryql73x4f6hjrSbiK+cibCOe6ShjrWTgKiikLakkriij7Z/a5WunMG9rM9pVIF2Yo2gjbRcR3ZeSXdgS3mCbpiDb5mGcpxHMWLGtNZ0YIvWxebXxubi0fDk0/Lr2viah65pVYGlkrh/a5aAbJZAKlxtWYVwXIinlLqfjbOOe6NGMGGhj7W+rNBQOmpSPGySfqeTf6dWQXBbRnRbRnXIttjJuNrVxOV6ZpGah697Z5J8aJN9aZR+apXr2/nt3Pp+a5WgjbPKhMdQAAAAFnRSTlMAABY5ep61uePy8/X29/f9/f7+/v7+MMugfQAAANRJREFUeF6VzsOWBEEQhtFoDo1IlNu2NbZtvP9TTFZtCp2b/hYR59zVDxCOzhd9fc5FQhBbfs8VConE35mqKkoqlUzupjfisP0k6Rh2UNLHjLxVk9SENcbY1S1jp4Tzm2fG7soCXmEdEQeX4hjkeihe7kVAx2XMaxjgL8uWjIk4evAwP0c7pTRucC/zrOP6PfdxV9Mdz/r4ooclxfG0l98EmBmbrWJg4PfjPk7v/jnhxHB5k1J6UKG0Qgg5alG69yugDocTSX1oo6TqjLyyKlpa9LfwD1JpSKnLd4QZAAAAAElFTkSuQmCC', action: { type: 'encapsulate', options: { pre: "" }				}			}		}	}); };

/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */ if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) { mw.loader.using( 'user.options' ).then( function {		// This can be the string "0" if the user disabled the preference (phab:T54542#555387)		if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {			$.when( mw.loader.using( 'ext.wikiEditor' ), $.ready ).then( customizeToolbar );		}	} ); } // Toolbar end