User:Writ Keeper/Scripts/rollbackWatchlister.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.
$(document).ready(watchlister);

function watchlister()
{
	var i;
	var array = $(".mw-rollback-link");
	for(i = 0; i < array.length; i++)
	{
		var anchor = (array[i].getElementsByTagName("a"))[0];
		anchor.setAttribute("onclick", "watchlistOnClick('" + anchor.href + "'); return false;");
		anchor.setAttribute("href", "#");
	}
}
function watchlistOnClick(url)
{
	var regex = /title=.+(?=&action)/

	var targetPage = regex.exec(url);
	var data = {
		format : 'json',
		action : 'query',
		meta : 'tokens',
		type : 'watch'
	}
	
	$.ajax({
		url : mw.util.wikiScript('api'),
		type : "POST", 
		dataType : 'json',
		data : data,

		success : function(data) {
			var token = data.responseJSON.query.tokens.watchtoken;
			alert(url.replace(/action=rollback.+/, "action=watch") + "&token=" + token);
			window.location = url;
		},
		error : function() {
			alert('Ajax error.');
		}
		});
}