/* _____________________________________________________________________________
/*jshint maxerr:999*/
* | |
//copy of [[User:Dr_pda/prosesize.js]]
* | === WARNING: GLOBAL GADGET FILE === |
//TODO: modernize (User:Shubinator/DYKcheck.js may have useful code? or use xtools api ) and fix to work on all maintained skins propose to become gadget
* | Changes to this page affect many users. |
//Wikipedia:Village_pump_(technical)/Archive_161#"Page_size"_Tool:_"Word_count"_script_'User:Dr_pda/prosesize.js'_does_not_work_with_skin_=_Timeless
* | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
//<nowiki>
* |_____________________________________________________________________________|
//This function adds a link to the toolbox which, when clicked, displays the size of the page
*
//and the size of the prose. See the talk page for more details.
/ */
/**
//To use this function add {{subst:js|User:Dr pda/prosesize.js}} to your monobook.js
* Prosesize
//
* Documentation at en.wikipedia.org/wiki/Wikipedia:Prosesize
function loadXMLDocPassingTemplate(url, handler, page) {
* Rewrite of [[User:Dr_pda/prosesize.js]].
// branch for native XMLHttpRequest object
*/
if (window.XMLHttpRequest) {
'use strict';
var req = new XMLHttpRequest();
( function () {
}
function sizeFormatter( size ) {
// branch for IE/Windows ActiveX version
var nbsp = "\xA0"; // Equivalent to
else if (window.ActiveXObject) {
if ( size > 10240 ) {
var req = new ActiveXObject("Microsoft.XMLHTTP");
return ( Math.round( size / 1024 ) + nbsp + 'kB' );
}
if (req) {
req.onreadystatechange = function() {
handler(req, page)
};
req.open("GET", url, true);
req.send("");
}
}
function getWikiText(req, page) {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
// ...processing statements go here...
response = req.responseXML.documentElement;
var rev = response.getElementsByTagName('rev');
if (rev.length > 0) {
result = rev[0].getAttribute('size');
if (result > 10240) {
result = (result / 1024).toFixed(0) + ' kB';
} else {
result = result + ' B';
}
wiki_value = document.createElement("li");
wiki_value.id = "wiki-size";
wiki_value.innerHTML = '<b>Wiki text: </b>' + result;
var output = document.getElementById("document-size-stats");
prose_value = document.getElementById("prose-size");
output.insertBefore(wiki_value, prose_value);
} else {
//alert("There was a problem using the Wikipedia Search to find the wiki text size\nEither the search is not working or the correct article did not appear on the first page of results");
wiki_value = document.createElement("li");
wiki_value.id = "wiki-size";
wiki_value.innerHTML = '<b>Wiki text: </b>Problem getting wiki text size';
var output = document.getElementById("document-size-stats");
prose_value = document.getElementById("prose-size");
output.insertBefore(wiki_value, prose_value);
}
} else {
return ( size + nbsp + 'B' );
alert("There was a problem retrieving the XML data:\n" +
req.statusText);
}
}
}
function getFileSizesizeElement(req id, pagetext, size, extraText ) {
return $( '<li>' )
// only if req shows "loaded"
.prop( 'id', id )
if (req.readyState == 4) {
.append(
// only if "OK"
$( '<b>' ).text( text ),
if (req.status == 200) {
document.createTextNode( ' ' + sizeFormatter( size ) + ( extraText || '' ) )
// ...processing statements go here...
);
var fsize = req.responseText.length;
window.status = fsize;
var total_value = document.createElement("li");
total_value.id = "total-size";
total_value.innerHTML = '<b>File size: </b>' + (fsize / 1024).toFixed(0) + ' kB';
var output = document.getElementById("document-size-stats");
var prose_html_value = document.getElementById("prose-size-html");
output.insertBefore(total_value, prose_html_value);
} else {
alert("There was a problem retrieving the XML data:\n" +
req.statusText + "\n(" + url + ")");
}
}
}
function getLengthgetRevisionSize(id proseValue ) {
var textLengthApi = 0new mw.Api();
function appendResult( size ) {
for (var i = 0; i < id.childNodes.length; i++) {
var wikiValue = sizeElement( 'wiki-size', 'Wiki text:', size );
if (id.childNodes[i].nodeType === Node.TEXT_NODE) {
proseValue.before( wikiValue );
textLength += id.childNodes[i].nodeValue.length;
}
} else if (id.childNodes[i].nodeType === Node.ELEMENT_NODE &&
if ( mw.config.get( 'wgAction' ) === 'submit' ) {
(id.childNodes[i].id == 'coordinates' || id.childNodes[i].className.indexOf('emplate') != -1)) {
// Get size of text in edit box
//special case for {{coord}} and {{fact}}-like templates
// eslint-disable-next-line no-jquery/no-global-selector
//Exclude from length, and don't set background yellow
appendResult( $( '#wpTextbox1' ).textSelection( 'getContents' ).length );
id.childNodes[i].style.cssText = 'background-color:white';
} else if ( mw.config.get( 'wgIsArticle' ) ) {
// Get revision size from API
textLength += getLength(id.childNodes[i]);
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 );
} );
}
}
return textLength;
}
function getRefMarkLengthgetFileSize(id, htmlproseHtmlValue ) {
// HTML document size not well defined for preview mode or section edit
var textLength = 0;
if ( mw.config.get( 'wgAction' ) !== 'submit' ) {
for (var i = 0; i < id.childNodes.length; i++) {
$.get( ___location ).then( function ( result ) {
if (id.childNodes[i].nodeType === Node.ELEMENT_NODE && id.childNodes[i].className == 'reference') {
var fsize = sizeElement( 'total-size', 'HTML document size:', result.length );
textLength += (html) ? id.childNodes[i].innerHTML.length : getLength(id.childNodes[i]);
proseHtmlValue.before( fsize );
} );
}
}
return textLength;
}
function getDocumentSizegetLength( id ) {
var bodyContenti;
var textLength = 0;
if ($.inArray(mw.config.get('skin'), ['vector', 'monobook']) !== -1) {
for ( i = 0; i < id.childNodes.length; i++ ) {
bodyContent = $('#mw-content-text .mw-parser-output');
if ( id.childNodes[ i ].nodeType === Node.TEXT_NODE ) {
}
textLength += id.childNodes[ i ].nodeValue.length;
//Same for all skins if previewing page
} else if (
if (mw.config.get('wgAction') == 'submit') {
id.childNodes[ i ].nodeType === Node.ELEMENT_NODE &&
bodyContent = $('#wikiPreview .mw-parser-output');
( id.childNodes[ i ].id === 'coordinates' || id.childNodes[ i ].className.indexOf( 'emplate' ) !== -1 )
}
) {
// special case for {{coord}} and {{fact}}-like templates
if (bodyContent.length === 0) {
// Exclude from length, and don't set background yellow
return;
id.childNodes[ i ].className += ' prosesize-special-template';
}
} else if (id.childNodes[ i ].tagName !== 'STYLE') {
bodyContent = bodyContent[0];
// Exclude style tags
textLength += getLength( id.childNodes[ i ] );
if (document.getElementById("document-size-stats")) {
//if statistics already exist, turn them off and remove highlighting
var output = document.getElementById("document-size-stats");
var oldStyle = output.className;
var pList = bodyContent.getElementsByTagName("p");
for (var i = 0; i < pList.length; i++) {
if (pList[i].parentNode == bodyContent) {
pList[i].style.cssText = oldStyle;
}
}
return textLength;
output.parentNode.removeChild(output);
}
var header = document.getElementById("document-size-header");
header.parentNode.removeChild(header);
} else {
var output = document.createElement("ul");
output.id = "document-size-stats";
function getRefMarkLength( id, html ) {
var prose_html_value = document.createElement("li");
var i;
prose_html_value.id = "prose-size-html";
var textLength = 0;
output.appendChild(prose_html_value);
for ( i = 0; i < id.childNodes.length; i++ ) {
if (
var ref_html_value = document.createElement("li");
id.childNodes[ i ].nodeType === Node.ELEMENT_NODE &&
ref_html_value.id = "ref-size-html";
id.childNodes[ i ].className === 'reference'
output.appendChild(ref_html_value);
) {
textLength += ( html ) ?
var prose_value = document.createElement("li");
id.childNodes[ i ].innerHTML.length :
prose_value.id = "prose-size";
getLength( id.childNodes[ i ] );
output.appendChild(prose_value);
output.className = bodyContent.getElementsByTagName("p").item(0).style.cssText;
var ref_value = document.createElement("li");
ref_value.id = "ref-size";
output.appendChild(ref_value);
bodyContent.insertBefore(output, bodyContent.firstChild);
var header = document.createElement("span");
header.id = "document-size-header";
header.innerHTML = '<br/>Document statistics: <small><i>(See <a href="//en.wikipedia.org/wiki/User_talk:Dr_pda/prosesize.js">here</a> for details.)<i></small>';
bodyContent.insertBefore(header, output);
//File size not well defined for preview mode or section edit
if (mw.config.get('wgAction') != 'submit') {
loadXMLDocPassingTemplate(___location.pathname, getFileSize, '');
}
//Get size of images only if browser supports filesize property (IE)
var iList = bodyContent.getElementsByTagName("img");
if (iList.length > 0 && iList[0].fileSize) {
//Get size of images included in document
var image_size = 0;
var first_magnify = true;
for (var i = 0; i < iList.length; i++) {
var im = iList[i];
if (im.getAttribute("src").indexOf("magnify-clip.png") != -1) {
if (first_magnify) {
image_size += im.fileSize * 1;
first_magnify = false;
}
} else {
image_size += im.fileSize * 1;
}
}
var image_value = document.createElement("li");
image_value.id = "image-size";
image_value.innerHTML = '<b>Images: </b>' + (image_size / 1024).toFixed(0) + ' kB';
output.appendChild(image_value);
}
return textLength;
//Calculate prose size and size of reference markers ([1] etc)
}
var pList = bodyContent.getElementsByTagName("p");
function main() {
prose_size = 0;
var prosePromise, proseValue, refValue, refHtmlValue, proseHtmlValue;
prose_size_html = 0;
// eslint-disable-next-line no-jquery/no-global-selector
refmark_size = 0;
var parserOutput = $( '#mw-content-text .mw-parser-output' );
refmark_size_html = 0;
// eslint-disable-next-line no-jquery/no-global-selector
word_count = 0;
var prevStats = $( '#document-size-stats' );
for (var i = 0; i < pList.length; i++) {
// eslint-disable-next-line no-jquery/no-global-selector
var para = pList[i];
var prevHeader = $( '#document-size-header' );
if (para.parentNode == bodyContent) {
var proseSize = 0;
prose_size += getLength(para);
var proseSizeHtml = 0;
prose_size_html += para.innerHTML.length;
var refmarksize = 0;
refmark_size += getRefMarkLength(para, false);
var refmarkSizeHtml = 0;
refmark_size_html += getRefMarkLength(para, true);
var wordCount = 0;
word_count += para.innerHTML.replace(/(<([^>]+)>)/ig, "").split(' ').length;
var refSize = 0;
para.style.cssText = "background-color:yellow";
var refSizeHtml = 0;
}
var header = $( '<span>' )
.prop( 'id', 'document-size-header' )
.html( 'Document statistics <small>(<a href="//en.wikipedia.org/wiki/Wikipedia:Prosesize">more information</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 ) {
// If statistics already exist, turn them off and remove highlighting
if ((prose_size - refmark_size) > 10240) {
prevStats.remove();
prose_value.innerHTML = '<b>Prose size (text only): </b>' + ((prose_size - refmark_size) / 1024).toFixed(0) + ' kB (' + word_count + ' words) "readable prose size"';
prevHeader.remove();
parserOutput.children( 'p' ).removeClass( 'prosesize-highlight' );
} else {
// Use prosesize API to get a more accurate prose size account
prose_value.innerHTML = '<b>Prose size (text only): </b>' + (prose_size - refmark_size) + ' B (' + word_count + ' words) "readable prose size"';
// 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)
if ((prose_size_html - refmark_size_html) > 10240) {
parserOutput.children( 'p' ).each( function () {
prose_html_value.innerHTML = '<b>Prose size (including all HTML code): </b>' + ((prose_size_html - refmark_size_html) / 1024).toFixed(0) + ' kB';
$( this ).addClass( 'prosesize-highlight' );
} else {
proseSize += getLength( this );
prose_html_value.innerHTML = '<b>Prose size (including all HTML code): </b>' + (prose_size_html - refmark_size_html) + ' B';
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 <references/>)
parserOutput.find( 'ol.references' ).each( function () {
var rList = bodyContent.getElementsByTagName("ol");
refSize = getLength( this );
var ref_size = 0;
refSizeHtml = this.innerHTML.length;
var ref_size_html = 0;
} );
for (var i = 0; i < rList.length; i++) {
if (rList[i].parentNode.className == "references") {
proseSize -= refmarksize;
ref_size = getLength(rList[i]);
ref_size_html = rList[i].innerHTML.length;
function show_output() {
proseValue = sizeElement( 'prose-size', 'Prose size (text only):', proseSize, ' (' + 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
);
}
}
if (
if ((ref_size + refmark_size) > 10240) {
!mw.config.get( 'wgCanonicalSpecialPageName' )
ref_value.innerHTML = '<b>References (text only): </b>' + ((ref_size + refmark_size) / 1024).toFixed(0) + ' kB';
) {
} else {
$.ready.then( function () {
ref_value.innerHTML = '<b>References (text only): </b>' + (ref_size + refmark_size) + ' B';
} /**
* Depending on whether in edit mode or preview/view mode,
* show the approppiate response upon clicking the portlet link
if ((ref_size_html + refmark_size_html) > 10240) {
*/
ref_html_value.innerHTML = '<b>References (including all HTML code): </b>' + ((ref_size_html + refmark_size_html) / 1024).toFixed(0) + ' kB';
var func, $portlet, notEnabled = false;
} else {
if (
ref_html_value.innerHTML = '<b>References (including all HTML code): </b>' + (ref_size_html + refmark_size_html) + ' B';
mw.config.get( 'wgAction' ) === 'edit' ||
}
( mw.config.get( 'wgAction' ) === 'submit' && document.getElementById( 'wikiDiff' ) )
) {
//get correct name of article from wikipedia-defined global variables
notEnabled = true;
var pageNameUnderscores = mw.config.get('wgPageName');
func = function () {
var pageNameSpaces = pageNameUnderscores.replace(/_/g, ' ');
mw.notify( 'You need to preview the text for the prose size script to work in edit mode.' );
};
//if page is a permalink, diff, etc don't try to search
} else if ( [ 'view', 'submit', 'historysubmit', 'purge' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
if (!___location.pathname.match('/w/index.php')) {
func = main;
//Get revision size from API
var searchURL = mw.config.get('wgScriptPath') + '/api.php?action=query&prop=revisions&rvprop=size&format=xml&revids=' + mw.config.get('wgCurRevisionId');
loadXMLDocPassingTemplate(searchURL, getWikiText, pageNameSpaces);
} else if (mw.config.get('wgAction') == 'submit') {
//Get size of text in edit box
result = document.getElementById('wpTextbox1').value.length;
if (result > 10240) {
result = (result / 1024).toFixed(0) + ' kB';
} else {
result = result + ' B';
}
if ( func ) {
wiki_value = document.createElement("li");
$portlet = $( mw.util.addPortletLink( 'p-tb', '#', 'Page size', 't-page-size', 'Calculate page and prose size' ) );
wiki_value.id = "wiki-size";
if ( notEnabled ) {
wiki_value.innerHTML = '<b>Wiki text: </b>' + result;
$portlet.addClass( 'prosesize-portlet-link-edit-mode' );
var output = document.getElementById("document-size-stats");
}
prose_value = document.getElementById("prose-size");
$portlet.on( 'click', function ( e ) {
output.insertBefore(wiki_value, prose_value);
e.preventDefault();
}
if ( window.ve && ve.init && ve.init.target && ve.init.target.active ) {
mw.notify( 'Prosesize does not work with the Visual Editor.' );
} else {
func();
}
} );
}
} );
}
}() );
}
$.when($.ready, mw.loader.using( 'mediawiki.util' )).then( function() {
// Depending on whether in edit mode or preview/view mode, show the approppiate response upon clicking the portlet link
var func;
if (mw.config.get('wgAction') == 'edit' || (mw.config.get('wgAction') == 'submit' && document.getElementById('wikiDiff'))) {
func = function() {
alert("You need to preview the text for the prose size script to work in edit mode.");
};
mw.util.addCSS( '#t-page-size:first-child { color:black;}');
} else if (['view', 'submit', 'historysubmit', 'purge'].indexOf(mw.config.get('wgAction')) !== -1) {
func = function() {
getDocumentSize();
};
}
if (func) {
$(mw.util.addPortletLink('p-tb', '#', 'Page size', 't-page-size', 'Calculate page and prose size')).on( 'click', function(e) {
e.preventDefault();
func();
});
}
});
//</nowiki>
|