User:Rutilant/ajax-watchlist.js

This is an old revision of this page, as edited by Rutilant (talk | contribs) at 06:58, 15 January 2019 (...). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/*
Last update: January 15, 2019
Status: Experimental
*/
(function() {
	var watchlist_ajax={timer:null,status:false}, token=false, lasttime=false;
	if(test_currentPage()){
		var top_content="<input id='watchlist-ajax-timeOut' placeholder='Refresh rate in milliseconds'><button id='a-w-start'>Start</button><button id='a-w-stop'>Stop</button><br>Status: <span id='aj-w-status'>not running</span>";
		var main_content="<div id='auto-updating-watchlist'></div>";
		$("div#watchlist-ajax").html(top_content+main_content);
		$("#a-w-start").click(function(){start_update_w()});
		$("#a-w-stop").click(function(){stop_update_w()});
	}

	function start_update_w(){
		if(test_currentPage() && !watchlist_ajax.status){
			var timeout=$.isNumeric($("#watchlist-ajax-timeOut").val())?$("#watchlist-ajax-timeOut").val():5000;
			watchlist_ajax.status=true;
			lasttime=false;
			$("#auto-updating-watchlist").html("");
			update_watchlistAjax(timeout);
			$("#aj-w-status").html(`running (${timeout} ms)`);
		}
	}
	function stop_update_w(){
		watchlist_ajax.status=false;
		clearTimeout(watchlist_ajax.timer);
		lasttime=false;
		$("#aj-w-status").html('stopped');
	}

	function update_watchlistAjax(timeout){
		if(test_currentPage() && watchlist_ajax.status){
			var start_from="";
			if(lasttime!==false){
				start_from="&wlstart="+lasttime+"&wldir=newer";
			}
			$.ajax({
				type: 'GET',
				url: '/w/api.php?action=query&list=watchlist&wllimit=30&wlprop=ids|title|sizes|flags|user|comment&format=json&curtimestamp=true&wlallrev=true'+start_from,
				dataType: 'json',
				loop_timeout:timeout,
				success:function(returndata){
					var sitelink=''; /* for local testing */
					var watchlist_items="";
					lasttime=returndata.curtimestamp;
					returndata=returndata.query.watchlist;
					if(returndata.length>0){
						returndata.forEach(function(e){
							var length=e.newlen-e.oldlen;
							var edit_summary=e.comment.substring(0, 400)||"<span style='color:#cecccc'>No edit summary.</span>" /* trim extremly long edit summary*/ ; 
							edit_summary=edit_summary.replace(/\[\[(.*?)\|(.*?)\]\]/g, "<a href=\"/wiki/$1\">$2</a>"); /* parse wikilinks */
							var difflink=sitelink+`/w/index.php?title=${e.title}&diff=${e.revid}`;
							var histlink=sitelink+`/w/index.php?title=${e.title}&action=history`;
							var userlink=`
							<a href='${sitelink}/wiki/User:${e.user}'>${e.user}</a>
							(<a href='${sitelink}/wiki/User talk:${e.user}'>talk</a> | 
							<a href='${sitelink}/wiki/Special:Contributions/${e.user}'>contrib</a>)
							`;
							length=(length<0?'<span style=color:red>'+length+'</span>':'<span style=color:green>+'+length+'</span>');
							watchlist_items+=`
							<div style='margin-bottom:5px; padding:3px; font-family:Calibri; border-bottom:1px dotted gray; width: 100%; word-break:break-all'>
							(<a href="${difflink}">diff</a> | <a href="${histlink}">hist</a>)
							${e.title} (${e.type}) (${length}) &mdash; ${userlink} (<span style='font-style:italic'>${edit_summary}</span>) [<span class="mw-rollback-link"><a href='#' class='aj-rollback-btn' data-rollback-pageid="${e.pageid}" data-rollback-user="${e.user}">ajax rollback</a></span>]
							</div>
							`;
						});
						$('.b-border-div').remove();
						$("#auto-updating-watchlist").prepend(watchlist_items+"<div style='border-bottom:2px solid skyblue;' class='b-border-div'></div>");
						$(".aj-rollback-btn").click(function(e) {custom_rollback(e, this)});
					}
				},
				error:e=>console.log(e),
				complete:function(){
					var timeout=this.loop_timeout;
					watchlist_ajax.timer=setTimeout(function(){update_watchlistAjax(timeout)}, timeout);
				}
			});		
		}
	}

	function test_currentPage(){
		if($("div#watchlist-ajax")){
			return true;
		}else{
			return false;
		}
	}
	
	function getToken(first_par, second_par){
		if(!token){
			$.ajax( "/w/api.php?action=query&meta=tokens&type=rollback&format=json" ).done(data=> {
				token=data.query.tokens.rollbacktoken; 
				custom_rollback(first_par, second_par, true);
			});
		}else{
			custom_rollback(first_par, second_par, true);
		}
	}
	
	function custom_rollback(e, clicked, withToken){
		e.preventDefault();
		if($(clicked).attr('href')===null){
			return;
		}
		if(!withToken){
			$(clicked).text("getting token");
			getToken(e, clicked);
			return;
		}
		var ID=$(clicked).attr("data-rollback-pageid");
		var user=$(clicked).attr("data-rollback-user");
		$(clicked).text('rollbacking...');
		$(clicked).attr("href", null);
		$.ajax({
			type: 'POST',
			url: '/w/api.php',
			c_b: clicked,
			data: {action:"rollback", pageid:ID, user:user, token:token, format:'json'},
			success:function(e){
				if(!e.error){
					$(this.c_b).text('reverted');
				}else{
					$(this.c_b).text(e.error.code);
					$(this.c_b).attr("href", "#");
					console.log(e);
				}
			},
			error:function(e){
				$(this.c_b).text('rollback failed');
				$(this.c_b).attr("href", "#");
				console.log(e);
			}
		});
	}
})();