Jump to content

User:Zhaofeng Li/SignMe.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
/*
	SignMe
	Fetches the user signature via ResourceLoader, and appends it to the textbox with a timestamp.
	This is only a proof-of-concept!
*/

// returns a signature, using the standard format
function SignMe_getSignature() {
	var months = new Array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" );
	var date = new Date();
	var username = mw.config.get("wgUserName");
	if ( username === null ) { // not logged-in!
		mw.log( "Unable to sign - Not logged-in" );
		return null;
	} else {
		var signature = mw.user.options.get("nickname");
		if ( signature === null ) { // no custom signature set
			signature = "[[User:" + username + "]]";
		}
		// omfg this is messy...
		var hours = date.getUTCHours();
		if ( hours < 10 ) hours = "0" + hours;
		var minutes = date.getUTCMinutes();
		if ( minutes < 10 ) minutes = "0" + minutes;
		var timestamp = hours + ":" + minutes
		                + ", " + date.getUTCDate() + " " + months[date.getUTCMonth()] + " " + date.getUTCFullYear()
		                + " (UTC)";
		return signature + " " + timestamp;
	}
}

// appends the signature to the textbox
// this assumes #wpTextbox1 is present
function SignMe_appendSignature() {
	var signature = SignMe_getSignature();
	if ( signature !== null ) {
		$( "#wpTextbox1" ).val( $( "#wpTextbox1" ).val() + signature );
		return true;
	} else {
		return false;
	}
}

// init
if ( $( "#wpTextbox1" ).length ) {
	var SignMe_portlet = mw.util.addPortletLink( "p-tb", "#", "SignMe" );
	$( SignMe_portlet ).click( SignMe_appendSignature );
}