Jump to content

User:DannyS712/SRG.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>
$( function () {
var SRG = {};
window.SRGHelper = SRG;

SRG.summary = ' - with SRG.js';
SRG.setup = function () {
	$('span.mw-editsection-bracket:first-child').each( function() {
		try {
			var sectionNumber = this.parentElement.childNodes[1].href.match( /action=edit&section=(\d+)/ )[1];
			this.after( ' | ' );
			$(this).after( $( '<a href="#" class="SRGHelper-add-comment" section=' + sectionNumber + '>Add a comment</a>') );
			this.after( ' | ' );
			$(this).after( $( '<a href="#" class="SRGHelper-mark-not-done" section=' + sectionNumber + '>Mark as not done</a>') );
			this.after( ' | ' );
			$(this).after( $( '<a href="#" class="SRGHelper-mark-done" section=' + sectionNumber + '>Mark as done</a>') );
			this.after( ' | ' );
			$(this).after( $( '<a href="#" class="SRGHelper-mark-stale" section=' + sectionNumber + '>Mark as stale</a>') );
		} catch ( e ) {
			
		}
	} );
	$('a.SRGHelper-mark-done').click( function(e) {
		e.preventDefault();
		console.log( this );
		SRG.doEdit( this, '{{done}}', 'Marking one request as done', '{{Status|done}}' );
	} );
	$('a.SRGHelper-mark-not-done').click( function(e) {
		e.preventDefault();
		console.log( this );
		SRG.doEdit( this, '{{not done}}', 'Marking one request as not done', '{{Status|not done}}' );
	} );
	$('a.SRGHelper-add-comment').click( function(e) {
		e.preventDefault();
		console.log( this );
		var commentText = window.prompt( 'Comment to add (will be indented once automatically)');
		if ( commentText && commentText !== '' && commentText !== null ) {
			SRG.doEdit( this, commentText, 'Adding a comment', false );
		}
	} );
	$('a.SRGHelper-mark-stale').click( function(e) {
		e.preventDefault();
		console.log( this );
		SRG.doEdit( this, '{{not done}} {{stale}}', 'Marking one request as not done - stale', '{{Status|notdone}}' );
	} );
};
SRG.doEdit = function ( section, comment, editSummary, newStatus ) {
	console.log( section, comment, editSummary, newStatus );
	
	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['*'];
		if ( newStatus !== false ) {
			wikitext = wikitext.replace( /{{Status(?:\|)?}}/i, newStatus );
		}
		wikitext = wikitext + '\n::' + comment + ' ~~~~';
		console.log( wikitext );
		new mw.Api().postWithEditToken( {
			action: 'edit',
			title: pageTitle,
			section: sectionNumber,
			text: wikitext,
			summary: editSummary + SRG.summary,
			minor: 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 ( mw.config.get('wgAction') === 'view' && (
				mw.config.get('wgArticleId') === 10769705 ||
				mw.config.get('wgArticleId') === 130130
			)
		) {
			window.SRGHelper.setup();
		}
	});
} );
// </nowiki>