MediaWiki:Gadget-articleCharacterCount.js

try { // containerize gadget, to protect other gadgets if this one goes wrong

/* Gives a character count of the number of letters on a current page before and after removing formatting characters.

It does the following before counting (in this order)
 * rm html
 * rm headers (==foo==)
 * rm bold/italic (italic)
 * rm Date template
 * put template parameters as text outside of template
 * remove templates
 * put linked text outside of external links
 * remove external links
 * Put images to there caption/alt text
 * make links to text
 * make category links to just category name
 * rm excess whitespace

Only thing formatting wise that is kept is bullets as arestricks. maintained by user:Bawolff mw.loader.load( '/w/index.php?title=' + 'user:Bawolff/mwapilib2.js' + '&action=raw&ctype=text/javascript' ); $(function { mw.util.addPortletLink('p-tb', 'javascript:countThoseChars(mw.config.get(\'wgPageName\'));', 'Character count', 't-charCount', 'Count the number of characters after removing formating of the current page'); });

function countThoseChars(page) { //precond page = mw.config.get('wgPageName'). or it will say its counting wrong page. var text = document.getElementById('wpTextbox1'); var init; if (text) { init = api(text.value); } else { init = api(page).getPage; } init.push.lift(function (p) { return p.length;}).swap. replace(/\<[^>]*\>/g, ''). replace(/^\s{0,4}(={1,6})([\s\S]*)\1\s*$/mg, "$2"). replace(/\'{2,5}/g, ''). replace(/\{\{[dD]ate\|[^\}]*\}\}/g, ''). replace(/(\{\{[^\|}]*)\|(\|[^\}=]*\|)?[^\|\}=]*=([^\|}]*)((?:\|[^\}]*)?\}\})/g, '$1$2$4 $3 '). replace(/(\{\{[^\|}]*)\|(\|[^\}=]*\|)?[^\|\}=]*=([^\|}]*)((?:\|[^\}]*)?\}\})/g, '$1$2$4 $3 '). replace(/(\{\{[^\|}]*)\|(\|[^\}=]*\|)?[^\|\}=]*=([^\|}]*)((?:\|[^\}]*)?\}\})/g, '$1$2$4 $3 '). replace(/(\{\{[^\|}]*)\|(\|[^\}=]*\|)?[^\|\}=]*=([^\|}]*)((?:\|[^\}]*)?\}\})/g, '$1$2$4 $3 '). replace(/(\{\{[^\|}]*)\|(\|[^\}=]*\|)?[^\|\}=]*=([^\|}]*)((?:\|[^\}]*)?\}\})/g, '$1$2$4 $3 '). replace(/(\{\{[^\|}]*)\|(\|[^\}=]*\|)?[^\|\}=]*=([^\|}]*)((?:\|[^\}]*)?\}\})/g, '$1$2$4 $3 '). replace(/(\{\{[^\|}]*)\|(\|[^\}=]*\|)?[^\|\}=]*=([^\|}]*)((?:\|[^\}]*)?\}\})/g, '$1$2$4 $3 '). replace(/(\{\{[^\|}]*)\|(\|[^\}=]*\|)?[^\|\}=]*=([^\|}]*)((?:\|[^\}]*)?\}\})/g, '$1$2$4 $3 '). replace(/\{\{[^\|}]*\}\}/g, ''). replace(/\{\{[^\|}]*\|([^\}]*)\}\}/g, ' $1 '). replace(/\[http(?:s)?:\/\/\S*\s([^\]]*)]/g, ' $1 '). replace(/http(?:s)?:\/\/\S*/g, ''). replace(/\[\iIfF][mMiI][aAlL][gGeE][eE]?\:[^\*\|([^\]]*)\]\]/g, ' $1 '). replace(/\[\cC]ategory\:([^\*)\]\]/g, ' $1 '). replace(/\[\\*\|([^\]]*)\]\]/g, '$1'). replace(/\[\[([^\]]*)\]\]/g, '$1'). replace(/\s{2,}/g, ' '). lift2(function (norm, raw) {alert("Normalized article '" + mw.config.get('wgTitle') + "' has " + norm.length + " characters for " + convertToPoints(norm.length) + " points (" + raw + " characters unnormalized)\n\nNote, this reflects the " + ( text ? "version in the text box, NOT the last saved version" : "last saved version, not what might be unsaved in an edit box somewhere" ) + ". If this article is a photoessay, it may count for more points then mentioned here."); return norm; }).exec; }

var convertToPoints = function (count) { var cats = document.getElementById('catlinks').getElementsByTagName('a'); var OR, user, points, catName; for (var i = 0; i < cats.length; i++) { catName = cats[i].title; /*if (catName.substring(catName.length - 9, catName.length) === "(WWC2010)") { user = catName.substring(9, catName.length - 10); }*/ if (catName === "Category:Original reporting") { OR = true; } } if (count < 1200) { points = 1; } else { points = 3; } if ((count - 1200)/700 > 0) { points += 2*Math.floor((count - 1200)/700); } if (OR) { points += 6; } if (document.getElementById('broadcast-report')) { points += 4; } return points; };

} catch (e) { // containerize gadget, to protect other gadgets if this one goes wrong // ignore }