MediaWiki:Gadget-countWLH.js

From Terraria Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// Add the total numbers of links and transclusions to WhatLinksHere pages
// Authors: RheingoldRiver, Rye Greenwood

$(document).ready(function() {
	if (mw.config.get('wgCanonicalSpecialPageName') !== 'Whatlinkshere') return;
	
	// get WLH page title and namespace from the form input fields
	var title = $('#mw-whatlinkshere-target').val();
	var ns = $('#namespace').val();
	title = title ? title : mw.config.get('wgTitle').replace('WhatLinksHere/', ''); // if failed, get title from URL
	
	// fetch data
	return new mw.Api().get({
		action : 'query',
		prop : 'linkshere|transcludedin',
		titles : title,
		lhlimit : 'max',
		tilimit : 'max',
		lhnamespace : ns === '' ? '*' : ns,
		tinamespace : ns === '' ? '*' : ns,
	}).then(function(data) {
		var linkshereCount, transcludedinCount = 0;
		// extract information from the API result
		for (var p in data.query.pages) {
			var page = data.query.pages[p];
			linkshereCount = page.linkshere ? page.linkshere.length : 0;
			transcludedinCount = page.transcludedin ? page.transcludedin.length : 0;
		}
		
		// i18n: load localized pipes and parentheses
		new mw.Api().loadMessagesIfMissing( [ 'pipe-separator', 'parentheses' ] )
		.then(function() {
			var pipe = mw.msg("pipe-separator");
			
			// prepare injection into HTML
			var fieldset = $("#mw-content-text > fieldset"); // element that holds the "Hide transclusions | ..." string
			var fieldsetText = fieldset.html(); // get raw HTML text of its contents
			var firstPipe = fieldsetText.indexOf(pipe);
			var secondPipe = fieldsetText.indexOf(pipe, firstPipe + 1);
			// inject
			var newText = ''.concat(
				fieldsetText.substring(0, firstPipe), " ",
				mw.msg('parentheses', transcludedinCount),
				fieldsetText.substring(firstPipe, secondPipe), " ",
				mw.msg('parentheses', linkshereCount),
				fieldsetText.substring(secondPipe)
			);
			fieldset.html(newText); // replace HTML text with new text
		});
	});
});