User:CMelo (WMF)/common.js

$( function {	const showUserActivity = function(ul) {			$( 'ul#ul-display-user-activity-'+i+' li a[title^="User:"]' ).each( function { const link = $( this ); const username = link.attr( 'title' ).substring( 5 ); });		$( 'ul#ul-display-user-activity-'+i+' li a[title^="User:"]' ).each( function { const link = $( this ); const username = link.attr( 'title' ).substring( 5 ); const api = new mw.Api; api.get( {				format: 'json',				action: 'query',				list: 'usercontribs',				uclimit: '1',				ucuser: username			} ).then( function ( result ) {				if ( result.query.usercontribs.length ) {					const timestamp = result.query.usercontribs[0].timestamp;					getLastActiveMarker( timestamp ).insertAfter( link );				}			} ); } );	}

const uls = document.querySelectorAll("ul"); if (uls.length > 0) { for (var i = 0; i < uls.length; i++) { var matches = uls[i].outerHTML.match(/href="\/wiki\/User:/gm);

if (matches && matches.length > 1) { uls[i].innerHTML = ' Display user activity '+uls[i].innerHTML; uls[i].id = 'ul-display-user-activity-'+i; document.querySelector("#display-user-activity-"+i).addEventListener("click", showUserActivity(this)); }		}	}

const messages = { en: { recent: 'Last edited recently', thisyear: 'Last edited this year', overayear: 'Last edited over a year ago' }	};

const lang = mw.config.get( 'wgUserLanguage' );

const getLastActiveMarker = function ( timestamp ) { const date = Date.parse( timestamp ); const now = Date.now; const diff = Math.floor( ( now - date ) / ( 1000 * 60 * 60 * 24 ) ); var timespan; if ( diff > 365 ) { timespan = 'overayear'; } else if ( diff > 30 ) { timespan = 'thisyear'; } else { timespan = 'recent'; }		const marker = "" + messages[lang][timespan] + " "; return $( marker ); }; } );