Jump to content

User:Extorc/rmMaster.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.
function closeReq(info){
	console.log("clicked close request button")
	var api = new mw.Api();
	api.postWithToken("csrf", {action: 'edit', title: info.title, appendtext: info.text, summary: info.summary, section: 2} ).done(function( data ) {
		alert( 'Page edited!' );
	} ).fail( function(code, data) {
		console.log( api.getErrorMessage( data ).text());
	} );
}

function doSomethingWithText( wikitext ) {
	console.log(wikitext);
}
function doSomethingInCaseOfError () {console.log( 'err' );}

console.log("rmMaster init")
$('.mw-heading').each( function(index){                        //Look for each section in the page
		var $heading = $(this).find('span.mw-headline')        //Fetch for its text content
		if($heading.text().trim().includes("Requested move")){ //If it contains the words Requested move, which supposedly all RMdiscs would then continue
			console.log("found rmdisc")
			$(this).append("<button>Close Request</button>")           //Append a test Button
		}
		else{
			console.log($heading.text().trim())                //Otherwise print the content of the heading for debudding
		}
	}
);

$('button').click(function(){
	var user = 'User:' + mw.config.get( 'wgUserName' );
	closeReq({title: 'Wikipedia:Sandbox',text: '{{subst:RM top' + '|<b>Moved</b>}}',summary: '[[' + user + ']] closed request using rmCloser'});
	$.getJSON(mw.util.wikiScript('api'),
	{format: 'json', action: 'query', prop: 'revisions', rvprop: 'content', rvlimit: 1, titles: 'Wikipedia:Sandbox'}).then(function ( data ) {
		var page, wikitext;
		try {
			for ( page in data.query.pages ) {
				wikitext = data.query.pages[page].revisions[0]['*'];
				doSomethingWithText( wikitext );
			}
		} catch ( e ) {
			doSomethingInCaseOfError();
		}
	}).catch( doSomethingInCaseOfError );
})