Jump to content

User:קיפודנחש/watchlistScout.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.
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 = $('<p>');
		$(titles.splice(0, 6)).each(function(index, item) {
			p
			.append($('<a>', {href: '#'}).text(item).click(press))
			.append(' ')
			.append($('<a>', {href: '#', title: i18n('history'), 'class': 'count'}).text(i18n('H')).click(press))
			.append(';&nbsp;&nbsp;');
		});
		if (titles.length) // it had more than 6.
			p.append($('<span>', {title: i18n('more', titles.length)}).text(i18n('...')));
		$('<div>', {'class': 'usermessage'})
			.append(i18n('you have'))
			.append($('<a>', {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();
});