미디어위키:Gadget-NopInserter.js

참고: 설정을 저장한 후에 바뀐 점을 확인하기 위해서는 브라우저의 캐시를 새로 고쳐야 합니다.

  • 파이어폭스 / 사파리: Shift 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5 또는 Ctrl-R을 입력 (Mac에서는 ⌘-R)
  • 구글 크롬: Ctrl-Shift-R키를 입력 (Mac에서는 ⌘-Shift-R)
  • 인터넷 익스플로러 / 엣지: Ctrl 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5를 입력.
  • 오페라: 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', '#', '이전 페이지 {{nop}}', 't-check-prev-page-nop', '이전 페이지에서 {{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('이전 페이지에서 {{nop}}를 사용하고 있습니다.');
		this.go_to_page();
	} else {
		if (confirm('이전 페이지에서 {{nop}}를 사용하고 있지 않습니다. 추가할까요?')) { 
			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, '페이지 경계에서 단락을 구분하기 위해 {{nop}}를 추가함.');
};

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('이전 페이지를 찾을 수 없습니다: [[' + 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( '오류: API에서 다음 오류 코드를 반환했습니다. "' + data.error.code + '": ' + data.error.info );
			} else {
				alert( '오류: API로부터 알 수 없는 결과를 받았습니다.' );
			}
		},
		error: function( xhr ) {
			$.alert( '오류: 요청을 처리하는데 실패했습니다.' );
		}
	});
};

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' ) == 250 ) {
		var NopInserterInstance = new NopInserter(96);
	}
});
//</source>