משתמש:Yova/common.js/watchlistAnnounce.js

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

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
(function() {

	function tsToDate(timestamp) {
			var date = new Date(timestamp);
			if (isNaN(date)) {// all sane browsers can just do new Date(rc.timestanp). only IE refuses.
					dar = timestamp.split(/[^\d]/); // timestamp looks like so: "2011-05-05T18:56:27Z"
					var month = parseInt(dar[1],10) - 1; // "Date" expexts months in the range of 0..11, timestamp is more conventional.
					var minutes = parseInt(dar[4], 10) - new Date().getTimezoneOffset();//ts is UTC, but new Date(y,M,d,h,m,s) assumes local TZ
					date = new Date(dar[0],month,dar[2],dar[3],minutes,dar[5]);
			}
			return date;
	}

	function ago(ms) {
			var s = ms / 1000;
			if (s < 100)
					return 'הרגע';
			var ranges = [
							{r: 60, t: 'דקות', s: 'דקה', d: 'שתי דקות'},
							{r: 60, t: 'שעות', s: 'שעה', d: 'שעתיים'},
							{r: 24, t: 'ימים', s: 'יממה', d: 'יומיים'},
							{r: 7, t: 'שבועות', s: 'שבוע', d: 'שבועיים'},
							{r: 52, t: 'שנים', s: 'שנה', d: 'שנתיים'}
					];

			var est = '', num;
			for (var i = 0; i < ranges.length; i++) {
					var range = ranges[i];
					s /= range.r;
					if (s < 1)
							break;
					num = '';
					if (s < 2)
							est = range.s;
					else if (s < 3 && range.d)
							est = range.d;
					else {
							num = '-' + Math.floor(s) + ' ';
							est = range.t;
					}
			}
			return 'לפני כ' + num + est;
	}
 
	
	function l(p,t) {
		return $('<a>', {href: mw.util.getUrl(p), text: t || p});
	}
	
    function WatchlistItem(item) {
		this.cookieName = 'watchlistAnnounce_' + encodeURI(wgUserName).replace(/%/g, '');
		$.extend(this, item);
		this.wasThere = function() {
			if (this.title == wgPageName.replace(/_/g, ' '))
				return true;
			var cookie = $.cookie(this.cookieName);
			if (!cookie)
				return false;
			var ar = cookie.split('\t');
			return wgUserName == ar[0] 
				&& this.title == ar[1] 
				&& this.timestamp == ar[2] 
				&& ar[3] == "was there";
		}
		
		this.sizeSpan = function() {
			var 
				d = this.newlen - this.oldlen,
				color = d < 0 ? 'red' : d > 0 ? 'green' : 'grey',
				sign = d < 0 ? '-' : d > 0 ? '+' : '';
			return $('<a>', {href: mw.util.getUrl(this.title) + '?diff=' + this['new_revid'], title: 'הבדל בין הגרסאות'})
				.html($('<span>').css({color: color}).text(' (' + Math.abs(d) + sign + ') '));
		}
		
		this.makeLine = function() {
			var 
				page = l(this.title),
				user = l('User:' + this.user, this.user)
						.append(' (')
						.append(l('Special:Contributions/' + this.user, 'תרומות'))
						.append(', ')
						.append(l('User Talk:' + this.user, 'שיחה'))
						.append(') '),
				ts = tsToDate(this.timestamp);
			return $('<div>')
				.append(l('Special:Watchlist', 'רשימת מעקב: '))
				.append('<br/>' + ago(new Date() - ts) + ' בדף:')
				.append(page)
				.append('&rlm;')
				.append(this.sizeSpan())
				.append('<br/>')
				.append(user)
				.append('<br/>')
				.append('(' + this.comment + ')')
				.html();
		}
		var ar = [wgUserName, this.title, this.timestamp, this.wasThere() ? "was there" : ""];
		$.cookie(this.cookieName, ar.join('\t'), {path: '/', expires: 3});
	}
	
	function checkWatchlist() {
		$.getJSON(mw.util.wikiScript('api'), {
			action: 'query',
			list: 'watchlist',
			wllimit: 1,
			wlexcludeuser: wgUserName,
			wlprop: 'user|title|timestamp|comment|sizes|ids',
			format: 'json'
			},
			function(data) {
				if (data && data.query && data.query.watchlist) {
					var item = new WatchlistItem(data.query.watchlist[0]),
						timer = null,
						control = $('#pt-watchlist a');
					if (!item.wasThere()) 
						mw.loader.using('jquery.tipsy', function() {
							$(function() {
								control
								.css({'background-color': 'yellow'})
								.tipsy({delayOut: 1500, 
										html: true,
										trigger: 'manual',
										title: function() {
											return item.makeLine()
										}
								}).unbind('mouseover mouseout')
								.mouseover(function() {
										control.tipsy('show')
								})
								.mouseout(function() {
										timer = setTimeout(function(){
												control.tipsy('hide')
										}, 1000);
								});
                                $('.tipsy')
									.unbind('mouseover mouseout')
									.live('mouseover', function() {
											clearTimeout(timer);
									})
									.live('mouseout', function(){
											timer = setTimeout(function(){
													control.tipsy('hide')
											}, 1000);
									});							
							});
						});
				}
			}
		);
	}


	if (wgCanonicalSpecialPageName != "Watchlist") {
		checkWatchlist();
		setInterval(checkWatchlist, 5000);
	}
})();