User:ExtorcDev/rmMaster.js

This is an old revision of this page, as edited by ExtorcDev (talk | contribs) at 14:07, 18 May 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
$('.mw-heading').each(function(index){
	var $heading = $(this).find('span.mw-headline');
	if($heading.text().includes('Requested move')){
		$(this).append('<br><button class="closeButton">Close</button><button class="relistButton">Relist</button>');
	}
	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(){
		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]['*'];
				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 = section.replace(/{{requested move.*/g,'{{subst:' + 'RM top|' + closeMSG + '|nac=yes}}');
						result = result.replace(sectionLog[i+1], updatedSection);
						console.log(result);
						
						editPage(url, result);	
					}
				}
			}
		} );
	});
	$('.relistButton').click(function(){
		console.log("Relisted");
		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]['*'];
				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');
						}
						else{
							console.log('Never relisted before');
						}
						// section = section + '\n{{subst:' + 'RM bottom}}\n';
						// var closeMSG = window.prompt("Close Message : ");
						// var 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);	
					}
				}
			}
		} );
	});
});
function editPage( url , result ) {
	var api2 = new mw.Api();
	api2.postWithToken("csrf", editParams(url, 'Closed') ).done(function( data ) {
		alert( 'Page edited!' );
	} ).fail( function(code, data) {
		console.log( api.getErrorMessage( data ).text());
	} );
}

function queryParams(url){ return { action: 'query', prop: 'revisions', rvprop: 'content', rvlimit: 1, titles: url }; }
function editParams(url, action){ return { action: 'edit', title: url, text: result, summary: + action + ' Request using [[User:ExtorcDev/rmMaster.js|rmMaster]]' }; }