Usuário(a):FranciscoMG/twinklefluff.js

Origem: Wikipédia, a enciclopédia livre.

Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Internet Explorer / Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
  • Opera: Pressione Ctrl-F5.
/* Twinkle - reversão ***********************************************************************
  *    Origem: [[Usuário:Lijealso/twinklefluff.js]]
  *     Ajuda: [[Wikipedia:Software/Scripts/Twinkle]]
  * Descrição: Permite facilitar a reversão de edições.
  *     Autor: [[:w:en:AzaToth]]
  ********************************************************************************************/
 
/**
 Twinklefluff revert and antivandalism utillity
 */
// If TwinkleConfig aint exist.
if( typeof( TwinkleConfig ) == 'undefined' ) {
	TwinkleConfig = {};
}
 
/**
 TwinkleConfig.revertMaxRevisions (int)
 defines how many revision to query maximum, maximum possible is 50, default is 50
 */
if( typeof( TwinkleConfig.revertMaxRevisions ) == 'undefined' ) {
	TwinkleConfig.revertMaxRevisions = 50;
}
 
 
/**
 TwinkleConfig.userTalkPageMode may take arguments:
 'window': open a new window, remmenber the opened window
 'tab': opens in a new tab, if possible.
 'blank': force open in a new window, even if a such window exist
 */
if( typeof( TwinkleConfig.userTalkPageMode ) == 'undefined' ) {
	TwinkleConfig.userTalkPageMode = 'tab';
}
 
/**
 TwinkleConfig.openTalkPage (array)
 What types of actions that should result in opening of talk page
 */
if( typeof( TwinkleConfig.openTalkPage ) == 'undefined' ) {
	TwinkleConfig.openTalkPage = [ 'agf', 'norm', 'vand' ];
}
 
/**
 TwinkleConfig.openTalkPageOnAutoRevert (bool)
 Defines if talk page should be opened when canling revert from contrib page, this because from there, actions may be multiple, and opening talk page not suitable. If set to true, openTalkPage defines then if talk page will be opened.
 */
if( typeof( TwinkleConfig.openTalkPageOnAutoRevert ) == 'undefined' ) {
	TwinkleConfig.openTalkPageOnAutoRevert = false;
}
 
/**
 TwinkleConfig.openAOLAnonTalkPage may take arguments:
 true: to open Anon AOL talk pages on revert
 false: to not open them
 */
if( typeof( TwinkleConfig.openAOLAnonTalkPage ) == 'undefined' ) {
	TwinkleConfig.openAOLAnonTalkPage = false;
}
 
/**
 TwinkleConfig.summaryAd (string)
 If ad should be added or not to summary, default [[WP:TWINKLE|TWINKLE]]
 */
if( typeof( TwinkleConfig.summaryAd ) == 'undefined' ) {
	TwinkleConfig.summaryAd = " (Twinkle)";
}
 
/**
 TwinkleConfig.markRevertedPagesAsMinor (array)
 What types of actions that should result in marking edit as minor
 */
if( typeof( TwinkleConfig.markRevertedPagesAsMinor ) == 'undefined' ) {
	TwinkleConfig.markRevertedPagesAsMinor = [ 'agf', 'norm', 'vand', 'torev' ];
}
 
/**
 TwinkleConfig.watchRevertedPages (array)
 What types of actions that should result in forced addition to watchlist
 */
if( typeof( TwinkleConfig.watchRevertedPages ) == 'undefined' ) {
	TwinkleConfig.watchRevertedPages = [ 'agf', 'norm', 'vand', 'torev' ];
}
 
/**
 TwinkleConfig.offerReasonOnNormalRevert (boolean)
 If to offer a promt for extra summary reason for normal reverts, default to true
 */
if( typeof( TwinkleConfig.offerReasonOnNormalRevert ) == 'undefined' ) {
	TwinkleConfig.offerReasonOnNormalRevert = true;
}
 
 
// a list of usernames, usually only bots, that vandalism revert is jumped over, that is
// if vandalism revert is choosen on such username, then it's target in on the revision before.
// This is for handeling quick bots that makes edits seconds after the original edit is made.
// This only affect vandalism rollback, for good faith rollback, it will stop, indicating a bot 
// has no faith, and for normal rollback, it will rollback that edit.
var WHITELIST = [
'GOE',
'GOE2',
'GOE3',
]
 
var revertXML;
var contentXML;
var contentDoc;
var editXML;
var vandal;
var type;
var goodRev;
var nbrOfRevisions;
var curStatus;
var curVersion = true;
 
$( function() {
		if( QueryString.exists( 'twinklerevert' ) ) {
			twinkleAutoRevert();
		} else {
			addRevertButtons();
		}
	} );
 
function twinkleAutoRevert() {
	if( QueryString.get( 'oldid' ) != wgCurRevisionId ) {
		// not latest revision
		return;
	}
 
	var ntitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-ntitle' )[0];
	if( ntitle.getElementsByTagName('a')[0].firstChild.nodeValue != 'Revisão actual' ) {
		// not latest revision
		return;
	}
 
	vandal = ntitle.getElementsByTagName('a')[3].firstChild.nodeValue.replace("'", "\\'");
 
	if( !TwinkleConfig.openTalkPageOnAutoRevert ) {
		TwinkleConfig.openTalkPage = [];
	}
 
	return revertPage( QueryString.get( 'twinklerevert' ), vandal );
}
 
function addRevertButtons() {
 
	var spanTag = function( color, content ) {
		var span = document.createElement( 'span' );
		span.style.color = color;
		span.appendChild( document.createTextNode( content ) );
		return span;
	}
 
	if( wgNamespaceNumber == -1 && wgCanonicalSpecialPageName == "Contributions" ) {
		var list = document.getElementById('bodyContent').getElementsByTagName( 'ul' )[0].getElementsByTagName( 'li' );
		var vandal = document.getElementById('contentSub').getElementsByTagName( 'a' )[0].getAttribute( 'title' ).replace(/^User( talk)?:/ , '').replace("'", "\\'");
 
		var revNode = document.createElement('strong');
		var revLink = document.createElement('a');
		revLink.appendChild( spanTag( 'Black', ' [' ) );
		revLink.appendChild( spanTag( 'SteelBlue', 'reverter' ) );
		revLink.appendChild( spanTag( 'Black', ']' ) );
		revNode.appendChild(revLink);
 
		var revVandNode = document.createElement('strong');
		var revVandLink = document.createElement('a');
		revVandLink.appendChild( spanTag( 'Black', ' [' ) );
		revVandLink.appendChild( spanTag( 'Red', 'vandalismo' ) );
		revVandLink.appendChild( spanTag( 'Black', ']' ) );
		revVandNode.appendChild(revVandLink);
 
		for(var i = 0; i < list.length; ++i ) {
			var item = list[i].lastChild;
			if ( !item ) {
				continue;
			}
			if( userIsInGroup( 'Autoreviewer' ) ) {
				item = item.previousSibling;
			}
			if( item.nodeName != 'STRONG' ) {
				continue
			}
 
			var href = list[i].getElementsByTagName( 'a' )[1].getAttribute( 'href' );
			var tmpNode = revNode.cloneNode( true );
			tmpNode.firstChild.setAttribute( 'href', href + '&' + QueryString.create( { 'twinklerevert': 'norm' } ) );
			list[i].appendChild( tmpNode );
			var tmpNode = revVandNode.cloneNode( true );
			tmpNode.firstChild.setAttribute( 'href', href + '&' + QueryString.create( { 'twinklerevert': 'vand' } ) );
			list[i].appendChild( tmpNode );
		}
 
 
	} else {
 
		var otitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-otitle' )[0];
		var ntitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-ntitle' )[0];
 
		if( !ntitle ) {
			// Nothing to see here, move along...
			return;
		}
 
		if( !otitle.getElementsByTagName('a')[0] ) {
			// no previous revision available
			return;
		}
 
		// Lets first add a [edit this revision] link
		var query = new QueryString( decodeURI( otitle.getElementsByTagName( 'a' )[0].getAttribute( 'href' ).split( '?', 2 )[1] ) );
 
		var oldrev = query.get( 'oldid' );
 
		var oldEditNode = document.createElement('strong');
 
		var oldEditLink = document.createElement('a');
		oldEditLink.href = "javascript:revertToRevision('" + oldrev + "')";
		oldEditLink.appendChild( spanTag( 'Black', '[' ) );
		oldEditLink.appendChild( spanTag( 'SaddleBrown', 'reverter para esta versão' ) );
		oldEditLink.appendChild( spanTag( 'Black', ']' ) );
		oldEditNode.appendChild(oldEditLink);
 
		var cur = otitle.insertBefore(oldEditNode, otitle.firstChild);
		otitle.insertBefore(document.createElement('br'), cur.nextSibling);
 
		if( ntitle.getElementsByTagName('a')[0].firstChild.nodeValue != 'Revisão actual' ) {
			// not latest revision
			curVersion = false;
			return;
		}
 
		vandal = ntitle.getElementsByTagName('a')[3].firstChild.nodeValue.replace("'", "\\'");
 
		var agfNode = document.createElement('strong');
		var vandNode = document.createElement('strong');
		var normNode = document.createElement('strong');
 
		var agfLink = document.createElement('a');
		var vandLink = document.createElement('a');
		var normLink = document.createElement('a');
 
		agfLink.href = "javascript:revertPage('agf' , '" + vandal + "')"; 
		vandLink.href = "javascript:revertPage('vand' , '" + vandal + "')"; 
		normLink.href = "javascript:revertPage('norm' , '" + vandal + "')"; 
 
		agfLink.appendChild( spanTag( 'Black', '[' ) );
		agfLink.appendChild( spanTag( 'DarkOliveGreen', 'reverter (boa-fé)' ) );
		agfLink.appendChild( spanTag( 'Black', ']' ) );
 
		vandLink.appendChild( spanTag( 'Black', '[' ) );
		vandLink.appendChild( spanTag( 'Red', 'reverter (vândalo)' ) );
		vandLink.appendChild( spanTag( 'Black', ']' ) );
 
		normLink.appendChild( spanTag( 'Black', '[' ) );
		normLink.appendChild( spanTag( 'SteelBlue', 'reverter' ) );
		normLink.appendChild( spanTag( 'Black', ']' ) );
 
		agfNode.appendChild(agfLink);
		vandNode.appendChild(vandLink);
		normNode.appendChild(normLink);
 
		var cur = ntitle.insertBefore(agfNode, ntitle.firstChild);
		cur = ntitle.insertBefore(document.createTextNode(' || '), cur.nextSibling);
		cur = ntitle.insertBefore(normNode, cur.nextSibling);
		cur = ntitle.insertBefore(document.createTextNode(' || '), cur.nextSibling);
		cur = ntitle.insertBefore(vandNode, cur.nextSibling);
		cur = ntitle.insertBefore(document.createElement('br'), cur.nextSibling);
	}
 
}
 
function revertPage( pType, pVandal, rev, page ) {
 
	wgPageName = page || wgPageName;
	wgCurRevisionId = rev || wgCurRevisionId;
 
 
	try {
		vandal = pVandal;
		type = pType;
		Status.init( document.getElementById('bodyContent') );
 
		revertXML = sajax_init_object();
		revertXML.overrideMimeType('text/xml');
 
		var query = {
			'action': 'query',
			'prop': 'revisions',
			'titles': wgPageName,
			'rvlimit': TwinkleConfig.revertMaxRevisions,
			'rvprop': [ 'ids', 'timestamp', 'user', 'comment' ],
			'format': 'xml'
		}
 
		Status.status( 'A analisar' );
		revertXML.onreadystatechange = revertPageCallback;
		revertXML.open( 'GET' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?' + QueryString.create( query ), true );
		revertXML.send( null );
	} catch(e) {
		if( e instanceof Exception ) {
			Status.error( 'Erro: ' + e.what() );
		} else {
			Status.error( 'Erro: ' + e );
		}
	}
 
}
function revertPageCallback() {
 
	if ( revertXML.readyState != 4 ){
		Status.progress('.');
		return;
	} 
 
	if( revertXML.status != 200 ){
		Status.error('Ocorreu um erro');
		return;
	}
 
	var doc = revertXML.responseXML.documentElement;
 
	if( !doc ) {
		Status.error( 'Ocorreu possivelmente um erro ao obter o documento, a interromper a operação.' );
		return;
	}
	var revisions = doc.getElementsByTagName('rev');
	var top = revisions[0];
 
	if( top.getAttribute( 'revid' ) < wgCurRevisionId ) {
		Status.error( [ 'A última versão ', htmlNode( 'strong', top.getAttribute('revid') ), ' é menor que a versão analisada. Pode ser um sinal de que a versão voi eliminada, o servidor está a sofrer atrasos na actualização ou que o Twinkle recebeu dados errados. As ações serão interrompidas.' ] );
		return;
	}
	if( !top ) {
		Status.error( 'A última versão da página não foi encontrada. A página pode ter sido eliminada ou que ocorreu um erro na transmissão dos dados. A interromper a reversão ');
		return;
	}
 
 
	Status.status( [ 'Averig