User:Paul2520/UserLastActive.js

// // Based on https://pl.wikipedia.org/wiki/Wikipedysta:Beau/skrypty/seen.js, which I learned about from https://meta.wikimedia.org/wiki/Community_Wishlist_Survey_2022/Bots_and_gadgets#Gadget:_Who_is_active 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 = 'This user'; var timestamp; var utc = Date.parse(data.timestamp);

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

html += ' made their last edit @ ' + timestamp + '.';

var div = document.createElement("div"); /* font-size was originally 0.5em */ div.style.cssText = "font-size:0.4em;line-height:1em;color:purple;"; 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; //