Snippets/Redirect skin.js

How to use Snippets
List of Snippets
Redirect skin.js
Language(s): JavaScript
Compatible with: MediaWiki 1.17+ (Vector; Monobook)

Description edit

Redirect User:Name/skin.js and skin.css to the current skin's pages (unless the 'skin' page really exists).

It probably makes more sense to encourage people to just use Special:MyPage/common.js and Special:MyPage/common.css instead of redirecting /skin.js. See also phab:T71555.

Code edit

The below snippet uses the mediawiki.util module. Either add that module to the list of dependencies or, wrap it in:
mw.loader.using( 'mediawiki.util', function () { .. } );

/**
 * Redirect User:Name/skin.js and skin.css to the current skin's pages
 * (unless the 'skin' page really exists).
 *
 * Dependencies: mediawiki.util
 *
 * @source https://www.mediawiki.org/wiki/Snippets/Redirect_skin.js
 * @revision 2016-04-13
 */
if ( mw.config.get( 'wgArticleId' ) === 0 && mw.config.get( 'wgNamespaceNumber' ) === 2 ) {
	mw.loader.using( 'mediawiki.util', function () {
		var titleParts = mw.config.get( 'wgPageName' ).split( '/' );
		// Make sure there was a part before and after the slash
		// And that the latter is 'skin.js' or 'skin.css'
		if ( titleParts.length === 2 ) {
			var userSkinPage = titleParts[0] + '/' + mw.config.get( 'skin' );
			if ( titleParts[1] === 'skin.js' ) {
				window.location.href = mw.util.getUrl( userSkinPage + '.js' );
			} else if ( titleParts[1] === 'skin.css' ) {
				window.location.href = mw.util.getUrl( userSkinPage + '.css' );
			}
		}
	} );
}