User:Mikevoir/BetterFandom.js

(function{	//Dependencies	importScriptPage('User:Mikevoir/lib.js', 'community');	var config = mw.config.values;	var api = new mw.Api;	(window.dev = window.dev || {}).betterFandom = window.dev.betterFandom || {};	// 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){			var $textbox = $( '#wpTextbox1' );			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				) { var addTo = document.querySelector(type.query || '.group-insert'); if (addTo) { 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{ $textbox.textSelection( 'replaceSelection', type.insert); }); container.appendChild(button); addTo.insertBefore(container, addTo.children[0]); }				}			});		}	};	// Load conditions	mw.hook('userjs._LIB').add(function(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				}); }		}	}); });