User:Magnus Manske/me too.js

/** Tool to change between multiple Wiki user names Include in the Special:MyPage/common.js of ALL your users on a wiki, like so: importScript("User:Magnus Manske/me too.js");

addOnloadHook ( function {	var store_password_in_cookie = true ; // EDIT THIS : true if your password should be stored for 1h in a cookie, false otherwise	var users = [ 'Magnus Manske', 'The other Magnus Manske' ] ; // EDIT THIS : A comma-separated list of all your user names

// -- // DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING! // --

$.each ( users, function ( k , user ) {		if ( mw.config.get('wgUserName') == user ) return ;		var portletLink = mw.util.addPortletLink( 'p-tb', 'javascript:;', 'Become '+user,'t-wd_me_too'+k);		$(portletLink).click ( function { me_too_change_user ( user, store_password_in_cookie ) ; return false ; } ) ;	}) ; } ) ;

function me_too_change_user ( new_user, store_password_in_cookie ) {

var hours_for_password_in_cookie = 1 ;

function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); }		return null; }

var cookie_name = 'mee_too_'+new_user ; var pass = readCookie(cookie_name); if ( pass == null || pass == 'null' ) pass = prompt("Enter password for user "+new_user, "") ; if ( pass == '' ) return ; // No password if ( store_password_in_cookie ) { var date = new Date; date.setTime(date.getTime+(hours_for_password_in_cookie*60*60*1000)); var expires = "; expires="+date.toGMTString; document.cookie = cookie_name+"="+pass+expires+"; path=/"; }

// Logout $.post('/w/api.php?action=logout&format=json',function(d0){		// Get token		var o = { action:'login',lgname:new_user,lgpassword:pass,format:'json' } ;		$.post('/w/api.php',o,function(d){ o.lgtoken = d.login.token ; // Log in				$.post('/w/api.php',o,function(d2){						if ( d2.login.result == "Success" ) {							$('body').html ( " Welcome, " + new_user+"! ") ;							location.reload ;						} else {							alert ( "Something went wrong: "+d2.login.result);						}					},'json') },'json')	},'json') ; }