Jump to content

User:Jeeputer/PCBIndicator.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.
// Fork of [[User:Jeeputer/defconIndicator.js]] (Which is
// itself a fork of [[User:Enterprisey/watchlist-notice.js]])
// to show Pending Changes Backlog icon on
// top of the page, next to the notification icon

mw.loader.using('mediawiki.util', function() {
	var showPCB = function(icon, level, numPages) {
		$('#pt-notifications-notice').after(
			$('<li>')
				.append(
					$('<span>')
					.css({
						'font-size': '80%'
					})
					.text('PCB:')
					.attr({
						'title': 'Pending Changes Backlog'
					})
				)
				.append(
					$('<img width="20" height="20" style="cursor:pointer;" title="Pending Changes Backlog level: ' +
						level + '; Click to update!">')
					.css({
						'padding': '0.25em 0.45em 0.2em',
						'cursor': 'pointer'
					})
					.attr('src',
						'/media/wikipedia/commons/' +
						icon)
					.click(updatePCB)
				)
				.append(
					$('<span id="pcb-numpages">')
					.css({
						'font-size': '80%'
					})
					.text(numPages + ' page' + (parseInt(numPages) == 1 ? '' : 's'))
				)
            .css({
                'opacity': '1',
                'transition': 'opacity 0.5s'
            })
			.attr('id', 'pcb-indicator')
		);
	};
	var changeIcon = function(icon, level, numPages) {
		$('#pcb-indicator img').attr(
			'src',
			'/media/wikipedia/commons/' +
			icon,
			'title', 'Pending Changes Backlog: ' + level + '; Click to update!'
		);
		$('#pcb-numpages').text(numPages + ' page' + (parseInt(numPages) == 1 ? '' : 's'))
		$('#pcb-indicator').css('opacity', '1');
	};
	var updatePCB = function() {
		$('#pcb-indicator').css('opacity', '0');
		$.getJSON(
			mw.util.wikiScript('api'), {
				format: 'json',
				action: 'parse',
				page: 'User:DatBot/pendingbacklog',
				prop: 'wikitext'
			}).done(function(data) {
			var colors = {
				'1': '6/6d/Ledred.png',
				'2': '8/80/Ledorange.png',
				'3': '0/0a/Ledyellow.png',
				'4': '5/58/Ledgreen.png',
				'5': '7/71/Ledblue.png'
			};
			var wikitext = data.parse.wikitext['*'];
			var level = wikitext.match(/\|\s*level\s*\=\s*([0-9])/)[1];
			var numPages = wikitext.match(/\|\s*info\s*\=\s*([0-9]*)/)[1];
			if (level) {
				var icon = colors[level];
				if (!$('#pcb-indicator').length) {
					showPCB(icon, level, numPages);
				} else {
					changeIcon(icon, level, numPages);
				}
			} else {
				mw.notify($('<span>Looks like the wikitext at the ' +
					'<a href="/wiki/User:DatBot/pendingbacklog">pendingbacklog page</a> ' +
					'has been changed so the script cannot find the level.</span>'
					), {
					type: 'error',
					autoHide: 'false',
					tag: 'pcb-error'
				});
			}
		});
	};
	$(document).ready(function() {
		updatePCB();
		window.setInterval(updatePCB, 120000);
	});
});