Jump to content

User:Ale jrb/Scripts/userhist.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ale jrb (talk | contribs) at 10:49, 8 September 2009 (test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
	function userHistMain() {
		this.init = function() {
			if ( wgAction == 'history' ) {
				this.displayBox();
			}
		}
		
		this.displayBox = function() {
			var box = document.createElement('input');
			box.setAttribute('id', 'userhist-isolate');
			box.setAttribute('type', 'text');
			box.setAttribute('width', '200px');
			
			var button = document.createElement('input');
			button.setAttribute('type', 'button');
			button.setAttribute('value', 'isolate history');
			
			if (button.addEventListener) { 
				button.addEventListener('click', userHist.getUserHist(document.getElementById('userhist-isolate').value), false); 
			} 
			else 
			{ 
				button.attachEvent('onclick', userHist.getUserHist(document.getElementById('userhist-isolate').value)); 
			}
			
			
			var div = document.createElement('div');
			div.appendChild(box);
			div.appendChild(button);
			
			document.getElementById('mw-history-searchform').appendChild(div);
		}
		
		this.getUserHist = function(user) {
			var api = 'http://en.wikipedia.org/w/api.php';
			if (typeof user == 'undefined') return false;
			if (!user) return false;
			
			user = user.replace(/ /g, '_');
			
			var apiLink = '?action=query&format=xml&prop=revisions&titles='+wgPageName+'&rvprop=ids|timestamp|flags|comment|user|size&rvlimit=500&rvuser='+user+'';
			
			this.req 				= new wa_ajaxcall();
			this.req.requestUrl		= apiLink;
			this.req.get			(function() {
										userHist.data = userHist.req.response;
										userHist.showUserHist();
										return true;
									});
		}
		
		this.showUserHist = function() {
			var data = this.data;
			
			if (data.getElementsByTagName('rev').length <= 0) {
				this.showError('That user has not edited this page');
				return false;
			}
			
			// get output
			var output = [];
			for (var i = 0; i < data.getElementsByTagName('rev').length; i ++) {
				var dataset = data.getElementsByTagName('rev')[i];
				
				output[i] = [];
				output[i][0] = dataset.getAttribute('oldid'); // oldid
				output[i][1] = dataset.getAttribute('user'); // user
				output[i][2] = dataset.getAttribute('timestamp'); // timestamp
				output[i][3] = dataset.getAttribute('comment'); // comment
				output[i][4] = dataset.getAttribute('size'); // size
			}
			
			// remove useless interface
			var histPar = document.getElementById('mw-history-compare');
			histPar.innerHTML = '';
			
			// build our own interface
			var newInt = '<ul id="pagehistory">';
			for (var i = 0; i < output.length; i ++) {
				var url = 'http://en.wikipedia.org/w/title='+wgArticleName;
				newInt += '<li class="">(<a href="'+url+'&oldid='+output[i][0]+'&diff=cur">cur</a>) (<a href="'+url+'&oldid='+output[i][0]+'&diff=prev">prev</a>) <a href="'+url+'&oldid='+output[i][0]+'">'+this.convertTimestamp(output[i][2])+'</a> <span class="historyuser"><a href="/wiki/User:'+output[i][1]+'">'+output[i][1]+'</a></span> <span class="historysize">('+output[i][4]+' bytes)</span> <span class="comment">('+this.parseComment(output[i][3])+')</span></li>' + "\n";
			}
			newInt += '</ul>';
			
			histPar.innerHTML = newInt;
		}
		
		this.showError = function(error) {
			alert(error);
		}
		
		this.convertTimestamp = function(timestamp) {
			return timestamp;
		}
		
		this.parseComment = function(comment) {
			return comment;
		}
	}

	importScript('User:Ale_jrb/Scripts/waLib.js');
	var userHist = new userHistMain();
	hookEvent('load', userHist.init);