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) { console.log(data.query.recentchanges); var revids = data.query.recentchanges.reduce(function(a, o){if("unpatrolled" in o){a.push(o.revid);} return a;}, []); console.log(revids); 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{ $( '#wpTextbox1' ).textSelection( 'replaceSelection', type.insert); });						container.appendChild(button);						addTo.insertBefore(container, addTo.firstChild);					}); }			});		}	};	// 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.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				}); }		}	}); });