User:Perhelion/viewerInfo.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
@Description: Anzahl der Beobachter für Wikiseiten rechts neben dem Titel
@Author:
 * adapted from de:User:Nightfly85;
 * modified & simplified & international (multi-)language support by Perhelion;
 **/
/*global $, mw*/
if (mw.config.get('wgAction') === 'view' && mw.config.get('wgIsArticle') && !(mw.libs.viewerInfo instanceof Object)) {
	mw.libs.viewerInfo = function () {
		"use strict";
		var msg = ($.inArray('de', mw.language.getFallbackLanguageChain()) !== -1) ?
			{ scarcely: 'Kaum', watchers: ' Beobachter'	} : { /* Localized strings */
			scarcely: 'scarcely',
			watchers: ' watchers'
		};
		$.getJSON(
			'https://' + location.hostname + '/w/api.php?action=query&format=json&prop=info&inprop=watchers&titles=' + mw.config.get('wgPageName'))
		.done(function (data) {
			if (!data || !data.query || !data.query.pages)
				return;
			data = data.query.pages;
			for (var w in data)
				w = data[w];
			if (w) {
				w = w.watchers || msg.scarcely;
				$('#firstHeading').append(
					$('<small>', {
						id: 'ca-n-watchers',
						style: 'margin-left:2em',
						text: w + msg.watchers
					}));
			}
		});
	};
	$.when(mw.loader.using(['mediawiki.language']), $.ready).then(mw.libs.viewerInfo);
}