MediaWiki:Gadget-Prosesize.js
Eʼtibor bering: Oʻzgartirishlaringizni koʻrish uchun, yangi moslamalaringizning saqlashdan keyin, brauzer keshini tozalash kerak:
Mozilla / Firefox: Ctrl+Shift+R, IE: Ctrl+F5, Safari: Cmd+Shift+R, Konqueror: F5, Opera: Tools → Preferences orqali keshni tozalang.
/**
* Prosesize gadjeti
* Hujjat sahifasi: https://en.wikipedia.org/wiki/Wikipedia:Prosesize
* Yana qarang: [[:en:User:Dr_pda/prosesize.js]].
*/
'use strict';
( function () {
function sizeFormatter( size ) {
var nbsp = "\xA0"; // =
if ( size > 10240 ) {
return ( Math.round( size / 1024 ) + nbsp + 'kB' );
} else {
return ( size + nbsp + 'B' );
}
}
function sizeElement( id, text, size, extraText ) {
return $( '<li>' )
.prop( 'id', id )
.append(
$( '<b>' ).text( text ),
document.createTextNode( ' ' + sizeFormatter( size ) + ( extraText || '' ) )
);
}
function getRevisionSize( proseValue ) {
var Api = new mw.Api();
function appendResult( size ) {
var wikiValue = sizeElement( 'wiki-size', 'Viki-matn:', size );
proseValue.before( wikiValue );
}
if ( mw.config.get( 'wgAction' ) === 'submit' ) {
// eslint-disable-next-line no-jquery/no-global-selector
appendResult( $( '#wpTextbox1' ).textSelection( 'getContents' ).length );
} else if ( mw.config.get( 'wgIsArticle' ) ) {
//
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 ) {
//
if ( mw.config.get( 'wgAction' ) !== 'submit' ) {
$.get( location ).then( function ( result ) {
var fsize = sizeElement( 'total-size', 'HTML-hujjat hajmi:', result.length );
proseHtmlValue.before( fsize );
} );
}
}
function getLength( id ) {
var i;
var textLength = 0;
for ( i = 0; i < id.childNodes.length; i++ ) {
if ( id.childNodes[ i ].nodeType === Node.TEXT_NODE ) {
textLength += id.childNodes[ i ].nodeValue.length;
} else if (
id.childNodes[ i ].nodeType === Node.ELEMENT_NODE &&
( id.childNodes[ i ].id === 'coordinates' || id.childNodes[ i ].className.indexOf( 'emplate' ) !== -1 )
) {
//
id.childNodes[ i ].className += ' prosesize-special-template';
} else if (id.childNodes[ i ].tagName !== 'STYLE') {
//
textLength += getLength( id.childNodes[ i ] );
}
}
return textLength;
}
function getRefMarkLength( id, html ) {
var i;
var textLength = 0;
for ( i = 0; i < id.childNodes.length; i++ ) {
if (
id.childNodes[ i ].nodeType === Node.ELEMENT_NODE &&
id.childNodes[ i ].className === 'reference'
) {
textLength += ( html ) ?
id.childNodes[ i ].innerHTML.length :
getLength( id.childNodes[ i ] );
}
}
return textLength;
}
function main() {
var 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' );
// eslint-disable-next-line no-jquery/no-global-selector
var prevHeader = $( '#document-size-header' );
var proseSize = 0;
var proseSizeHtml = 0;
var refmarksize = 0;
var refmarkSizeHtml = 0;
var wordCount = 0;
var refSize = 0;
var refSizeHtml = 0;
var header = $( '<span>' )
.prop( 'id', 'document-size-header' )
.html( 'Statistika <small>(<a href="//uz.wikipedia.org/wiki/Vikipediya:Asboblar/Prosesize">koʻproq maʼlumot</a>)</small>:' );
var output = $( '<ul>' )
.prop( 'id', 'document-size-stats' );
var combined = $( '<div>' )
.prop( 'id', 'document-size' )
.append( header, output );
if ( parserOutput.length === 0 ) {
return;
}
if ( prevStats.length ) {
//
prevStats.remove();
prevHeader.remove();
parserOutput.children( 'p' ).removeClass( 'prosesize-highlight' );
} else {
//
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;
} );
//
parserOutput.find( 'ol.references' ).each( function () {
refSize = getLength( this );
refSizeHtml = this.innerHTML.length;
} );
proseValue = sizeElement( 'prose-size', 'Sahifa hajmi (faqat matn):', proseSize - refmarksize, ' (' + wordCount + ' ta soʻz) [sahifadagi andozalar va jadvallar hisobga olinmagan]' );
refValue = sizeElement( 'ref-size', 'Manbalar (faqat matn):', refSize + refmarksize );
refHtmlValue = sizeElement( 'ref-size-html', 'Manbalar:', refSizeHtml + refmarkSizeHtml, ' [sahifada ishlatilgan barcha HTML-kodlar ham hisobga olingan]' );
proseHtmlValue = sizeElement( 'prose-size-html', 'Sahifa hajmi:', proseSizeHtml - refmarkSizeHtml, ' [sahifada ishlatilgan barcha HTML-kodlar ham hisobga olingan]' );
output.append( proseHtmlValue, refHtmlValue, proseValue, refValue );
parserOutput.prepend( combined );
getFileSize( proseHtmlValue );
getRevisionSize( proseValue );
}
}
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( 'You need to preview the text for the prose size script to work in edit mode.' );
};
} else if ( [ 'view', 'submit', 'historysubmit', 'purge' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
func = main;
}
if ( func ) {
$portlet = $( mw.util.addPortletLink( 'p-tb', '#', 'Sahifa hajmi', 't-page-size', 'Sahifa hajmini hisoblash' ) );
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 gadjeti Koʻrib tahrirlagich (Visual Editor) da ishlamaydi.' );
} else {
func();
}
} );
}
} );
}
}() );