মিডিয়াউইকি:Gadget-NopInserter.js

টীকা: সংরক্ষণ করার পর, পরিবর্তনসমূহ তৎক্ষণাৎ নাও দেখাতে পারে। আপনার ব্রাউজারের ক্যাশ কিভাবে এড়াবেন তা জানতে এখানে ক্লিক করুন।
  • ফায়ারফক্স / সাফারি: Shift ধরে রাখা অবস্থায়পুনঃলোড করুন-এ ক্লিক করুন, অথবা Ctrl-F5 বা Ctrl-R (ম্যাক-এ ⌘-R) চাপুন
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাক-এ ⌘-Shift-R) চাপুন
  • ইন্টারনেট এক্সপ্লোরার: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 চাপুন
  • অপেরা: মেনু → ব্যবস্থাপনাসমূহ-এ যান (ম্যাকে অপেরা → পছন্দসমূহ) এবং এরপর গোপনীয়তা ও সুরক্ষা → ব্রাউজিং-এর তথ্য পরিষ্কার করুন → ক্যাশে করা ছবি ও ফাইলগুলি

অন্যান্য ব্রাউজার সম্পর্কে বিশদ নির্দেশাবলীর জন্য, উইকিপিডিয়া:আপনার ক্যাশে বাইপাস করুন দেখুন।

//<source lang="javascript">
// 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]);

	if (match!==null) {
		alert('A trailing {{nop}} found on the previous page.');
		this.go_to_page();
	} 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;

	//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( 'csrfToken' )
		},
		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  = 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);
	}
});
//</source>