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.
//<nowiki>
$('<br><button class="closeButton">Close</button><button class="relistButton">Relist</button>').insertBefore('.tmbox-move');
$('<button class="rmTechButton">Raise Technical Move request</button>').insertBefore('.mw-archivedtalk');

var url = window.location.href.match(/Talk.*$/gm).toString().replace(/_/g, ' ');
if(url.match(/#/gm)){
	url = url.toString().replace(/#.*$/gm,'');
}
console.log(url);
$('.closeButton').click(function(){
	queryPage(function(result){
		sectionLog = result.split("==");
		for(let i = 0; i < sectionLog.length; i++){
			if(sectionLog[i].match(/Requested move \d/gm) && sectionLog[i+1].match(/{{requested move.*/g)){
				var section = sectionLog[i+1];
				section = section + '\n{{subst:RM bottom}}\n';
				var closeMSG = window.prompt("Close Message : ");
				var updatedSection;
				if(section.match(/multiple=yes/gm)){
					updatedSection = section.replace(/{{requested move.*multiple=yes\n.*/g,'{{subst:RM top|' + closeMSG + '|nac=yes}}');
				}
				else{
					updatedSection = section.replace(/{{requested move.*/g,'{{subst:RM top|' + closeMSG + '|nac=yes}}');
				}
				result = result.replace(sectionLog[i+1], updatedSection);
				console.log(result);
				editPage(url, result, 'Closed');
			}
		}	
	});
});

$('.relistButton').click(function(){
	console.log("Relisted");
	queryPage(function(result){
		sectionLog = result.split("==");
		for(let i = 0; i < sectionLog.length; i++){
			if(sectionLog[i].match(/Requested move \d/gm) && sectionLog[i+1].match(/{{requested move.*/g)){
				var section = sectionLog[i+1];
				console.log(section);
				if(section.match(/'''''Relisting.*>/gm)){
					console.log('Already relisted atleast once');
					var prevRelists = section.match(/'''''Relisting.*>/);
					var newRelists = prevRelists + '{{subst:relisting}}';
					var updatedSection = section.replace(prevRelists, newRelists);
					result = result.replace(section, updatedSection);
					console.log(result);
					editPage(url, result, 'Relisted');
				}
				else{
					var updatedSection = section.replace(section.match(/.UTC./m),'(UTC){{subst:relisting}}');
					result = result.replace(section, updatedSection);
					console.log(result);
					editPage(url, result, 'Relisted');
				}
			}
		}
	});
});

$('.rmTechButton').click(function(){
	queryPage(function(result){
		sectionLog = result.split("==");
		for(let i = 0; i < sectionLog.length; i++){
			if(sectionLog[i].match(/Requested move \d/gm)){
				var section = sectionLog[i+1];
				var moveFrom = window.prompt("Enter Move Source : ");
				var moveTo = window.prompt("Enter Move Destination : ");
				var reason = window.prompt("Request reason : ");
				var techReq = '{{subst:RMassist|'+ moveFrom +'|' + moveTo + '|reason=' + reason + '}}';
				
				var api2 = new mw.Api();
				api2.get( queryParams('Wikipedia:Requested moves/Technical requests') ).done( function ( data ) {
				    var page2;
					for (page2 in data.query.pages){
						result2 = data.query.pages[page2].revisions[0]['*'];
						var newMatch = result2.match(/Uncontro.*====\n.*>\n<!--.*/gm).toString() + '\n' + techReq;
						console.log(newMatch);
						result2 = result2.replace(/Uncontro.*====\n.*>\n<!--.*/gm, newMatch);
						console.log(result2);
						editPage('Wikipedia:Requested moves/Technical requests', result2, 'Raised Technical Request');
					}
				});
			}
		}	
	});
});

function editPage( loc , result , action) {
	var api2 = new mw.Api();
	api2.postWithToken("csrf", editParams(loc, result, action) ).done(function( data ) {
		alert( 'Page edited!' );
	} ).fail( function(code, data) {
		console.log( api.getErrorMessage( data ).text());
	} );
}

function queryParams(loc){ return { action: 'query', prop: 'revisions', rvprop: 'content', rvlimit: 1, titles: loc }; }
function editParams(loc, content, action){ return { action: 'edit', title: loc, text: content, summary: action + ' Request using [[User:ExtorcDev/rmMaster.js|rmMaster]]' }; }
function queryPage(onQuery){
	var api = new mw.Api();
	api.get( queryParams(url) ).done( function ( data ) {
	    var page;
		for (page in data.query.pages){
			result = data.query.pages[page].revisions[0]['*'];
			onQuery(result);
		}
	});
}
//</nowiki>