User:Polygnotus/Scripts/ProseSize.js
Appearance
From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Polygnotus/Scripts/ProseSize.
/** * Wikipedia Prose Size Calculator * Add this to your common.js page on Wikipedia * (e.g., https://en.wikipedia.org/wiki/Special:MyPage/common.js) * * This script adds a "Calculate Prose Size" button to your Wikipedia interface * that shows article size metrics when clicked. */ // Create a self-invoking function to avoid polluting global namespace (function(){ // Wait for the document to be fully loaded $(document).ready(function(){ // Only run in content namespaces if(mw.config.get('wgNamespaceNumber')<0){ return; } // Add a small delay to ensure all Wikipedia UI elements are loaded setTimeout(function(){ // Find the appropriate place to insert our link letinserted=false; // Try to add to the "Tools" section (most likely target) const$toolsMenu=$('.vector-menu-content-list').filter(function(){ return$(this).find('#t-whatlinkshere').length>0; }).first(); if($toolsMenu.length){ // Create our list item with the same classes as other items const$listItem=$('<li id="t-prosesize" class="mw-list-item">') .append( $('<a>') .attr('href','#') .html('<span>Calculate prose size</span>') .on('click',function(e){ e.preventDefault(); calculateProseSize(); }) ); // Insert at the end of the tools list $toolsMenu.append($listItem); inserted=true; console.log('ProseSize: Added to tools menu'); } // Try adding to print/export section as fallback if(!inserted){ const$printMenu=$('.vector-menu-content-list').filter(function(){ return$(this).find('#t-print').length>0; }).first(); if($printMenu.length){ const$listItem=$('<li id="t-prosesize" class="mw-list-item">') .append( $('<a>') .attr('href','#') .html('<span>Calculate prose size</span>') .on('click',function(e){ e.preventDefault(); calculateProseSize(); }) ); $printMenu.append($listItem); inserted=true; console.log('ProseSize: Added to print/export menu'); } } // If neither menu is found, the button simply won't appear if(!inserted){ console.log('ProseSize: Could not find suitable menu location'); }else{ console.log('ProseSize calculator loaded successfully'); } },1000);// Wait 1 second after page load before trying to insert }); functioncalculateProseSize(){ // Make sure OOUI is loaded mw.loader.using(['oojs-ui-core','oojs-ui-windows']).then(function(){ // Show loading indicator const$loadingOverlay=$('<div>') .css({ position:'fixed', top:0, left:0, right:0, bottom:0, background:'rgba(255,255,255,0.7)', zIndex:10000, display:'flex', alignItems:'center', justifyContent:'center' }) .append( $('<div>') .text('Calculating article metrics...') .css({ padding:'20px', background:'white', borderRadius:'5px', boxShadow:'0 2px 10px rgba(0,0,0,0.2)' }) ) .appendTo('body'); // Use setTimeout to allow the loading indicator to render setTimeout(function(){ try{ // Get the content area const$content=$('#mw-content-text'); constfullHtmlSize=$content.html().length; // Clone the content to manipulate without affecting the page const$clone=$content.clone(); // Calculate HTML document size consthtmlDocSize=document.documentElement.outerHTML.length; // Get all references const$refs=$clone.find('.reflist, .references'); letrefsHtml=''; $refs.each(function(){ refsHtml+=$(this).html()||''; }); constrefsHtmlSize=refsHtml.length; // Remove elements that aren't prose $clone.find('table, .navbox, .vertical-navbox, .infobox, .sidebar, '+ '.reflist, .references, .refbegin, .refend, '+ '.mw-editsection, .thumb, .metadata, .tmulti, '+ '.mbox-small, .ambox, #coordinates, .mw-jump-link, '+ '.mw-empty-elt, style, script, .noprint').remove(); // Get the wiki text size (estimate if not in edit mode) letwikiTextSize; if($('#wpTextbox1').length){ wikiTextSize=$('#wpTextbox1').val().length; }else{ // More accurate estimation based on HTML content size // Typical wiki text is roughly 1/3 to 1/5 of HTML size wikiTextSize=Math.round(fullHtmlSize/4); // Apply a sanity check - most wiki articles are under 100kB if(wikiTextSize>100000){ // Apply a logarithmic scale for very large articles wikiTextSize=Math.round(50000*(1+Math.log10(wikiTextSize/50000))); } } // Calculate prose with HTML constproseWithHtml=$clone.html().length; // Calculate text-only content constproseText=$clone.text().trim(); constproseTextSize=proseText.length; constwordCount=proseText.split(/\s+/).filter(Boolean).length; // Calculate text-only references constrefsText=$refs.text().trim(); constrefsTextSize=refsText.length; // Create a dialog with the results constresultsContent=newOO.ui.FieldsetLayout({ items:[ newOO.ui.LabelWidget({ label:'HTML document size: '+formatSize(htmlDocSize), classes:['prose-size-result'] }), newOO.ui.LabelWidget({ label:'Prose size (including all HTML code): '+formatSize(proseWithHtml), classes:['prose-size-result'] }), newOO.ui.LabelWidget({ label:'References (including all HTML code): '+formatSize(refsHtmlSize), classes:['prose-size-result'] }), newOO.ui.LabelWidget({ label:'Wiki text: '+formatSize(wikiTextSize), classes:['prose-size-result'] }), newOO.ui.LabelWidget({ label:'Prose size (text only): '+formatSize(proseTextSize)+' ('+wordCount+' words) "readable prose size"', classes:['prose-size-result'] }), newOO.ui.LabelWidget({ label:'References (text only): '+formatSize(refsTextSize), classes:['prose-size-result'] }) ] }); // Create and display the dialog constdialog=newOO.ui.MessageDialog(); constwindowManager=newOO.ui.WindowManager(); $('body').append(windowManager.$element); windowManager.addWindows([dialog]); // Remove loading indicator $loadingOverlay.remove(); // Create a results text field that can be easily selected constresultsText=[ 'HTML document size: '+formatSize(htmlDocSize), 'Prose size (including all HTML code): '+formatSize(proseWithHtml), 'References (including all HTML code): '+formatSize(refsHtmlSize), 'Wiki text: '+formatSize(wikiTextSize), 'Prose size (text only): '+formatSize(proseTextSize)+' ('+wordCount+' words) "readable prose size"', 'References (text only): '+formatSize(refsTextSize) ].join('\n'); // Add a text field to the content for easy copying consttextField=newOO.ui.MultilineTextInputWidget({ value:resultsText, readOnly:true, rows:6 }); // Add the text field to the results resultsContent.addItems([ newOO.ui.FieldLayout(textField,{ label:'Select text to copy:', align:'top' }) ]); windowManager.openWindow(dialog,{ title:'Article Size Statistics', message:resultsContent.$element, size:'medium', actions:[ { action:'accept', label:'Close', flags:'primary' }, { action:'copy', label:'Copy All', flags:'' } ] }); // Select all text when clicked textField.$element.on('click',function(){ textField.select(); }); // Handle the copy action dialog.on('closing',function(windowManager,closing){ if(closing.action==='copy'){ consttextToCopy=[ 'HTML document size: '+formatSize(htmlDocSize), 'Prose size (including all HTML code): '+formatSize(proseWithHtml), 'References (including all HTML code): '+formatSize(refsHtmlSize), 'Wiki text: '+formatSize(wikiTextSize), 'Prose size (text only): '+formatSize(proseTextSize)+' ('+wordCount+' words) "readable prose size"', 'References (text only): '+formatSize(refsTextSize) ].join('\n'); // Create a temporary textarea element const$textarea=$('<textarea>') .val(textToCopy) .css({ position:'fixed', top:0, left:0, width:'2em', height:'2em', padding:0, border:'none', outline:'none', boxShadow:'none', background:'transparent' }) .appendTo('body'); try{ // Select the text and copy it $textarea.select(); constsuccess=document.execCommand('copy'); if(success){ mw.notify('Results copied to clipboard!',{type:'success'}); }else{ // Try the new API as fallback if(navigator.clipboard&&navigator.clipboard.writeText){ navigator.clipboard.writeText(textToCopy) .then(function(){ mw.notify('Results copied to clipboard!',{type:'success'}); }) .catch(function(){ mw.notify('Could not copy to clipboard. Please press Ctrl+C to copy manually.',{type:'error'}); // Select the text again for user to copy manually $textarea.select(); }); }else{ mw.notify('Could not copy to clipboard. Please press Ctrl+C to copy manually.',{type:'error'}); // Select the text again for user to copy manually $textarea.select(); } } }catch(err){ mw.notify('Could not copy to clipboard. Please press Ctrl+C to copy manually.',{type:'error'}); // Select the text again for user to copy manually $textarea.select(); }finally{ // Keep the textarea for a moment in case the user needs to copy manually setTimeout(function(){ $textarea.remove(); },5000); } // Prevent the dialog from closing after copy closing.preventDefault(); } }); // Add some styling for the dialog $('<style>') .text('.prose-size-result { margin: 8px 0; font-family: monospace; font-size: 14px; }') .appendTo('head'); }catch(error){ // Remove loading indicator if an error occurs $loadingOverlay.remove(); // Show error message mw.notify('Error calculating prose size: '+error.message,{type:'error'}); console.error('ProseSize calculator error:',error); } },0); }).catch(function(error){ mw.notify('Error loading OOUI components. Please try again.',{type:'error'}); console.error('ProseSize OOUI loading error:',error); }); } // Helper function to format byte sizes functionformatSize(bytes){ if(bytes>=1024){ returnbytes+' B'+' ('+Math.round(bytes/1024)+' kB)'; }else{ returnbytes+' B'; } } })();// End self-invoking function