User:קיפודנחש/watchlistScout.js

mw.loader.using( ['mediawiki.api', 'mediawiki.user', 'mediawiki.util'], function {

function i18n(key) { switch (mw.config.get('wgUserLanguage')) { case 'he': switch ( key ) { case 'history': return 'היסטוריה'; case 'you have': return 'יש שינויים שטרם קראת ב'; case 'watchlist': return 'רשימת המעקב שלך'; case 'more': return 'ועוד ' + arguments[0] + ' דפים.'; case 'H': return '(ה)'; }				break; default: switch ( key ) { case 'history': return 'History'; case 'you have': return 'Unviewed pages in '; case 'watchlist': return 'your watchlist'; case 'more': return 'and ' + arguments[0] + ' more.'; case 'H': return '(H)'; }								break; }		return key; }	var api = new mw.Api; function announce(titles) { function press { this.href = $(this).hasClass('count') ? mw.util.getUrl($(this).prev.text) + '?action=history' : mw.util.getUrl($(this).text); }		var p = $(' '); $(titles.splice(0, 6)).each(function(index, item) {			p			.append($('', {href: '#'}).text(item).click(press))			.append(' ')			.append($('', {href: '#', title: i18n('history'), 'class': 'count'}).text(i18n('H')).click(press))			.append('; ');		}); if (titles.length) // it had more than 6. p.append($(' ', {title: i18n('more', titles.length)}).text(i18n('...'))); $(' ', {'class': 'usermessage'}) .append(i18n('you have')) .append($('', {href: mw.util.getUrl('Special:Watchlist'), text: i18n('watchlist')})) .append(p) .prependTo(mw.util.$content); }	function calcParams { var opts = { /* watchlisthideown: we do not care about this one, since "unread" will never have own edits anyway */ watchlisthideanons: '!anon', watchlisthidebots:	'!bot', watchlisthideliu: 	'anon', /* this strangely named option means "hide regitered users", IOW, show anons */ watchlisthideminor:	'!minor', watchlisthidepatrolled: '!patrolled' },			show = ['unread']; $.each( opts, ( k, v ) => { if ( mw.user.options.get( k ) ) show.push( v ); } ); return { list: 'watchlist', wlprop: 'ids|user|title|timestamp|notificationtimestamp', wlshow: show.join( '|' ), wltype: mw.user.options.get( 'watchlisthidecategorization' ) ? 'edit|new|log' : 'edit|new|log|categorize', wllimit: 50 };	}

function checkNotif { api.get( calcParams ) .done(			function(data) {				var counts = {}, 					titles = [];				if (data && data.query && data.query.watchlist)					$(data.query.watchlist).each(function(index, item) { if (item.notificationtimestamp) { if (counts[item.title]) counts[item.title]++; else { counts[item.title] = 1; titles.push(item.title); }						}					});					if (titles.length)						announce(titles, counts);					else						setTimeout(checkNotif, 60000);			}		); }	checkNotif; });