Jump to content

User:Nardog/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.
// 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' &&
(async function () {
	let index = mw.config.get('wgTitle')[0];
	if (!/[A-Z]/.test(index)) {
		index = 'others';
	}
	let data;
	try {
		data = JSON.parse(
			(await $.ajax(`/w/rest.php/v1/page/Wikipedia:Vital_articles/data/${index}.json`, {
				headers: {
					'Api-User-Agent': 'VitalTopicon (https://en.wikipedia.org/wiki/User:Nardog/VitalTopicon.js)'
				}
			})).source
		)[mw.config.get('wgTitle')];
	} catch {}
	if (!data) return;
	let $icon = $('<div>').attr({
		id: 'mw-indicator-vital-article',
		class: 'mw-indicator'
	}).append(
		$('<a>').attr({
			href: '/wiki/Wikipedia:Vital_articles/Level/'   data.level,
			title: `This is a level-${data.level} vital article in ${data.topic}${
				data.sublist ? ` (${data.sublist})` : ''
			}.`
		}).append(
			$('<img>').attr({
				alt: 'Vital article',
				src: '//upload.wikimedia.org/wikipedia/commons/1/13/Círculos_Concéntricos.svg',
				width: 20,
				height: 20
			})
		)
	);
	mw.hook('wikipage.indicators').fire($icon);
	await $.ready;
	$('.mw-indicators').prepend($icon, '\n');
}());