Jump to content

User:RCSDevs/author.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
importStylesheet('User:RCSDevs/author.css');

$(document).ready(function()
{
	var tab = $('#ca-nstab-main');
	
	
	if(tab.length && tab.attr('class') === 'selected')
	{
	
		var currentURL = decodeURIComponent(tab.find('span a').eq(0).attr('href')),
			title = currentURL.substring(currentURL.indexOf('/wiki/') + 6);

		//Define parameters for call to MediaWiki API (https://www.mediawiki.org/wiki/API)
		var params = {
				action: 'query',
				prop: 'revisions',
				rvprop: 'user',
				rvlimit: 'max',
				format: 'json',
				titles: title
			},
			coordLabel = $('#coordinates');

		//First checks if the article has a (redundent) coordinate label
		if(coordLabel.length)
		{
			//For some reason .first() doesn't work
			coordLabel.closest('tr').find('th').html('<a href="/wiki/Geographic_coordinate_system" title="Geographic coordinate system">Coordinates</a>');
			coordLabel.parent().remove();
		}
		
		//Send request with JSON callback
		$.getJSON('https://en.wikipedia.org/w/api.php', params, function(json)
		{
			var pages = json.query.pages,
				revs = pages[Object.keys(pages)[0]].revisions, //Extract 'revisions' array. For some reason, normal dot format doesn't work
				
				/* The following line:
					1) Calculate occurances for all usernames in the revision list (the last 500 revisions)
					2) Sort the array by decreasing occurances
					3) Pull the top value and set 'username'
				*/
				username = revs.filter(function(rev) { return !rev.user.includes('.') }).sort(function(a, b) { return revs.filter(function(x) { return x.user === b.user }).length - revs.filter(function(x) { return x.user === a.user }).length })[0].user,
				
				//Construct an inline-styled message to add to page
				message = '<div id="siteSub" style="float: right"><b><i>The main contributor to this article is </i><div id="pt-userpage" style="display: inline-block; background-position: left center"><strong style="color: #009500">' + username + '</strong><div id="heart-icon" title="Thank this user for their work!"></div></div></b></div>';

			//Add message to article body
			$('#siteSub').before(message);
			
			$('#heart-icon').click(function()
			{
				var params = {
					action: 'wikilove',
					title: 'User:RCSDevs',//'User:' + username.replace(' ', '_'),
					text: 'Your work has been noticed! Thanks for contributing to /wiki/' + title,
					token: mw.user.tokens.get('editToken'),
					type: 'barnstar-normal'
				};
		
				$.post('https://en.wikipedia.org/w/api.php', params);
			});
		});
	}
});