Участник:Stjn/linkCount.js

/*
 * Get transclusion count and the link count link on template/module pages
 * Uses linkcount.toolforge.org to calculate it
 */
mw.hook( 'wikipage.content' ).add( () => {
	var _c = mw.config.get( [
		'wgAction',
		'wgNamespaceNumber',
		'wgPageName',
		'wgServerName',
	] );
	
	// Do not run outside templates and modules
	if ( [ 10, 828 ].includes( _c.wgNamespaceNumber ) === false ) return;
	
	// Do not run outside of view or edit actions
	if ( [ 'view', 'edit', 'submit' ].includes( _c.wgAction ) === false ) return;
	
	// Do not run on maintenance subpages (/doc etc.)
	var ignoredSubpages = [
		'doc',
		'sandbox', 'песочница',
		'testcases'
	];
	var isIgnoredSubpage = ignoredSubpages.find( function( el ) {
		return _c.wgPageName.endsWith( '/' + el );
	} );
	if ( isIgnoredSubpage ) return;
	
	// Do not run twice
	if ( document.querySelector( '.firstHeading-linkCount' ) !== null ) return;
	
	var linkCountParams = `?page=${ encodeURIComponent( _c.wgPageName ) }&project=${ _c.wgServerName }`;
	fetch(
		`https://linkcount.toolforge.org/api/${ linkCountParams }`
	).then( res => res.json() ).then( data => {
		if ( !data.transclusions || !( 'all' in data.transclusions ) ) return;
		var url = 'https://linkcount.toolforge.org/' + linkCountParams;
		var title = data.transclusions.all + ' transclusions, ' +
			data.wikilinks.all + ' wikilinks, ' +
			data.redirects + ' redirects — linkcount.toolforge.org';
		
		var link = document.createElement( 'a' );
		link.href = url;
		link.title = title;
		link.setAttribute( 'target', '_blank' );
		link.textContent = `(${ data.transclusions.all })`;

		var linkCount = document.createElement( 'small' );
		linkCount.className = 'firstHeading-linkCount';
		linkCount.innerHTML = ' ';
		linkCount.appendChild( link );
		
		document.querySelector( '#firstHeading' ).appendChild( linkCount );
	} )
} );

 

Prefix: a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9

Portal di Ensiklopedia Dunia