Jump to content

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

SRG.setup = function () {
	$('span.mw-editsection-bracket:first-child').each( function() {
		console.log( this );
		try {
			var sectionNumber = this.parentElement.childNodes[1].href.match( /action=edit&section=(\d+)/ )[1];
			console.log( sectionNumber );
			this.after( ' | ' );
			$(this).after( $( '<a href="#" class="SRGHelper-mark-done" section=' + sectionNumber + '>Mark as done</a>') );
		} catch ( e ) {
			
		}
	} );
	$('a.SRGHelper-mark-done').click( function(e) {
		e.preventDefault();
		console.log( this );
		SRG.close( this );
	} );
};
SRG.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.replace( /{{Status(?:\|)?}}/i, '{{Status|done}}' );
		wikitext = wikitext + '\n:{{done}} ~~~~';
		console.log( wikitext );
		new mw.Api().postWithEditToken( {
			action: 'edit',
			title: pageTitle,
			section: sectionNumber,
			text: wikitext,
			summary: 'Mark a section as done (with [[User:YeYen KY/SRG.js|SRG.js]])',
			minor: false,
			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>