User:Nardog/VitalTopicon.js

Source: 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:Vital articles|vital article]].

mw.config.get('wgNamespaceNumber') === 0 &&
mw.config.get('wgAction') === 'view' &&
mw.loader.using('mediawiki.api').then(() => (
	new mw.Api().get({
		action: 'query',
		titles: 'Talk:' + mw.config.get('wgTitle'),
		prop: 'categories',
		clcategories: 'Category:Wikipedia level-1 vital articles|Category:Wikipedia level-2 vital articles|Category:Wikipedia level-3 vital articles|Category:Wikipedia level-4 vital articles|Category:Wikipedia level-5 vital articles|Category:Wikipedia level-unknown vital articles',
		formatversion: 2,
		maxage: 86400,
		smaxage: 86400
	})
)).then(response => {
	let cat = (((((response || {}).query || {}).pages || [])[0] || {}).categories || [])[0];
	if (!cat) return;
	let level = cat.title.match(/^Category:Wikipedia level-(.+) vital articles$/)[1];
	let $icon = $('<div>').attr({
		id: 'mw-indicator-vital-article',
		class: 'mw-indicator'
	}).append(
		$('<a>').attr({
			href: `/wiki/Wikipedia:Vital_articles${['3', 'unknown'].includes(level) ? '' : '/Level/' + level}`,
			title: `This is a level-${level} vital article.`
		}).append(
			$('<img>').attr({
				alt: 'Vital article',
				src: '//upload.wikimedia.org/wikipedia/commons/1/13/C%C3%ADrculos_Conc%C3%A9ntricos.svg',
				width: 20,
				height: 20
			})
		)
	);
	mw.hook('wikipage.indicators').fire($icon);
	$(() => {
		$('.mw-indicators').prepend($icon, '\n');
	});
});