Jump to content

User:קיפודנחש/watchlistMark.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.
// the usual prayer
if (mw.config.get('wgCanonicalSpecialPageName') == "Contributions" || mw.config.get('wgNamespaceNumber') == 14)
mw.loader.using('mediawiki.api', function() { $(function() {
		
	function i18n(key) {
		switch (mw.config.get('wgUserLanguage')) {
			case 'he':
				switch (key) {
					case 'watch':
						return '[עקוב]';
					case 'unwatch':
						return '[הסר]'
					case 'watchhint':
						return 'הוסף לרשימת המעקב שלי';
					case 'unwatchhint':
						return 'הסר מרשימת המעקב שלי';
					case 'showwatchlinks':
						return 'הצג קישוריות מעקב';
				}
				break;
			default:
				switch (key) {
					case 'watch':
						return '[watch]';
					case 'unwatch':
						return '[unwatch]'
					case 'watchhint':
						return 'Add to my Watchlist';
					case 'unwatchhint':
						return 'Remove from my watchlist';
					case 'showwatchlinks':
						return 'Show watchlist controls';
				}
				break;
		}
		return key;
	}
			
	var isCat = mw.config.get('wgNamespaceNumber') == 14,
		watchList = {},
		api = new mw.Api();

	function readWatchList(continuation) {
		var params = {list: 'watchlistraw', wrlimit: 500, rawcontinue: ''};
		if (continuation)
			params.wrcontinue = continuation;
		api.get(params, function(data) {
			if (data && data.watchlistraw)
				$.each(data.watchlistraw, function(key, val) { watchList[val.title] = true; });
			if (data && data['query-continue'])
				readWatchList(data['query-continue']['watchlistraw']['wrcontinue']);
			else
				colorWatched();
		});
	}

	function watchText(watch) { return (isCat ? ' ':'') + i18n(watch ? 'watch' : 'unwatch');}
	function watchTitle(watch){ return i18n(watch ? 'watchhint' : 'unwatchhint');}

	function colorWatched() {

		var pagesList = isCat ? $('#mw-pages').find('li') : mw.util.$content.find('ul > li');
		var all = [];
		var all = pagesList.each(function() {
			var li = $(this);
			var line = li.children().filter('a:last');
			var page = line.attr('title');
			var watched = watchList[page] || false;
			if (watched)
				line.addClass('iswatched');
			var watchLink = $('<span>', {'class': watched? 'unwatchit-link' : 'watchit-link', title: watchTitle( !watched)})
				.text(watchText(! watched))
				.click(function() {watchIt($(this), page)})
			li.append(watchLink);
		});
	}

	function watchIt(span, page) {
		var watched = span.hasClass('unwatchit-link');
		api[watched ? 'unwatch' : 'watch'](page, function() {
				var lines=$('a').filter(function(){return $(this).text()==page});
				lines.parent().children('span.watchit-link, span.unwatchit-link')
					.toggleClass('unwatchit-link  watchit-link')
					.text(watchText(watched))
					.attr({title: watchTitle(watched)});
				lines.toggleClass('iswatched');
		});
	}

	mw.util.addCSS(
		".unwatchit-link{color:#a00; cursor: pointer; font-size: 70%;display:none}  \n" +
		".watchit-link{color:#008; cursor: pointer; font-size: 70%;display:none}  \n" +
		".iswatched{font-weight: bolder;}  \n" +
		"</style> "
	)
	
	readWatchList();
	
	var showLink = $('<a>', {href: '#', text: i18n('showwatchlinks')}).click(
		function(){
			mw.util.addCSS('.unwatchit-link,.watchit-link{display:inline;}');
			$(this).remove();
		}
	);
	if ($('#contentSub a').length)
		$('#contentSub a:last').after(showLink).after(' | ');
	else
		$('#contentSub').append(showLink);
})}); // using ... ready