User:Tomodachi94/common/disambiguations.js

mw.loader.using("mediawiki.api").then(function {	var allowedNS = ['', 'Template'];	// Filter by allowed namespaces	if (!allowedNS.includes(mw.config.get('wgCanonicalNamespace'))) {		return;	}	// var linksOnPage = {	// 		action: 'query',	// 		format: 'json',	// 		titles: mw.config.values.wgPageName,	// 		prop: 'links'	// 	};	async function getTransclusions {			var transclusionParams = {			action: 'query',			titles: 'Template:Disambiguation',			prop: 'transcludedin',			format: 'json',			tilimit: 'max'		};		var api = new mw.Api;		var transclusions = [];		while (true) {			// console.log(transclusionParams);			var result = await api.get(transclusionParams);			// console.log(result);			var pages = result.query.pages;			for (var p in pages) {				transclusions = transclusions.concat(pages[p].transcludedin);			}			if (result.continue !== undefined) {				for (var param in result.continue) { transclusionParams[param] = result.continue[param]; }			} else { return transclusions.map(obj => obj.title); }		}	}	var addDisambig = function(disambigData, element) { disambigData.then(function(transclusions) {			var links = element.find('a[href^="/"]').each(function { var pagelink = $(this); // if (pagelink.children.length > 0) { // 	// NOT IN USE - Only change color of simple links // 	return; // }				var title = pagelink.attr('title'); if (transclusions.includes(title)) { // console.log(title); pagelink.addClass('mw-disambig'); }			});			var amount = links.filter(function { return $(this).hasClass('mw-disambig'); }).length;			console.log('Disambiguated ' + amount + ' links.');		}); };	var action = mw.config.get('wgAction'); var disambigData = getTransclusions;

if (action === 'edit') { // Run script when preview is updated var preview = $('#wikiPreview'); var observer = new MutationObserver(function(event) {			addDisambig(disambigData, preview);		}); observer.observe(preview[0], {characterData: true, childList: true}); } else if (action === 'view') { addDisambig(disambigData, $('#bodyContent')); } });