User:AzaToth/twinklediff.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

function twinklediff() { 
	if( wgNamespaceNumber < 0 ) {
		return;
	}
 
	var query = {
		'title': wgPageName,
		'diff': 'cur',
		'oldid': 'prev'
	};
 
	addPortletLink( 'p-cactions' , wgServer + wgScriptPath + '/index.php?' + QueryString.create( query ), 'last', 'tw-lastdiff', 'Show most recent diff' );
 
	if( !QueryString.exists( 'diff' ) ) {
		// Not diff page
		return;
	} 
 
 
	var l = addPortletLink( 'p-cactions', "javascript:twinklediff.evaluate(false);", 'since', 'tw-since', 'Show difference between last diff and the revision made by previous user' );
 
	var l = addPortletLink( 'p-cactions', "javascript:twinklediff.evaluate(true);", 'since mine', 'tw-sincemine', 'Show difference between last diff and my last revision' );
}
addOnloadHook( twinklediff );
 
twinklediff.evaluate = function twinklediffEvaluate(me) {
	var ntitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-ntitle' )[0];
 
	var user;
	if( me ) {
		user = wgUserName;
	} else {
		var node = document.getElementById( 'mw-diff-ntitle2' );
		if( ! node ) {
			// nothing to do?
			return;
		}
		user = document.evaluate( 'a[1]', node, null, XPathResult.STRING_TYPE, null ).stringValue;
	}
	var query = {
		'prop': 'revisions',
		'action': 'query',
		'titles': wgPageName,
		'rvlimit': 1, 
		'rvprop': [ 'ids', 'user' ],
		'rvstartid': wgCurRevisionId - 1, // i.e. not the current one
		'rvuser': user
	};
	Status.init( document.getElementById('bodyContent') );
	var wikipedia_api = new Wikipedia.api( 'Grabbing data of initial contributor', query, twinklediff.callbacks.main );
	wikipedia_api.params = { user: user };
	wikipedia_api.post();
}
 
twinklediff.callbacks = {
	main: function( self ) {
		var xmlDoc = self.responseXML;
		var revid = xmlDoc.evaluate( '//rev/@revid', xmlDoc, null, XPathResult.NUMBER_TYPE, null ).numberValue;
 
		if( ! revid ) {
			self.statelem.error( 'no suitable earlier revision found, or ' + self.params.user + ' is the only contributor. Aborting.' );
			return;
		}
		var query = {
			'title': wgPageName,
			'oldid': revid,
			'diff': wgCurRevisionId
		};
		window.location = wgServer + wgScriptPath + '/index.php?' + QueryString.create( query );
	}
}