सामग्री पर जाएँ

मीडियाविकि:Gadget-NopInserter.js

विकिस्रोत से

ध्यान दें: प्रकाशित करने के बाद बदलाव देखने के लिए आपको अपने ब्राउज़र के कैश को हटाना पड़ सकता है।

  • Firefox/Safari: Reload क्लिक समय Shift दबाएँ, या फिर Ctrl-F5 या Ctrl-R दबाएँ (Mac पर ⌘-R)
  • Google Chrome: Ctrl-Shift-R दबाएँ (Mac पर ⌘-Shift-R)
  • Internet Explorer/Edge: Refresh पर क्लिक करते समय Ctrl दबाएँ, या Ctrl-F5 दबाएँ
  • Opera: Ctrl-F5 दबाएँ।
// 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 {{nop}},
// 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(mw.config.get( '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', '#', 'Prev. page {{nop}}', 't-check-prev-page-nop', 'Check if the previous page ends in a newline-{{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 = /<noinclude>([\s\S]*)<\/noinclude>([\s\S]*?)<noinclude>([\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 '<noinclude>'+parts[1]+'</noinclude>'+parts[2]+'<noinclude>' + parts[3] +'</noinclude>';
};

NopInserter.prototype.check_nop = function(parts) {
	var match = /\n{{[Nn]op}}\s*$/.exec(parts[2]);
	var prefs = mw.config.get('userjs-nopinserter') || {};

	if (match!==null) {
		alert('A trailing {{nop}} found on the previous page.');
		this.go_to_page();
	} else {
		if (prefs.dontConfirmNopAddition === true) {
			this.add_trailing_nop(parts); // Don't prompt if explicitly requested not to.
		} else {
			if (confirm('No trailing {{nop}} was found on the previous page. Add one?')) {
				this.add_trailing_nop(parts);
			}
		}
	}
};

NopInserter.prototype.add_trailing_nop = function(parts) {
	//remove trailing space and add the nop
	parts[2] = parts[2].replace(/\s*$/i,'') + '\n{{nop}}';

	var new_text = this.assemble_page_parts(parts);

	this.save_page(this.prev_page, new_text, 'Adding trailing {{nop}} to break paragraph at the page boundary.');
};

NopInserter.prototype.get_raw_page = function(pagetitle, callback) {
	var myself = this,
		api = new mw.Api();

	//get the current page text
	api.get( {
	    action: 'parse',
		page: pagetitle,
		prop: 'wikitext'
	} ).done( function ( data ) {
		var parts = myself.get_page_parts(data.parse.wikitext['*']);
		$.proxy(callback, myself)(parts);
	} ).fail( function () {
		alert('Previous page could not be loaded: [[' + pagetitle + ']]');
	} );
};

NopInserter.prototype.save_page = function(title, content, summary) {
	var myself = this;

	var prefs = mw.config.get('userjs-nopinserter') || {};
	var highlightCSS = "";
	var resetCSS = "";
	var resetTimeout = prefs.notificationTimeout ? prefs.notificationTimeout : 2000;

	if (prefs.notificationStyle === "highlight") {
		highlightCSS = {'background-color': 'palegreen'};
		resetCSS = {'background-color': ''};
	} else {
		highlightCSS = {'outline': '2px solid green'};
		resetCSS = {'outline': ''};
	}

	$.ajax({
		url: mw.util.wikiScript( 'api' ),
		data: {
			format: 'json',
			action: 'edit',
			title: title,
			summary: summary,
			text: content,
			token: mw.user.tokens.get( 'csrfToken' )
		},
		dataType: 'json',
		type: 'POST',
		success: function( data ) {
			if ( data && data.edit && data.edit.result == 'Success' ) {
				if (prefs.notificationStyle === "none") {
					// No notification at all.
				}
				else if (prefs.notificationStyle === "message") {
					mw.notify(
						"A {{nop}} was added to the preceding page.",
						{title: '{{nop}} added', type: 'info', tag: 'NopInserterNopAdded'}
					);
				}
				else { // Defaults to "highlight".
					$('#ca-proofreadPagePrevLink').children().css(highlightCSS);
					setTimeout(function() {
						$('#ca-proofreadPagePrevLink').children().css(resetCSS);
					}, resetTimeout);
				}
				myself.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  = mw.config.get('wgArticlePath').replace('$1', this.prev_page);
	}
};

$(document).ready( function() {
	//Page namespace only
	if( mw.config.get( 'wgNamespaceNumber' ) == 104 ) {
		var NopInserterInstance = new NopInserter(96);
	}
});