Wikipedysta:Beau/skrypty/seen.js

Z Wikipedii, wolnej encyklopedii

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
// Autor: [[pl:User:Beau]]

var seenGadget = {
	version: 2,

	init: function() {
		if ( mw.config.get( 'wgNamespaceNumber' ) != 2 && mw.config.get( 'wgNamespaceNumber' ) != 3 ) {
			return;
		}

		if ( mw.util.getParamValue('printable') == 'yes' ) {
			return;
		}

		this.username = mw.config.get( 'wgTitle' ).replace(/\/.*$/, '');

		var that = this;

		var request = {
			action:	'query',
			list:	'usercontribs',
			ucuser:	this.username,
			ucdir:	'older',
			uclimit:	1,
			ucprop:	'timestamp',
			format:	'json',
			requestid:	new Date().getTime()
		};

		jQuery.getJSON( mw.util.wikiScript( 'api' ), request, function(result) {
			jQuery(document).ready(function() {
				if (result) {
					that.showResults(result);
				}
			});
		});
	},
	padZeros: function(number, length) {
		number = new String(number);
		while (number.length < length) {
			number = "0" + number;
		}
		return number;
	},
	formatDate: function(timestamp) {
		var date = new Date();
		date.setTime(timestamp);
		return this.padZeros(date.getFullYear(), 4) + '-' +
			this.padZeros(date.getMonth() + 1, 2) + '-' +
			this.padZeros(date.getDate(), 2) + ' ' +
			this.padZeros(date.getHours(), 2) + ':' +
			this.padZeros(date.getMinutes(), 2) + ':' +
			this.padZeros(date.getSeconds(), 2);
	},
	showResults: function(data) {
		data = data.query.usercontribs[0];
		if (!data || data.user != this.username)
			return;

		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 html = 'Ten użytkownik';
		var timestamp;
		var utc = Date.parse(data.timestamp);

		if (isNaN(utc)) {
			timestamp = data.timestamp;
		}
		else {
			timestamp = this.formatDate(utc);
		}

		html += ' ostatnią edycję wykonał ' + timestamp + '.';

		var div = document.createElement("div");
		div.style.cssText = "font-size:0.5em;line-height:1em";
		div.className = 'plainlinks';
		div.innerHTML = html;

		if ( mw.config.get( 'skin' ) == 'modern' ) {
			div.style.marginLeft = "10px";
			div.style.display = "inline-block";
		}

		firstHeading.appendChild(div);
	}
};

seenGadget.init();