User:Mikevoir/BetterFandom.js

(function{	// Double load protection	(window.dev = window.dev || {}).betterFandom = window.dev.betterFandom || {};	if (window.dev.betterFandom.__LOADED) {return;}	//Dependencies	importScriptPage('User:Mikevoir/lib.js', 'community');	var lib;	var config = mw.config.values;	var api = new mw.Api;	// Improvements	var betterFandom = {		patrolOnDelete: function{			lib.waitFor('button#wpConfirmB[value="Delete page"]', function{ document.querySelector('button#wpConfirmB[value="Delete page"]').addEventListener('click', function{					api.get({action:'query', rctitle: config.wgPageName, list:'recentchanges', rcprop:'ids|patrolled', meta: 'tokens', type: 'patrol'}).then(function(data){ if(data && data.query && data.query.recentchanges && data.query.tokens && data.query.tokens.patroltoken) { var revids = data.query.recentchanges.reduce(function(a, o){if("unpatrolled" in o){a.push(o.revid);} return a;}, []); if (revids && revids.length>0) { revids.forEach(function(revid){api.post({action:'patrol', revid:revid, token: data.query.tokens.patroltoken});}); }						}					});				});			});		},		noThanks: function{			document.querySelectorAll('.mw-thanks-thank-link').forEach(function(el) { if (el.parentNode.nodeName === 'SPAN'){ el.parentNode.remove; }else{ if ( el.previousSibling.data === '( ' && el.nextSibling.data === ' )' ) { el.nextSibling.remove; el.previousSibling.remove; }					el.remove; }			});		},		customInsert: function(types){			if (!Array.isArray(types)) {types = [types];}			types.forEach(function(type){ if (					type.insert &&					( (type.insert) instanceof String || 'string' == typeof type.insert ) &&					type.insert.length>0				) { lib.waitFor((type.query || '.group-insert'), function{						var addTo = document.querySelector((type.query || '.group-insert'));						var container = document.createElement('span');						container.classList.add('tool', 'oo-ui-widget', 'oo-ui-widget-enabled', 'oo-ui-buttonElement', 'oo-ui-buttonElement-frameless', 'oo-ui-iconElement', 'oo-ui-buttonWidget');						var button = document.createElement('a');						button.classList.add('oo-ui-buttonElement-button');						button.innerHTML = (type.button || 'INS');						button.role = 'button';						button.addEventListener('mouseup', function{ if (type.replaceAll){ $( '#wpTextbox1' ).textSelection( 'setContents', type.insert); }							else{ $( '#wpTextbox1' ).textSelection( 'replaceSelection', type.insert); }						});						container.appendChild(button);						addTo.insertBefore(container, addTo.firstChild);					}); }			});		},		originalPreview: function {			var fileBox = new MutationObserver(function (mutations, me) { mutations.forEach(function(mutation){					if ( mutation && mutation.removedNodes ) {						mutation.removedNodes.forEach(function(removedNode){ if (removedNode.id && removedNode.id == 'LightboxPreload'){ var preview = document.querySelector('.WikiaLightbox > div.media > img'); var fullLink = document.querySelector('.WikiaLightbox > header.LightboxHeader > .see-full-size-link'); if (preview) { preview.src = preview.src + '&format=original'; } if (fullLink) { fullLink.href = fullLink.href + '&format=original'; } }						});					}				});			});			fileBox.observe(document.body, { childList: true });		}	};	// Load conditions	mw.hook('userjs._LIB').add(function(_LIB){ window.dev.betterFandom.__LOADED = true; lib = _LIB; if (config.wgAction == 'delete'){ betterFandom.patrolOnDelete; }		if (['edit', 'submit'].includes(config.wgAction) && window.dev.betterFandom.customInsert) { betterFandom.customInsert(window.dev.betterFandom.customInsert); }		if (config.wgNamespaceNumber == 0) { betterFandom.originalPreview; }		if (			config.wgCanonicalSpecialPageName == 'Recentchanges' ||			config.wgDiffNewId ||			config.wgAction == 'history'		) { lib.waitFor('.mw-thanks-thank-link', betterFandom.noThanks); // start observing for RC reloads if (config.wgCanonicalSpecialPageName == 'Recentchanges') { var observer = new MutationObserver(function (mutations, me) {					if ( mutations[0] && mutations[0].target && mutations[0].target.classList.contains('mw-rcfilters-ui-changesListWrapperWidget') ) {betterFandom.noThanks;}				}); observer.observe(document.querySelector('.mw-changeslist'), {				 childList: true,				  subtree: true				}); }		}	}); });