MediaWiki:Gadget-Prosesize.js: Difference between revisions

Content deleted Content added
tweak comment
fix
 
(11 intermediate revisions by 2 users not shown)
Line 15:
( function () {
function sizeFormatter( size ) {
var nbsp = "\xA0"; // Equivalent to  
if ( size > 10240 ) {
return ( Math.round( size / 1024 ) + '  + 'kB' );
} else {
return ( size + '  + 'B' );
}
}
Line 25 ⟶ 26:
return $( '<li>' )
.prop( 'id', id )
.append(
$( '<b>' ).text( text ) ),
document.appendcreateTextNode( ' ' + sizeFormatter( size ) + ( extraText || '' ) );
);
}
 
Line 76 ⟶ 79:
// Exclude from length, and don't set background yellow
id.childNodes[ i ].className += ' prosesize-special-template';
} else if (id.childNodes[ i ].tagName !== 'STYLE') {
} else {
// Exclude style tags
textLength += getLength( id.childNodes[ i ] );
}
Line 100 ⟶ 104:
 
function main() {
var prosePromise, proseValue, refValue, refHtmlValue, proseHtmlValue;
// eslint-disable-next-line no-jquery/no-global-selector
var parserOutput = $( '#mw-content-text .mw-parser-output' );
// eslint-disable-next-line no-jquery/no-global-selector
var prevStats = $( '#document-size-stats' );
Line 131 ⟶ 135:
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 () {
Line 146 ⟶ 155:
refSizeHtml = this.innerHTML.length;
} );
 
proseSize -= refmarksize;

function show_output() {
proseValue = sizeElement( 'prose-size', 'Prose size (text only):', proseSize - refmarksize, ' (' + wordCount + ' words) "readable prose size"' );
refValue = sizeElement( 'ref-size', 'References (text only):', refSize + refmarksize );
refHtmlValue = sizeElement( 'ref-size-html', 'References (including all HTML code):', refSizeHtml + refmarkSizeHtml );
proseHtmlValue = sizeElement( 'prose-size-html', 'Prose size (including all HTML code):', 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
);
}
}