User:DixonD/NopInserter.js

// Inserts a NOP on the previous Page: page. // Complain to User:Inductiveload // Release 1.0	- 2012-04-23 - initial release //         1.1 - 2012-04-25 - add user option to auto advance // // To automatically advance to the edited page after adding a , // add the following to your user JS: // // //Automatically advance to the previous page after checking for nops // mw.user.options.set({'nopinserter_auto_advance':true});

NopInserter = function{ var myself=this; var match = /(.*?)\/([0-9]+)/.exec( wgPageName ); if (match == null ){ return } var basename = match[1]; var pagenum = parseInt(match[2]); if (pagenum < 1 ){ return } this.prev_page = basename + '/' + (pagenum - 1); var portletLink = mw.util.addPortletLink( 'p-tb', '#',			' на попер. сторінці', 't-check-prev-page-nop', 'Перевірка чи попередня сторінка закінчується на .'	); $( portletLink ).click( function ( e ) {		e.preventDefault;		myself.get_raw_page(myself.prev_page, myself.check_nop);	}); }

NopInserter.prototype.get_page_parts = function(data){ //split up the page into header, body and footer var match = / ([\s\S]*)<\/noinclude>([\s\S]*?) ([\s\S]*)<\/noinclude>/m.exec(data); return match; }

NopInserter.prototype.assemble_page_parts = function(parts){ //re-assemble a page from the header, body and footer return ' '+parts[1]+' '+parts[2]+' ' + parts[3] +' '; }

NopInserter.prototype.check_nop = function(parts){ var match = /\s*$/.exec(parts[2]); if (match!=null){ alert('На попередній сторінці вже є .'); this.go_to_page; } else { if (confirm('На попередній сторінці немає. Додати?')) { this.add_trailing_nop(parts); }	} }

NopInserter.prototype.add_trailing_nop = function(parts){

parts[2] = parts[2].replace(/\s*$/i,'') + '\n'; //remove trailing space and add the nop var new_text = this.assemble_page_parts(parts); this.save_page(this.prev_page, new_text, 'Adding trailing to break paragraph at the page boundary.'); }

NopInserter.prototype.get_raw_page = function(pagetitle, callback){ var myself = this; //get the current page text $.ajax({		url: mw.util.wikiScript( 'index' ),		data: {			action: 'raw',			title: pagetitle,		},		cache:false,		success:function(data){			var parts = myself.get_page_parts(data);			$.proxy(callback, myself)(parts);		},		error:function(jqXHR, textStatus, errorThrown){			alert('Previous page could not be loaded: ' + pagetitle + '');		},	}); } NopInserter.prototype.save_page = function(title, content, summary){ var myself = this; $.ajax({		url: mw.util.wikiScript( 'api' ),		data: {			format: 'json',			action: 'edit',			title: title,			summary: summary,			text: content,			token: mw.user.tokens.get( 'editToken' )		},		dataType: 'json',		type: 'POST',		success: function( data ) {			if ( data && data.edit && data.edit.result == 'Success' ) {				$('#ca-prev').css({'outline':'2px solid green'});				setTimeout(function { $('#ca-prev').css({'outline':''}); }, 2000);				this.go_to_page;			} else if ( data && data.error ) {				alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );			} else {				alert( 'Error: Unknown result from API.' );			}		},		error: function( xhr ) {			$.alert( 'Error: Request failed.' );		}	}); }

NopInserter.prototype.go_to_page = function{ //go to the previous page if the user's options specify to do so	if ( mw.user.options.exists( 'nopinserter_auto_advance' ) && mw.user.options.get('nopinserter_auto_advance') ){ window.location = wgArticlePath.replace('$1', this.prev_page); } }

$(document).ready( function{	if( wgNamespaceNumber==104 ) { //pagespace only		var NopInserterInstance = new NopInserter(96);	} });