User:Mikevoir/global.js

importArticles({   type: 'script',    articles: [        'u:dev:MediaWiki:AjaxBatchDelete.js',        'u:dev:MediaWiki:Bang.js',        'u:dev:MediaWiki:FileUsageAuto-update/code.js',        'u:dev:MediaWiki:LastEdited/code.js',        'u:dev:MediaWiki:MassRename/code.js',        'u:dev:MediaWiki:MassNullEdit/code.js',        'u:dev:MediaWiki:PurgeButton.js',        'u:dev:MediaWiki:Wikimarks/code.js',    ] });

// Load functions upon page load if (/comp|inter|loaded/.test(document.readyState)) { delay(noBlankLinkSuggest, 6000); delay(noThank); delay(properLink); delay(properDiff); } else { document.addEventListener('DOMContentLoaded', function {		delay(noBlankLinkSuggest, 6000);		delay(noThank);		delay(properLink);		delay(properDiff);	}); } // Load functions upon RC refresh document.addEventListener('click', function(event) {   if (event.target && event.target.closest('.mw-rcfilters-ui-filterWrapperWidget-bottom > .mw-rcfilters-ui-filterWrapperWidget-showNewChanges > a.oo-ui-buttonElement-button')) {		delay(noThank, 5000);		delay(properLink, 5000);    } });

// Default delay simplify usage and maintenance function delay(func, delay) { if (!delay) {delay = 1000;} //wait at least 1s just in case setTimeout(func, delay); }

// No thanks function noThank { 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;		}	}); }

// Make RC properly give links on diffs that include page creations function properLink { document.querySelectorAll('.mw-changeslist-links > span:first-child').forEach(		function(el) {			if (el.childNodes.length === 1 && el.childElementCount === 0){				var row = el.parentNode.parentNode.parentNode.parentNode.parentNode;				if (row.nodeName == 'TBODY') { row = row.parentNode; }				var pageName = row.querySelectorAll('.mw-changeslist-title')[0].attributes.title.nodeValue;				el.removeChild(el.firstChild); // delete plain text				var row_revid = row.getAttributeNode('data-mw-revid');				if (row_revid && row_revid.value) {					// single edit diff					el.appendChild(buildLink([row_revid.value, 0], pageName));				}else{					// multiple edit diff					var revisions = [];					row.querySelectorAll('tr.mw-rcfilters-ui-highlights-enhanced-nested.mw-changeslist-edit').forEach(function(edit) { // console.log(edit); //debug var edit_revid = edit.getAttributeNode('data-mw-revid'); if (edit_revid && edit_revid.value) { revisions[revisions.length] = edit_revid.value; }					});					// console.log(revisions); //debug					el.appendChild(buildLink(revisions, pageName));				}			}		}	); document.querySelectorAll('tr.mw-changeslist-src-mw-new.mw-rcfilters-ui-highlights-enhanced-nested').forEach(		function(row) {			if (!row.querySelector('.mw-changeslist-diff')){				var el = row.querySelector('td.mw-enhanced-rc-nested');				var pageName = row.querySelector('.mw-enhanced-rc-time a').title;				var targetNode = el.childNodes[3];				console.log(targetNode);				var row_revid = row.getAttributeNode('data-mw-revid').value;				targetNode.parentNode.insertBefore(document.createTextNode(' | '), targetNode);				targetNode.parentNode.insertBefore(buildLink([row_revid.value, 0], pageName, 'prev'), targetNode);				targetNode.parentNode.insertBefore(document.createTextNode(')'), targetNode); targetNode.remove; }		}	);	function buildLink(revisions, page, label) {		// console.log(revisions); //debug		var link = document.createElement('a');		var from = revisions[revisions.length-1];		var to = revisions[0];		if (from == 0) {			if (!label){label = 'diff';}			link.classList.add('mw-changeslist-diff');		} else {			if (!label){label = revisions.length + ' changes';}			link.classList.add('mw-changeslist-groupdiff');		}		link.href = '/wiki/' + page + '?diff=' + to + '&oldid=' + from;		link.title = page;		link.innerHTML = label;		return link;	} }

// Properly display diff for page creation edits function properDiff { if (document.querySelectorAll('#mw-diff-ntitle1 > strong > a').item(0)) { var table = document.getElementsByClassName('diff')[0]; var api = new mw.Api; var revid = document.querySelectorAll('#mw-diff-ntitle1 > strong > a').item(0).href.replace(/^.+\?oldid=/g, ''); var api_opt = { action: 'compare', torev: revid, fromslots: 'main', 'fromtext-main': '', prop: 'diff|ids', formatversion: 2 };		if ( revid && document.querySelectorAll('#mw-diff-otitle1 > strong > a').item(0) && !document.querySelectorAll('#differences-prevlink').item(0) ) { //Add edit link to page creation diff table.querySelectorAll('#mw-diff-otitle4')[0].innerHTML = ' strong > a')[0].title + 			'?diff=' + 			document.querySelectorAll('#mw-diff-otitle1 > strong > a')[0].href.replace(/^.+\?oldid=/g, '') + 			'&oldid=0" title="' + 			document.querySelectorAll('#mw-diff-otitle1 > strong > a')[0].title + 			'" id="differences-prevlink">← Older edit'; }else if (revid && !document.querySelectorAll('#differences-prevlink').item(0)) { //Render proper diff table.getElementsByClassName('diff-ntitle')[0].colSpan = 4; table.getElementsByClassName('diff-notice')[0].parentNode.remove; api.get(api_opt).then(function(data) {				table.innerHTML = '  ' + table.innerHTML;				table.querySelectorAll('tbody')[0].innerHTML = table.querySelectorAll('tbody')[0].innerHTML + data.compare.body;			}); }	} }

// Hide blank link suggestion popup as fandom cant be bothered to fix it function noBlankLinkSuggest { var parent = document.querySelector('.wikiEditor-ui-linkSuggest'); if (parent) { var targetNode = parent.querySelector('.oo-ui-selectWidget'); // console.log(targetNode); var hideIfBlank = function(mutations) { mutations.forEach(function(mutation){				var node = mutation.target;				if (node.childElementCount == 0) {					parent.classList.add('oo-ui-element-hidden');				}			}); };		var observer = new MutationObserver(hideIfBlank); observer.observe(targetNode, { childList: true }); } }