MediaWiki:Gadget-protectionLocks.js

From Support Wiki
Jump to navigation Jump to search

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.
// Code adopted from Minecraft Wiki ( https://minecraft.wiki/w/MediaWiki:Gadget-protectionLocks.js )
// Page protection indicators
;(function($, mw) {
	'use strict';

	const config = mw.config.get([
		'wgRestrictionEdit',
		'wgIsMainPage',
		'wgAction'
	]);
	const protectionLevelData = config.wgRestrictionEdit;
	if (
		// Null on nonexistent or special pages. Avoids a crash there.
		!protectionLevelData || config.wgIsMainPage ||
		// No need to display the indicator when viewing history or editing the page
		config.wgAction !== 'view') {
		return;
	}

	function getImageThumbnailURL(name, store, size) {
		const encodedName = mw.util.wikiUrlencode(name);
		return 'https://commons.wiki.gg/images/' + store + '/' + encodedName;
	}

	function mimicIndicator(id, imgName, imgStore, title) {
		return $('<div class="mw-indicator">').attr({
				'id': 'mw-indicator-' + id
			}).append($('<div class="mw-parser-output">')
			.append($('<span typeof="mw:File">')
			.append($('<a>')
			.attr({
				'title': title
			}).append($('<img>')
				.attr({
				'alt': title,
				'src': getImageThumbnailURL(imgName, imgStore, 25),
				'srcset': getImageThumbnailURL(imgName, imgStore, 38) +
					' 1.5x, ' +
					getImageThumbnailURL(imgName, imgStore, 50) +
					' 2x',
				'width': '25',
				'height': '25'
				})
			))));
	}

	const protectionLevel = protectionLevelData[0];
	if (protectionLevel === 'autoconfirmed') {
		mimicIndicator(
			'protection-semi',
			'Template-badinfo.svg',
			'6/66',
			'This page is semi-protected so that only registered users can edit it.'
		).appendTo($('.mw-indicators'));
	} else if (protectionLevel === 'sysop') {
		mimicIndicator(
			'protection-full',
			'Protected.svg',
			'8/86',
			'This page is fully protected so that only administrators can edit it.'
		).appendTo($('.mw-indicators'));
	}
})(window.jQuery, window.mediaWiki);