Jump to content

User:CMelo (WMF)/common.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
$( function () {
	
	const showUserActivity = function(ul) {
		console.log('this', 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 = '<input id="display-user-activity-'+i+'" type="checkbox" onclick="showUserActivity('+i+')"> 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 =
			"<span class=\"mw-activeusergadget-span mw-activeusergadget-" +
			timespan + "\">" + messages[lang][timespan] + "<span>";
		return $( marker );
	};
} );