Jump to content

User:Skynxnex/VitalTopicon.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.
// Copy of [[User:Nardog/VitalTopicon.js]] to allow my own experiments

// 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 (level ${level})`,
				src: '//upload.wikimedia.org/wikipedia/commons/1/13/Círculos_Concéntricos.svg',
				width: 20,
				height: 20
			})
		).append(`<strong>L${level}</strong>`)
	);
	mw.hook('wikipage.indicators').fire($icon);
	$(() => {
		$('.mw-indicators').prepend($icon, '\n');
	});
});