User:Mooeypoo/User script demos/ArticleInfoBar.js

/** * User script: Add an article info bar to pages, showing word count and number of red links */ // First wait for mediawik.util to load, and the page to be ready. $.when( mw.loader.using( [ 'mediawiki.util' ] ), $.ready ).then(function {	var $redLinks = $( '.mw-body a.new' ), // Select all red links		wordCount = 0;

// Define i18n message key messages = { en: { 'gadget-demo-articleinfo-text': 'Red links: $1 | Word count: $2' } };	// Set English as the interface language mw.messages.set( messages.en );

// Count words $( '.mw-body p' ).each( function {		// We're estimating word count by taking all paragraphs, then taking their		// text content, and counting words. This isn't perfect, and doesn't quite		// work for all languages, but it works for a general demo		var wordArray = $( this ).text.split( ' ' );		wordCount += wordArray.length;	} );

// Set up a portlet link // See information here: https://github.com/wikimedia/mediawiki/blob/master/resources/src/mediawiki/mediawiki.util.js#L191 mw.util.addPortletLink(		'p-personal', // Location		'#', // Link		mw.msg( 'gadget-demo-articleinfo-text', $redLinks.length, wordCount ), // Text		'pt-articleInfo', // ID		, // Tooltip		, // Access key		$( '#p-personal ul li:first-child' ) // Node to insert before. We pick the first child in the list	); } );