User:SD0001/quickViewDeleted.js

Source: Wikipedia, the free encyclopedia.
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.
/*
    Quickly view a preview of the last deleted revision of a deleted page.
    The preview is shown at the bottom of the page. 
    
    To enable, add the line below to your common.js page:
    importScript('User:SD0001/quickViewDeleted.js'); // [[User:SD0001/quickViewDeleted.js]]
*/

$.when(
	$.ready,
	mw.loader.using('mediawiki.api')
).then(function() {
	if (!$('.mw-undelete-subtitle').length || mw.config.get('wgCurRevisionId') || mw.config.get('wgUserGroups').indexOf('sysop') === -1) {
		return;
	}
	var api = new mw.Api();
	api.get({
		"action": "query",
		"format": "json",
		"prop": "deletedrevisions",
		"titles": mw.config.get('wgPageName'),
		"formatversion": "2",
		"drvprop": "content",
		"drvlimit": "1"
	}).then(function(json) {
		var wikitext = json.query.pages[0].deletedrevisions[0].content;
		wikitext = '=Last deleted version=\n' + wikitext;
		return api.post({
			"action": "parse",
			"format": "json",
			"title": mw.config.get('wgPageName'),
			"text": wikitext,
			"prop": "text",
			"disableeditsection": 1,
			"formatversion": "2"
		});
	}).then(function(json) {
		var html = json.parse.text;
		$('#mw-content-text').append(
			$('<div>').attr('id', 'quickViewDeleted-text').html(html)
		);
		$('.mw-undelete-subtitle').append(
			$('<span>').css({
				'float': 'right'
			}).append(
				$('<a>').text('Jump to preview >').attr('href', '#quickViewDeleted-text')
			)
		);
	}).catch(console.error);
});