לדלג לתוכן

משתמש:קיפודנחש/watchlistWatcher.js

מתוך ויקיפדיה, האנציקלופדיה החופשית

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
mw.loader.using('mediawiki.api', 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[1] + ' דפים.';
					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[1] + ' more.';
					case 'H':
						return '(H)';
				}				
				break;
		}
		return key;
	}
	
	var api = new mw.Api({cache: false});
	
	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 4.
			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)
			.insertAfter('#contentSub');
	}
	
	function checkNotif() {
		api.get( { list: 'watchlist', wlprop: 'title|notificationtimestamp', wlshow: '!bot', wllimit: 50 })
			.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();
});