User:Dlrohrer2003/common.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Scripts that I have made
mw.loader.load( '/w/index.php?title=User:Dlrohrer2003/validate-css.js&action=raw&ctype=text/javascript' ); // [[User:Dlrohrer2003/validate-css.js]]
mw.loader.load( '/w/index.php?title=User:Dlrohrer2003/validate-svg.js&action=raw&ctype=text/javascript' ); // [[User:Dlrohrer2003/validate-svg.js]]
mw.loader.load( '/w/index.php?title=User:Dlrohrer2003/source-code-wikilinks.js&action=raw&ctype=text/javascript' ); // [[User:Dlrohrer2003/source-code-wikilinks.js]]
mw.loader.load( '/w/index.php?title=User:Dlrohrer2003/randab.js&action=raw&ctype=text/javascript' ); // [[User:Dlrohrer2003/randab.js]]

// Scripts that other users have made
mw.loader.load( '/w/index.php?title=User:SD0001/shortdescs-in-category.js&action=raw&ctype=text/javascript' ); // Backlink: [[User:SD0001/shortdescs-in-category.js]]


/* Code snippets
 * -----------------------------------------------------------------------------
 * Description: Various small pieces of code that do useful things
 */

( function () {

	// Change the location that the 'Sandbox' link in the Personal Tools Portlet goes to.
	document.querySelector( '#pt-sandbox a' ).href = '/wiki/User:Dlrohrer2003/Sandbox';

	const minPageCount = 5;
	const conf = mw.config.get( [ 'wgPageName', 'wgNamespaceNumber' ] );
	const plural = n => n > 1 ? 's' : '';

	function editWatchlistSectionCount( fieldset ) {

		const len = fieldset.querySelectorAll( 'input[type="checkbox"]' ).length - 1;

		if ( len >= minPageCount ) {
			fieldset.querySelector( 'legend' ).append( `: ${ len } title${ plural( len ) }` );
		}
	}

	function recentChangesDayCount( header ) {

		const len = header.nextElementSibling.querySelectorAll( 'li' ).length;

		if ( len >= minPageCount ) {
			header.append( ` (${ len } change${ plural( len ) })` );
		}
	}

	function categorySubcategoryCount( group ) {

		const len = group.querySelectorAll( 'li' ).length;

		if ( len >= minPageCount ) {
			group.querySelector( 'h3' ).append( ` (${ len } subcategor${ len > 1 ? 'ies' : 'y' })` );
		}
	}

	function categoryPageCount( group ) {

		const len = group.querySelectorAll( 'li' ).length;

		if ( len >= minPageCount ) {
			group.querySelector( 'h3' ).append( ` (${ len } page${ plural( len ) })` );
		}
	}

	switch ( conf.wgPageName.split( '/' )[0] ) {
		// Count the number of titles in a given namespace when editing a watch list
		case 'Special:EditWatchlist':
			document.querySelectorAll( 'fieldset[id]' ).forEach( editWatchlistSectionCount );
			break;

		// Count the number of changes in the Watchlist or Recent Changes made on a given day
		case 'Special:Watchlist':
		case 'Special:RecentChanges':
		case 'Special:RecentChangesLinked':
			document.querySelectorAll( 'h4' ).forEach( recentChangesDayCount );
			break;
	}

	// Count the number of pages and subcategories in a category section.
	if ( conf.wgNamespaceNumber === 14 ) {
		document.querySelectorAll( '#mw-subcategories .mw-category-group' ).forEach( categorySubcategoryCount );
		document.querySelectorAll( '#mw-pages .mw-category-group' ).forEach( categoryPageCount );
	}

})();