User:BPositive/getPageContent.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
//This UserScript returns the revision before FuzzyBot's edit on the page, if at all it exists

mw.loader.using( ['jquery.ui', 'mediawiki.api'], function () {
	
	function getDiff( pageName, pageContent ) {
		var api = new mw.Api();

		return api.post( {
			action:'query',
			prop: 'revisions',
			format: 'json',
			rvprop: 'content',
			rvlimit: '1',
			titles: pageName,
			rvdifftotext: pageContent
		} ).then( function ( data ) {
			var obj, diff;
			for ( var page in data.query.pages ) {
				obj = data.query.pages[page];
			}
			diff = obj.revisions[0].diff['*'];
			console.log(diff);
			return diff;
		} ).promise();
	}
	
	
	//Function to show the content in a dialog box
	function renderContentDialog ( content ) {
		var $dialog = $( '<div></div>' )
			.text ( content )
			.dialog ({
				autoOpen: true,
				title: 'Revision before FuzzyBot\'s edit on ' + wgPageName,
				width: '70%',
				modal: true
			});
	}
	
	//Function to get the content of the page opened
    function getContent(){
	    var api_1 = new mw.Api();
	    //This api call returns the timestamp of FuzzyBot's edit
	    api_1.get ({
	        action:'query',
	        prop: 'revisions',
	        format: 'json',
	        rvprop: 'timestamp',
	        rvuser: 'FuzzyBot',
	        rvdir: 'newer',
	        titles: wgPageName
	    }).done ( function( data ) {
	    	//FB = FuzzyBot
	    	if ( typeof data['query']['pages'][wgArticleId]['revisions'] === 'undefined' ) {	// variable is undefined
    			renderContentDialog("No edit by FuzzyBot on this page");
			}
			else {
		    	var timestampFB = data['query']['pages'][wgArticleId]['revisions'][0]['timestamp'];
		        console.log ( "Timestamp for FuzzyBot's revision: " + timestampFB );
		        var dateFB = new Date( timestampFB );
		        dateFB.setSeconds( dateFB.getSeconds() - 1 );
		        var timestampOld = dateFB.toISOString();
		        console.log( "New Timestamp: " + timestampOld );
		        
		        var api_2 = new mw.Api();
		        //This api call returns the content before FuzzyBot's edit
			    api_2.get ({
			        action:'query',
			        prop: 'revisions',
			        format: 'json',
			        rvprop: 'content',
			        rvstart: timestampOld,
			        titles: wgPageName
			    }).done ( function( data ) {
			    	console.log(data['query']['pages'][wgArticleId]['revisions'][0]['*'].split('\n\n'));
			    	renderContentDialog(data['query']['pages'][wgArticleId]['revisions'][0]['*']);
			    });
			}
	    });
    }
    
    $(document).ready( function() {
    	
    	getDiff('Communication', 'aaaaaaa');
		// Add a link to the toolbox
		var link = mw.util.addPortletLink(
			'p-tb',
			'#',
			'Get content',
			't-prettylinkwidget',
			'Get revision before FuzzyBot\'s edit',
			null,
			'#t-whatlinkshere'
		);
		
		$(link).click( function( e ) {
			// Avoid the browser going to '#'
			e.preventDefault();
			// getContent();
		});
	});
});