Jump to content

User:Ale jrb/Scripts/unblock.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ale jrb (talk | contribs) at 21:38, 23 March 2010 (test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// AJAX unblocking script

function unblockMain () {
	
	this.init = function () {
		if ( ( wgPageName === 'Special:BlockList' ) && ( wgAction === 'unblock' ) && ( document.getElementById ( 'unblockip' ) ) ) unblock.hookInterface ();
	}
	
	this.hookInterface = function () {
		var unblockform = document.getElementById ( 'mw-unblock-table' );
		unblockform.childNodes[0].childNodes[4].childNodes[3].innerHTML = '<input type="submit" tabindex="4" name="wpBlock" value="Unblock"> <input id="unblock-background" type="button" value="Background Unblock" name="wpBlock2" tabindex="3" onclick="unblock.doUnblock ();" /><br /><div id="unblock-back-status"></div>';
		
		//var regTest = /input type="hidden" value="([^ ]+?)" name="wpEditToken"/gi;
		//var matches = regTest.exec ( this.unblockform.innerHTML );
		//if ( ! matches ) return false;
		this.unblocktoken = encodeURIComponent ( this.getInputValue ( 'wpEditToken' ) ); //matches [1] );
	}
	
	this.doUnblock = function () {
		var user = this.getInputValue ( 'wpUnblockAddress' );
		var reason = this.getInputValue ( 'wpUnblockReason' );
		
		if ( ! user ) { this.outputError ( 'You must enter a user' ); return false; }
		if ( user === '' ) { this.outputError ( 'You must enter a user' ); return false; }
		if ( reason !== '' ) { reason = '&reason=' + encodeURIComponent ( reason ); }
		
		wa ( ':api' ).command ( 'unblock', 'format=xml&action=unblock&token=' + this.unblocktoken + '&user=' + encodeURIComponent ( user ) + reason, 'POST' ).wait ( function () {
			if ( wa ( ':api' ).results ['unblock']['error-attr'] !== undefined ) { unblock.outputError ( wa ( ':api' ).results ['unblock']['error-attr']['info'] ); return false; } else {
				unblock.outputError ( 'You have successfully unblocked ' + user + ' (<a href="' + wgServer + wgScript + '?view=User_talk:' + encodeURIComponent ( user ) + '">go to talk page</a>)' );
			}
		}).run ();
	}
	
	this.getInputValue = function ( inputName ) {
		var inputs = document.getElementsByTagName ( 'input' );
		for ( var i = 0, l = inputs.length; i < l; i ++ ) {
			if ( inputs[i].getAttribute ( 'name' ) == inputName ) return inputs[i].value;
		}
		return false;
	}
	
	this.outputSuccess = function ( success ) {
		document.getElementById ( 'unblock-back-status' ).innerHTML = '<span style="padding: 4px; color: #558855; font-weight: bold;">' + success + '</span>';
		return error;
	}
	this.outputError = function ( error ) {
		document.getElementById ( 'unblock-back-status' ).innerHTML = '<span style="padding: 4px; color: #885555; font-weight: bold;">unblock error: ' + error + '</span>';
		return error;
	}
}

importScript ( 'User:Ale_jrb/Scripts/waLib2.js' );
var unblock = new unblockMain ();
hookEvent ( 'load', unblock.init );