User:Rutilant/ajax-watchlist.js

This is an old revision of this page, as edited by Rutilant (talk | contribs) at 09:03, 3 January 2019 (updated). 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 2, 2019
	Status: Experimental
*/

var watchlist_ajax={timer:null,status:false};
$(function(){
	if(test_currentPage){
		var top_content="<input id='watchlist-ajax-timeOut' placeholder='Refresh rate in milliseconds'><button id='a-w-start' onclick='start_update_w()'>Start</button><button id='a-w-stop' onclick='stop_update_w()'>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);
	}
});

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;
		update_watchlistAjax(timeout);
		$("#aj-w-status").html(`running (${timeout} ms)`);
	}	
}
function stop_update_w(){
	watchlist_ajax.status=false;
	clearTimeout(watchlist_ajax.timer);
	$("#aj-w-status").html('stopped');
}
function update_watchlistAjax(timeout){
	if(test_currentPage() && watchlist_ajax.status){
		$.ajax({
			url: '/wiki/Special:Watchlist',
			type:'GET',
			loop_timeout:timeout,
			success: function(data) { 
				var c_div = $( '<div></div>' ); // dummy DOM element to parse the string as HTML
				c_div.html(data);
				$(".mw-changeslist-legend", c_div).remove(); //remove the changelist legend
				var watchlist_data=$(".mw-changeslist", c_div);
				$("#auto-updating-watchlist").html(watchlist_data.html());
				$('span.mw-rollback-link a').click(function(e) {ajax_rollback(e, this)});
			},
			error:function(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 ajax_rollback(e, clicked) {
	// originally from AjaxRollback (https://en.wikipedia.org/wiki/User:Abelmoschus_Esculentus/AjaxRollback.js)
	e.preventDefault();
	var $this = $(clicked);
	var href = $this.attr("href");
	$this.html('rollbacking...');
	$this.attr("href", null);
	$.ajax({
		url: href,
		success: function(response) {
			var error, other_error;
			try{
				var h_element = $( '<div></div>' ); 
				h_element.html(response);
				var firstHeading = $("#firstHeading", h_element).html(); 
				if(firstHeading!=="Rollback failed"){
					error=false;
				}else{
					error=true;
				}
			}catch(n){
				error="unknown";
				other_error=n;
			}
			if(!error){
				$this.html('reverted');
			}else if(error==="unknown"){
				$this.html('unknown response');
				$this.attr("href", this.url);
				console.log(other_error);
			}else{
				$this.html('rollback failed');
				$this.attr("href", this.url);		
			}
		},
		error: function(e) {
			$this.html('rollback failed');
			$this.attr("href", this.url);
			console.log(e);
		}
	});
}