미디어위키:Gadget-Prosesize.js

/* * Prosesize- Documentation at en.wikipedia.org/wiki/Wikipedia:Prosesize- Rewrite of User:Dr_pda/prosesize.js.
*/
'use strict';
( function () {
	function sizeFormatter( size ) {
		var nbsp = "\xA0"; // Equivalent to  
		/		if ( size > 10240 ) {
			return ( Math.round( size / 1024 ) + nbsp + 'kB' );
		} else {
			return ( size + nbsp + '바이트' );
		}
		*/
		return ( size + nbsp + '바이트' );
	}
	function sizeElement( id, text, size, extraText ) {
		return $(  )
			.prop( 'id', id )
			.append(
				$(  ).text( text ),
				document.createTextNode( ' ' + sizeFormatter( size ) + ( extraText 
) )
			);
	}
	function getRevisionSize( proseValue ) {
		var Api = new mw.Api();
		function appendResult( size ) {
			var wikiValue = sizeElement( 'wiki-size', '위키텍스트:', size );
			proseValue.before( wikiValue );
		}
		if ( mw.config.get( 'wgAction' ) === 'submit' ) {
			// Get size of text in edit box
			// eslint-disable-next-line no-jquery/no-global-selector
			appendResult( $( '#wpTextbox1' ).textSelection( 'getContents' ).length );
		} else if ( mw.config.get( 'wgIsArticle' ) ) {
			// Get revision size from API
			Api.get( {
				action: 'query',
				prop: 'revisions',
				rvprop: 'size',
				revids: mw.config.get( 'wgRevisionId' ),
				formatversion: 2
			} ).then( function ( result ) {
				appendResult( result.query.pages 0 .revisions 0 .size );
			} );
		}
	}
	function getFileSize( proseHtmlValue ) {
		// HTML document size not well defined for preview mode or section edit
		if ( mw.config.get( 'wgAction' ) !== 'submit' ) {
			$.get( location ).then( function ( result ) {
				var fsize = sizeElement( 'total-size', 'HTML 문서 길이:', result.length );
				proseHtmlValue.before( fsize );
			} );
		}
	}
	function getLength( id ) {
		var i;
		var textLength = 0;
		for ( i = 0; i ' )
			.prop( 'id', 'document-size-header' )
			.html( '문서 통계 (추가 정보):' );
		var output = $(  )
			.prop( 'id', 'document-size-stats' );
		var combined = $(  )
			.prop( 'id', 'document-size' )
			.append( header, output );
		if ( parserOutput.length === 0 ) {
			return;
		}
		if ( prevStats.length ) {
			// If statistics already exist, turn them off and remove highlighting
			prevStats.remove();
			prevHeader.remove();
			parserOutput.children( 'p' ).removeClass( 'prosesize-highlight' );
		} else {
			// Use prosesize API to get a more accurate prose size account
			// The calculations below are left in for the highlighting
			prosePromise = $.getJSON( 'https://prosesize.toolforge.org/api/' + mw.config.get( 'wgServerName' ) + '/'
				+ encodeURIComponent( mw.config.get( 'wgPageName' ) ) + '?revision=' + mw.config.get( 'wgRevisionId' ) );
			// Calculate prose size and size of reference markers (1 etc)
			parserOutput.children( 'p' ).each( function () {
				$( this ).addClass( 'prosesize-highlight' );
				proseSize += getLength( this );
				proseSizeHtml += this.innerHTML.length;
				refmarksize += getRefMarkLength( this, false );
				refmarkSizeHtml += getRefMarkLength( this, true );
				wordCount += this.innerHTML.replace( /(+)>)/ig,  ).split( ' ' ).length;
			} );
			// Calculate size of references (i.e. output of )
			parserOutput.find( 'ol.references' ).each( function () {
				refSize = getLength( this );
				refSizeHtml = this.innerHTML.length;
			} );
			
			proseSize -= refmarksize;
			
			function show_output() {
				proseValue = sizeElement( 'prose-size', '산문 길이 (텍스트만):', proseSize, ' (' + wordCount + ' 단어) "읽을 수 있는 산문 길이"' );
				refValue = sizeElement( 'ref-size', '출처 (텍스트만):', refSize + refmarksize );
				refHtmlValue = sizeElement( 'ref-size-html', '출처 (HTML 코드 포함):', refSizeHtml + refmarkSizeHtml );
				proseHtmlValue = sizeElement( 'prose-size-html', '산문 길이 (HTML 코드 포함):', proseSizeHtml - refmarkSizeHtml );
				output.append( proseHtmlValue, refHtmlValue, proseValue, refValue );
				parserOutput.prepend( combined );
				getFileSize( proseHtmlValue );
				getRevisionSize( proseValue );
			}
			
			// Add the relevant outputs once we have fetched the prose size.
			prosePromise.then(
				function( data ) {
					if ( mw.config.get( 'wgIsArticle' ) ) {
						// Tool doesn't work on previews
						proseSize = data.prose_size;
						wordCount = data.word_count;
				    }
					show_output();
				},
				// If tool is down fallback to our prose count
				show_output
			);
		}
	}
	if (
		
mw.config.get( 'wgCanonicalSpecialPageName' )
	) {
		$.ready.then( function () {
			/*			 * Depending on whether in edit mode or preview/view mode,
			 * show the approppiate response upon clicking the portlet link
			*/
			var func, $portlet, notEnabled = false;
			if (
				mw.config.get( 'wgAction' ) === 'edit' 
				( mw.config.get( 'wgAction' ) === 'submit' && document.getElementById( 'wikiDiff' ) )
			) {
				notEnabled = true;
				func = function () {
					mw.notify( 'Prosesize 스크립트를 편집 모드에서 작동시키려면 텍스트가 미리 보기 상태여야 합니다.' );
				};
			} else if (  'view', 'submit', 'historysubmit', 'purge' .indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
				func = main;
			}
			if ( func ) {
				$portlet = $( mw.util.addPortletLink( 'p-cactions', '#', '문서 길이', 't-page-size', '문서 길이와 산문 길이를 계산' ) );
				if ( notEnabled ) {
					$portlet.addClass( 'prosesize-portlet-link-edit-mode' );
				}
				$portlet.on( 'click', function ( e ) {
					e.preventDefault();
					if ( window.ve && ve.init && ve.init.target && ve.init.target.active ) {
						mw.notify( 'Prosesize는 시각 편집기에서 동작하지 않습니다.' );
					} else {
						func();
					}
				} );
			}
		} );
	}
}() );