User:Aaron Liu/MoveTop.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.
// Shows a [[mw:Help:Page status indicators|topicon]]
// when viewing a [[Wikipedia:Protection policy#Move protection]]-ed article.
// Half of code from [[User:Nardog/VitalTopicon.js]]
/* eslint-disable no-unused-expressions */

function getMoveTopicon(prot){
	switch(prot) {
		case 'autoconfirmed':
			return '1/1f/Move-protection-shackle-broken-arrow-lime.svg';
		case 'extendedconfirmed':
			return '1/1f/Jawp_2020_Move-EC-Protection_Sample-01.svg';
		case 'sysop':
			return '4/44/Move-protection-shackle.svg';
		case 'templateeditor':
			return 'c/c7/Logo-move-extended-protection.svg';
		default:
			return '1/13/Padlock-olive-arrow.svg';
	}
}

mw.config.get( 'wgAction' ) === 'view' &&
!mw.config.get('wgIsMainPage') &&
mw.config.get( 'wgRestrictionMove' ).length !== 0 &&
mw.loader.using( 'mediawiki.api' ).then( () => (
	new mw.Api().get( {
		action: 'query',
		titles: mw.config.get( 'wgPageName' ),
		prop: 'info',
		inprop: 'protection',
		formatversion: 2,
		maxage: 86400,
		smaxage: 86400
	} )
) ).then( ( response ) => {
	const prot = ( ( ( ( ( response || {} ).query || {} ).pages || [] )[ 0 ] || {} ).protection || [] );
	for ( const p of prot ) {
		if ( p.type === 'move' ) {
			new mw.Api().loadMessagesIfMissing( [ 'restriction-level-' + p.level ] ).done( () => {
				const $icon = $( '<div>' ).attr( {
					id: 'mw-indicator-pp-default',
					class: 'mw-indicator'
				} ).append(
					$( '<a>' ).attr( {
						href: '/wiki/Wikipedia:Protection_policy#move',
						title: `This page is ${ mw.message( 'restriction-level-' + p.level ).text() } against moving ${ ( p.expiry !== 'infinity' ) ? 'until ' + new Date( p.expiry ).toLocaleString() : 'indefinitely' }.`
					} ).append(
						$( '<img>' ).attr( {
							alt: 'Page move-protected',
							src: '//upload.wikimedia.org/wikipedia/commons/' + getMoveTopicon( p.level ),
							width: 20,
							height: 20
						} )
					)
				);
				mw.hook( 'wikipage.indicators' ).fire( $icon );
				$( () => {
					$( '.mw-indicators' ).prepend( $icon, '\n' );
				} );
			} );
			return; // multiple move protections shouldn't be possible
		}
	}
} );