User:Christo76/js/watchCat.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.
/*
|
|  author:	Kaligula
|  license:	CC-BY-SA-3.0
|  version:	2.3.5
|  credits:	Matma Rex
|               Nux
| _____________________________________________________________________________________
|
|  opis działania: [[Wikipedysta:Kaligula/js/watchCat]]
\*_____________________________________________________________________________________*/

//if ( (wgNamespaceNumber==14 && wgAction=='view') || wgPageName=='Specjalna:GadgetPrefs' ) {

	addOnloadHook(function () {

          importStylesheet('User:Christo76/js/watchCat.css');

			$.ajax({
				url:location.protocol+'//en.wikipedia.org/wiki/Special:EditWatchlist/raw'
			}).done(function(html){
				var wList = $(html).find('#mw-input-wpTitles').text();

				$('#mw-pages a').each(function(i){
					if ( (wList.match(new RegExp('^'+this.innerHTML.replace(/([\(\)\[\]\{\}\|\?\.\*\^\$\+])/g,'\\$1')+'$','m'))!=null) == (watchCat_invert == false)) {
					//there's a match and we don't invert selection OR there's no match and we invert selection
						$(this).toggleClass('watchCat-main');

					}
				});
				
								$('#mw-subcategories a').each(function(i){
					if ( (wList.match(new RegExp('^'+'Category:'+this.innerHTML.replace(/([\(\)\[\]\{\}\|\?\.\*\^\$\+])/g,'\\$1')+'$','m'))!=null) == (watchCat_invert == false)) {
					//there's a match and we don't invert selection OR there's no match and we invert selection
						$(this).toggleClass('watchCat-main');

					}
				});

				if (watchCat_pending || watchCat_changed) {

					function watchCatPendChanArr() {
						var arr=[],j=0,i;
						$('#mw-pages a.watchCat-main').each(function(i){
							if (i%50==0) { j=i/50; arr[j] = []; }
							arr[j].push(this.innerHTML)
						})
						arr.forEach(function(el, i){ arr[i]=el.join('|'); })
						return arr;
					}

					if (watchCat_changed) {
	//					function watchCatChan() {
							var arr = watchCatPendChanArr();
							window.watchCatStamps = {};
							window.watchCatChanCounter = 0;
							arr.forEach(function(el, i){
								$.ajax({
									url:location.protocol+'//en.wikipedia.org/w/api.php?action=query&prop=info&format=json&inprop=watched%7Cnotificationtimestamp&indexpageids=&titles=' + encodeURI(arr[i])
								}).done(function(data){
									watchCatChanCounter++;
									data.query.pageids.forEach(function(elm, j){
										if (data.query.pages[elm].watched == "") {
											watchCatStamps[data.query.pages[elm].title] = (data.query.pages[elm].notificationtimestamp != "");
										};
									});
									if (watchCatChanCounter==arr.length){
										$('#mw-pages a.watchCat-main').each(function(){
											(watchCatStamps[this.innerHTML]) ? ($(this).toggleClass('watchCat-watched-changed')) : {};
										});
									}
								});
							});
	//					}
					}
					
					function watchCatPend() {
						var arr = watchCatPendChanArr();
						window.watchCatFlags = {};
						window.watchCatPendCounter = 0;
						arr.forEach(function(el, i){
							$.ajax({
								url:location.protocol+'//en.wikipedia.org/w/api.php?action=query&indexpageids&prop=flagged&format=json&titles=' + encodeURI(arr[i])
							}).done(function(data){
								watchCatPendCounter++;
								data.query.pageids.forEach(function(elm, j){
									watchCatFlags[data.query.pages[elm].title] = ( (!data.query.pages[elm].flagged) ? "watchCat-flagged-unreviewed" : ((data.query.pages[elm].flagged.pending_since) ? "watchCat-flagged-pending" : "watchCat-flagged-reviewed") );
								});
								if (watchCatPendCounter==arr.length){
									$('#mw-pages a.watchCat-main').each(function(index){
										$(this).toggleClass(watchCatFlags[this.innerHTML]);
									});
								}
							});
						});
					}

					if (watchCat_pending) {
						watchCatPend();
					}
					else {
						$('#mw-pages > p')[0].outerHTML += '\n→ <a href=# onclick="watchCatPend()">Sprawdź wersje oznaczone obserwowanych artykułów</a><br>\n→ Narzędzia zewnętrzne: <a href="http://toolserver.org/~magnus/deep_insight.php?language=pl&category=' + wgTitle + '&depth=10&mode=web&doit=Do+it" class="external text">wersje nieprzejrzane</a>/<a href="http://toolserver.org/~magnus/deep_out_of_sight.php?language=pl&category=' + wgTitle + '&depth=10&mode=web&doit=Do+it" class="external text">wersje oczekujące na ponowne przejrzenie</a>'
					}
				}
			});
		});
	});
//}