Jump to content

User:ZI Jony/MarkResolved.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
//<nowiki>
//Authored by DannyS712
//Maintained by ZI Jony
var EasyResolve = {};
window.EasyResolve = EasyResolve;

EasyResolve.summary = 'Marked as resolved';
EasyResolve.setup = function () {
	$('span.mw-editsection-bracket:first-child').each( function() {
		console.log( this );
		var sectionNumber = this.parentElement.childNodes[1].href.match( /action=edit&section=(\d+)/ )[1];
		console.log( sectionNumber );
		this.after( ' | ' );
		$(this).after( $( '<span class="EasyResolveClose" section=' + sectionNumber + '>Resolved</span>') );
	} );
	$('span.EasyResolveClose').click( function() {
		console.log( this );
		EasyResolve.close( this );
	} );
};
EasyResolve.close = function ( section ) {
	console.log( section );
	var sectionNumber = section.outerHTML.match( /section="(\d+)"/ )[1];
	var pageTitle = mw.config.get( 'wgPageName' );
	console.log( sectionNumber );
	new mw.Api().get( {
		action: 'parse',
		page: pageTitle,
		prop: 'wikitext',
		section: sectionNumber
	}).done( function( result ) {
		console.log( result );
		var wikitext = result.parse.wikitext['*'];
		wikitext = wikitext + '\n{{section resolved|1=~~~~}}';
		console.log( wikitext );
		new mw.Api().postWithEditToken( {
			action: 'edit',
			title: pageTitle,
			section: sectionNumber,
			text: wikitext,
			summary: EasyResolve.summary,
			notminor: true,
			nocreate: true
		}).done( function( result ) {
			console.log( result );
			if ( result && result.edit && result.edit.result && result.edit.result === 'Success' ){
				location.reload();
			}
		});
	});
};
mw.loader.using( 'mediawiki.api', function() {
	$(document).ready( function () {
		if ( $('#ca-addsection').length > 0 &&
			mw.config.get('wgAction') == 'view' &&
			( mw.config.get('wgNamespaceNumber') % 2 === 1 ||
				mw.config.get('wgNamespaceNumber') === 4
			)
			)
			{
			EasyResolve.setup();
		}
	});
} );
//</nowiki>