Jump to content

User:Incnis Mrsi/edithysteria.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.
// Ported from [[pl:MediaWiki:Gadget-edithysteria.js]], see there for authorship

window.editHysteriaGadget = {
	version: 5,

	init: function() {
		if ( mw.config.get( 'wgNamespaceNumber' ) < 0 || mw.util.getParamValue('printable') === 'yes' ) {
			return;
		}

		var that = this;

		var request = {
			action:	'query',
			prop:	'revisions',
			rvprop:	'user|ids',
			format:	'json',
			titles:	mw.config.get( 'wgPageName' ),
			rvlimit:	50,
			requestid:	new Date().getTime()
		};
		jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function(result) {
			jQuery(document).ready(function() {
				if (result) {
					that.showResults(result);
				}
			});
		});
	},
	userlink: function(name) {
		var str = '<a href="' + mw.util.getUrl('User:' + name) + '">' + name + '</a> ' +
		 '(<a href="' + mw.util.getUrl('User talk:' + name) + '">talk</a>' +
		 ' <b>·</b> <a href="' + mw.util.getUrl('Special:Contributions/' + name) + '">contribs</a>)';

//		if ( jQuery.inArray( 'sysop', mw.config.get( 'wgUserGroups' ) ) != -1 ) {
//			str += ', <a href="' + mw.util.getUrl('Special:Blockip/' + name) + '">block</a>';
//		}
//
//		str += ')';

		return str;
	},
	showResults: function(data) {
		var page = data.query.pages[ mw.config.get( 'wgArticleId' ) ];
		var firstHeading;
		var headers = document.getElementsByTagName( 'h1' );

		for ( var i = 0; i < headers.length; i++ ) {
			var header = headers[i];
			if(header.className == "firstHeading" || header.id == "firstHeading" || header.className == "pagetitle") {
				firstHeading = header; break;
			}
		}

		if( !firstHeading ) {
			firstHeading = document.getElementById("section-0");
		}

		if( !firstHeading ) {
			return;
		}

		var div = document.createElement("div");
		div.style.cssText = "font-size:0.5em;line-height:1em";
		div.className = 'plainlinks';
		if ( mw.config.get( 'skin' )  == 'modern' ) {
			div.style.marginLeft = "10px";
			div.style.display = "inline-block";
		}

		firstHeading.appendChild(div);

		var html = '';
		if (! page) {
			div.innerHTML = html + "Page not exists.";
			div.style.color = "red";
			return;
		}

		var topuser = page.revisions[0].user;
		var ldiff;
		var luser;
		var count = 1;

		for (var i = 1; i < page.revisions.length; i++) {
			ldiff = page.revisions[i].revid;
			luser = page.revisions[i].user;
			if (luser != topuser)
				break;
			count++;
		}

		if (ldiff) {
			html += 'Last <a href="'+ mw.util.getUrl( mw.config.get('wgPageName') ) + '?diff=cur&oldid=' + ldiff + '"/>edited</a>' +
				' by ' + this.userlink(topuser) + ' (' + count + '×)';

			if(luser !== "" && luser != topuser) {
				html += ', previous edit by ' + this.userlink(luser);
			}
			html += '.';
			div.innerHTML = html;
		}
		else if (topuser !== "") {
			div.innerHTML = html + 'This is the only version by ' + this.userlink(topuser) + '.';
		}
		if ( mw.config.get('skin') == 'modern' ) {
			var links = div.getElementsByTagName('a');
			for (var i = 0; i < links.length; i++) {
				links[i].style.color = 'white';
				links[i].style.textDecoration = 'underline';
			}
		}
	}
};

editHysteriaGadget.init();