Jump to content

User:RCSDevs/common.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by RCSDevs (talk | contribs) at 22:06, 11 July 2016. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
importScript('User:RCSDevs/quiz.js');

$(document).ready(function()
{
	var tab = $('#ca-nstab-main');
	
	//Ensure the current page is an actual article
	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 JSON 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.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"><a href="#" style="color: #009500">' + username + '</a><div id="heart-icon" src="data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0D%0A%3Csvg%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20x%3D%220px%22%20y%3D%220px%22%0D%0A%09%20width%3D%2240px%22%20height%3D%2216px%22%20viewBox%3D%220%200%2040%2016%22%20enable-background%3D%22new%200%200%2040%2016%22%20xml%3Aspace%3D%22preserve%22%3E%0D%0A%3ClinearGradient%20id%3D%22SVGID_1_%22%20gradientUnits%3D%22userSpaceOnUse%22%20x1%3D%2293.4795%22%20y1%3D%22-94.7085%22%20x2%3D%22102.5057%22%20y2%3D%22-105.4655%22%20gradientTransform%3D%22matrix%281%200%200%20-1%20-90%20-94%29%22%3E%0D%0A%09%3Cstop%20%20offset%3D%220%22%20style%3D%22stop-color%3A%23F39C9C%22%2F%3E%0D%0A%09%3Cstop%20%20offset%3D%220.5757%22%20style%3D%22stop-color%3A%23E97F7F%22%2F%3E%0D%0A%09%3Cstop%20%20offset%3D%220.8252%22%20style%3D%22stop-color%3A%23E36666%22%2F%3E%0D%0A%3C%2FlinearGradient%3E%0D%0A%3Cpath%20fill%3D%22url%28%23SVGID_1_%29%22%20d%3D%22M14.616%2C2.514c-0.894-0.957-2.082-1.483-3.345-1.483c-1.229%2C0-2.384%2C0.498-3.271%2C1.404%0D%0A%09C7.115%2C1.529%2C5.958%2C1.031%2C4.729%2C1.031c-1.263%2C0-2.452%2C0.527-3.345%2C1.483c-1.845%2C1.973-1.845%2C5.185-0.002%2C7.16%0D%0A%09c0.116%2C0.125%2C0.275%2C0.295%2C5.505%2C4.443L8%2C15l1.113-0.883c5.229-4.148%2C5.387-4.318%2C5.503-4.441C16.461%2C7.7%2C16.461%2C4.488%2C14.616%2C2.514z%22%2F%3E%0D%0A%3ClinearGradient%20id%3D%22SVGID_2_%22%20gradientUnits%3D%22userSpaceOnUse%22%20x1%3D%2294.8711%22%20y1%3D%22-95.6479%22%20x2%3D%22101.3658%22%20y2%3D%22-104.9233%22%20gradientTransform%3D%22matrix%281%200%200%20-1%20-90%20-94%29%22%3E%0D%0A%09%3Cstop%20%20offset%3D%220%22%20style%3D%22stop-color%3A%23FAADAD%22%2F%3E%0D%0A%09%3Cstop%20%20offset%3D%220.3465%22%20style%3D%22stop-color%3A%23F39C9C%22%2F%3E%0D%0A%09%3Cstop%20%20offset%3D%220.5757%22%20style%3D%22stop-color%3A%23E97F7F%22%2F%3E%0D%0A%09%3Cstop%20%20offset%3D%220.8252%22%20style%3D%22stop-color%3A%23E36666%22%2F%3E%0D%0A%3C%2FlinearGradient%3E%0D%0A%3Cpath%20fill%3D%22url%28%23SVGID_2_%29%22%20d%3D%22M13.645%2C3.496c-0.666-0.709-1.55-1.101-2.49-1.101c-0.943%2C0-1.824%2C0.384-2.453%2C1.06L8%2C4.093%0D%0A%09L7.341%2C3.496c-0.637-0.68-1.545-1.069-2.493-1.069c-0.948%2C0-1.856%2C0.389-2.493%2C1.07c-1.351%2C1.444-1.351%2C3.796%2C0%2C5.245%0D%0A%09C2.41%2C8.801%2C2.713%2C9.085%2C7.46%2C12.852L8%2C13.281l0.539-0.43c4.75-3.771%2C5.045-4.05%2C5.106-4.111%0D%0A%09C14.995%2C7.294%2C14.995%2C4.941%2C13.645%2C3.496z%22%2F%3E%0D%0A%3Cpath%20fill%3D%22none%22%20stroke%3D%22%23FFE7E6%22%20stroke-width%3D%220.75%22%20stroke-miterlimit%3D%2210%22%20d%3D%22M7.46%2C12.852C2.713%2C9.085%2C2.41%2C8.801%2C2.355%2C8.743%0D%0A%09c-1.351-1.449-1.351-3.801%2C0-5.245c0.636-0.681%2C1.545-1.07%2C2.493-1.07c0.948%2C0%2C1.855%2C0.389%2C2.493%2C1.069L8%2C4.093l0.702-0.637%0D%0A%09c0.629-0.676%2C1.509-1.06%2C2.452-1.06c0.941%2C0%2C1.825%2C0.392%2C2.491%2C1.101c1.351%2C1.445%2C1.351%2C3.798%2C0%2C5.244%0D%0A%09c-0.06%2C0.062-0.356%2C0.341-5.106%2C4.11L8%2C13.281L7.46%2C12.852z%22%2F%3E%0D%0A%3ClinearGradient%20id%3D%22SVGID_3_%22%20gradientUnits%3D%22userSpaceOnUse%22%20x1%3D%22117.4795%22%20y1%3D%22-94.7061%22%20x2%3D%22126.5061%22%20y2%3D%22-105.4636%22%20gradientTransform%3D%22matrix%281%200%200%20-1%20-90%20-94%29%22%3E%0D%0A%09%3Cstop%20%20offset%3D%220%22%20style%3D%22stop-color%3A%23F5ABAB%22%2F%3E%0D%0A%09%3Cstop%20%20offset%3D%220.5757%22%20style%3D%22stop-color%3A%23EC9292%22%2F%3E%0D%0A%09%3Cstop%20%20offset%3D%220.8252%22%20style%3D%22stop-color%3A%23E37676%22%2F%3E%0D%0A%3C%2FlinearGradient%3E%0D%0A%3Cpath%20fill%3D%22url%28%23SVGID_3_%29%22%20d%3D%22M38.616%2C2.514c-0.894-0.957-2.082-1.483-3.345-1.483c-1.229%2C0-2.385%2C0.498-3.271%2C1.404%0D%0A%09c-0.884-0.906-2.042-1.404-3.271-1.404c-1.263%2C0-2.451%2C0.527-3.345%2C1.483c-1.845%2C1.973-1.845%2C5.185-0.002%2C7.16%0D%0A%09c0.116%2C0.125%2C0.276%2C0.295%2C5.505%2C4.443L32%2C15l1.113-0.883c5.229-4.148%2C5.387-4.318%2C5.503-4.441C40.461%2C7.7%2C40.461%2C4.488%2C38.616%2C2.514z%22%2F%3E%0D%0A%3ClinearGradient%20id%3D%22SVGID_4_%22%20gradientUnits%3D%22userSpaceOnUse%22%20x1%3D%22118.8701%22%20y1%3D%22-95.6484%22%20x2%3D%22125.3656%22%20y2%3D%22-104.9249%22%20gradientTransform%3D%22matrix%281%200%200%20-1%20-90%20-94%29%22%3E%0D%0A%09%3Cstop%20%20offset%3D%220%22%20style%3D%22stop-color%3A%23FBB9B9%22%2F%3E%0D%0A%09%3Cstop%20%20offset%3D%220.3465%22%20style%3D%22stop-color%3A%23F5ABAB%22%2F%3E%0D%0A%09%3Cstop%20%20offset%3D%220.5757%22%20style%3D%22stop-color%3A%23EC9292%22%2F%3E%0D%0A%09%3Cstop%20%20offset%3D%220.8252%22%20style%3D%22stop-color%3A%23E37676%22%2F%3E%0D%0A%3C%2FlinearGradient%3E%0D%0A%3Cpath%20fill%3D%22url%28%23SVGID_4_%29%22%20d%3D%22M37.645%2C3.496c-0.664-0.709-1.549-1.101-2.487-1.101c-0.944%2C0-1.824%2C0.384-2.455%2C1.06L32%2C4.093%0D%0A%09l-0.66-0.597c-0.637-0.68-1.544-1.069-2.492-1.069c-0.947%2C0-1.856%2C0.389-2.492%2C1.07c-1.351%2C1.444-1.351%2C3.796%2C0%2C5.245%0D%0A%09c0.058%2C0.059%2C0.359%2C0.343%2C5.105%2C4.109L32%2C13.281l0.539-0.43c4.75-3.771%2C5.047-4.05%2C5.105-4.111%0D%0A%09C38.996%2C7.294%2C38.996%2C4.941%2C37.645%2C3.496z%22%2F%3E%0D%0A%3Cpath%20fill%3D%22none%22%20stroke%3D%22%23FFE7E6%22%20stroke-width%3D%220.75%22%20stroke-miterlimit%3D%2210%22%20d%3D%22M31.461%2C12.852%0D%0A%09c-4.746-3.767-5.049-4.051-5.105-4.108c-1.351-1.449-1.351-3.801%2C0-5.245c0.636-0.681%2C1.545-1.07%2C2.492-1.07%0D%0A%09c0.948%2C0%2C1.855%2C0.389%2C2.492%2C1.069L32%2C4.093l0.701-0.637c0.631-0.676%2C1.511-1.06%2C2.455-1.06c0.939%2C0%2C1.822%2C0.392%2C2.488%2C1.101%0D%0A%09c1.352%2C1.445%2C1.352%2C3.798%2C0%2C5.244c-0.06%2C0.062-0.355%2C0.341-5.105%2C4.11L32%2C13.281L31.461%2C12.852z%22%2F%3E%0D%0A%3C%2Fsvg%3E%0D%0A"></div></b></div>';

			//Add message to article body
			$('#siteSub').before(message);
		});
	}
	
	$('#ca-wikilove').click(function(event)
	{
		event.preventDefault();
		alert("Send wikilove api request to " + this.text());
	});
});