User:DannyS712 test/undo.js

From 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.
// Install with:
// <code><nowiki>		{{subst:Iusc|User:DannyS712/Force EC.js}}																	</nowiki></code>
// or with
// <code><nowiki>		importScript( 'User:DannyS712/Force EC.js' ); // Backlink: [[User:DannyS712/Force EC.js]] 				</nowiki></code> 
//
// If forking this script, please note my contributions / give me credit
//<nowiki>
$(function() {
var EC_config = {
	name: '[[User:DannyS712/Force EC|Force EC.js]]',
	version: 1.0,
	debug: false
};
var url = window.location.href;
console.log( url );
if (url.indexOf('action=history') > -1){
	if (location.search.includes('action=history')) {
		var parent_ids = document.querySelectorAll('li[data-mw-revid]');
		var revid, rev_user, links;
		for (var iii = 0; iii < parent_ids.length; iii++) {
			revid = parent_ids[iii].getAttribute('data-mw-revid');
			if (revid && revid != mw.config.get('wgCurRevisionId')) {
				rev_user = parent_ids[iii].getElementsByClassName('history-user')[0].getElementsByTagName('a')[0].getAttribute('title').replace('User:','');
				var params = $.param({
					title: mw.config.get('wgPageName'),
					action: 'submit',
					summary: 'Partially undo edits starting from revision ' + revid,
					from_old: revid
				});
				links = parent_ids[iii].getElementsByClassName('mw-changeslist-links');
				links[links.length - 1].innerHTML += '<span><a href="/w/index.php?' + params + '">Partially undo</a></span>';
			}
		}
	}
} else if (url.indexOf('submit') > -1 && url.indexOf('from_old') > -1){
	var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
	var revid = url.replace(/.*from_old=/, '');
	var old_content = get_page( revid );
	var initial_content = '{{subst:void|LEAVE THIS LINE ALONE, IT FORCES THE EDIT CONFLIC}}' + old_content.replace(/=\n/g, '=\n{{subst:void|}}') + '\n{{subst:void|LEAVE THIS LINE ALONE, IT FORCES THE EDIT CONFLIC}}';
	var old_time = get_rev( revid );
	console.log( revid, old_time );
	$('input[name=wpStarttime]').val(old_time);
	$('input[name=wpEdittime]').val(old_time);
	$('input[name=editRevId]').val(revid);
	$('input[name=baseRevId]').val(revid);
	$('input[name=parentRevId]').val(revid);
	$('#wpTextbox1').val(initial_content);
}
function get_page( old_id ){
	var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
	console.log( old_id );
	var page_to_get = {
        action: 'parse',
        oldid: old_id,
        prop: 'wikitext',
        format: 'json',
        formatversion: 2
    };
    var result = null;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: page_to_get,
		dataType: 'json',
		async: false,
		success: function(page) {
			console.log( page );
			var text = page.parse.wikitext;
			//console.log( text );
			result = text;
		} 
	});
	return result;
}
function get_rev( rev ){
	var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
	console.log( rev );
	var rev_to_get = {
        action: 'query',
        prop: 'revisions',
        titles: mw.config.get('wgPageName'),
        rvprop: 'timestamp',
        rvstartid: rev,
        rvlimit: 1,
        format: 'json',
        formatversion: 2
    };
    var result = null;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: rev_to_get,
		dataType: 'json',
		async: false,
		success: function(rev_info) {
			console.log( rev_info );
			var rev_time = rev_info.query.pages[0].revisions[0].timestamp.replace(/[^0-9]/g, '');
			console.log( rev_time );
			result = rev_time;
		} 
	});
	return result;
}
});
//</nowiki>