Użytkownik:Expert3222/patrolModule.js: Różnice pomiędzy wersjami

Z Nondanych
m (użyteczne komunikaty debugowania)
 
(Nie pokazano 6 pośrednich wersji utworzonych przez tego samego użytkownika)
Linia 2: Linia 2:
var QuickPatrolDelay = 20; //syfiło w logach, rejestrując patrolowanie po kilka razy
var QuickPatrolDelay = 20; //syfiło w logach, rejestrując patrolowanie po kilka razy


var patrolModule = {};
//użycie: patrolChange(diff).done(callback).fail(callback2);

function patrolChange(diff, tokenAlreadyReset)
//usage: patrolModule.patrolChange(diff).done(callback).fail(callback2);
patrolModule.patrolChange = function(diff, tokenAlreadyReset)
{
{
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay))
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay))
return $.Deferred().reject(); //rejected promise
$.when();
QuickPatrolLastClick = Date.now();
QuickPatrolLastClick = Date.now();
if (diff === null || diff === "" || typeof diff == "undefined")
if (diff === null || diff === "" || typeof diff == "undefined")
{
{
return $.when(); //resolved promise
return $.Deferred().reject();
}
}
Linia 17: Linia 19:
deferred = $.Deferred();
deferred = $.Deferred();
console.log("dupa");
api.post(
api.post(
{
{
Linia 26: Linia 27:
}).done(function(data)
}).done(function(data)
{
{
deferred.resolve();
deferred.resolve(data);
}).fail(function(error)
}).fail(function(error)
{
{
Linia 39: Linia 40:
meta: "tokens",
meta: "tokens",
type: "patrol"
type: "patrol"
}).done(function(data)
}).done(function()
{
{
mw.user.tokens.set("patrolToken", data.query.tokens.patroltoken);
mw.user.tokens.set("patrolToken", data.query.tokens.patroltoken);
patrolChange(diff, true)
patrolChange(diff, true)
.done(function()
.done(function(data)
{
{
deferred.resolve();
deferred.resolve(data);
console.log("NOWY TOKEN PRAWIDŁOWY");
console.log("NOWY TOKEN PRAWIDŁOWY");
}).fail(function()
}).fail(function()
Linia 65: Linia 66:
}
}


//użycie: patrolRangeOfChanges(to, from).done(callback).fail(callback2);
//usage: patrolModule.patrolRangeOfChanges(to, from).done(callback).fail(callback2);
function patrolRangeOfChanges(to, from, tokenAlreadyReset)
patrolModule.patrolRangeOfChanges = function(to, from, tokenAlreadyReset)
{
{
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay))
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay))
return $.when();
return $.Deferred().reject();
QuickPatrolLastClick = Date.now();
QuickPatrolLastClick = Date.now();
if (diff === null || diff === "" || typeof diff == "undefined")
if (to === null || to === "" || typeof to == "undefined")
{
{
return $.when(); //resolved promise
return $.Deferred().reject();
}
}
Linia 95: Linia 96:
api.post(request).done(function(data)
api.post(request).done(function(data)
{
{
deferred.resolve();
deferred.resolve(data);
}).fail(function(error)
}).fail(function(error)
{
{
Linia 112: Linia 113:
mw.user.tokens.set("patrolToken", data.query.tokens.patroltoken);
mw.user.tokens.set("patrolToken", data.query.tokens.patroltoken);
patrolRangeOfChanges(to, from, true)
patrolRangeOfChanges(to, from, true)
.done(function()
.done(function(data)
{
{
deferred.resolve();
deferred.resolve(data);
console.log("NOWY TOKEN PRAWIDŁOWY");
console.log("NOWY TOKEN PRAWIDŁOWY");
}).fail(function()
}).fail(function()
Linia 134: Linia 135:
}
}


//użycie: patrolPage(pageid).done(callback).fail(callback2);
//usage: patrolModule.patrolPage(pageid).done(callback).fail(callback2);
function patrolPage(id, tokenAlreadyReset)
patrolModule.patrolPage = function(id, tokenAlreadyReset)
{
{
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay))
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay))
return $.when();
return $.Deferred().reject();
QuickPatrolLastClick = Date.now();
QuickPatrolLastClick = Date.now();
if (diff === null || diff === "" || typeof diff == "undefined")
if (id === null || id === "" || typeof id == "undefined")
{
{
return $.when(); //resolved promise
return $.Deferred().reject();
}
}
Linia 157: Linia 158:
}).done(function(data)
}).done(function(data)
{
{
deferred.resolve();
deferred.resolve(data);
}).fail(function(error)
}).fail(function(error)
{
{
Linia 174: Linia 175:
mw.user.tokens.set("patrolToken", data.query.tokens.patroltoken);
mw.user.tokens.set("patrolToken", data.query.tokens.patroltoken);
patrolPage(id, true)
patrolPage(id, true)
.done(function()
.done(function(data)
{
{
deferred.resolve();
deferred.resolve(data);
console.log("NOWY TOKEN PRAWIDŁOWY");
console.log("NOWY TOKEN PRAWIDŁOWY");
}).fail(function()
}).fail(function()
Linia 196: Linia 197:
}
}


function resetLastClick()
//usage: patrolModule.resetLastClick()
//before every call to any of above functions
patrolModule.resetLastClick = function()
{
{
QuickPatrolLastClick = 0;
QuickPatrolLastClick = 0;

Aktualna wersja na dzień 13:25, 20 maj 2020

var QuickPatrolLastClick = 0; //https://stackoverflow.com/questions/28609994/javascript-prevent-function-from-executing-multiple-times
var QuickPatrolDelay = 20; //syfiło w logach, rejestrując patrolowanie po kilka razy

var patrolModule = {};

//usage: patrolModule.patrolChange(diff).done(callback).fail(callback2);
patrolModule.patrolChange = function(diff, tokenAlreadyReset)
{
	if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay))
    	return $.Deferred().reject(); //rejected promise
	QuickPatrolLastClick = Date.now();
	
	if (diff === null || diff === "" || typeof diff == "undefined")
	{
		return $.Deferred().reject();
	}
	
	var api = new mw.Api(),
		deferred = $.Deferred();
	
	api.post(
	{
		action: 'fmod-patrol',
		format: 'json',
		revid: diff,
		token: mw.user.tokens.get("patrolToken")
	}).done(function(data)
	{
		deferred.resolve(data);
	}).fail(function(error)
	{
		if (!tokenAlreadyReset)
		{
			console.log("TOKEN PATROLOWANIA NIEPRAWIDŁOWY - DRUGA PRÓBA");
			api.post
			({
				format: "json",
				formatversion: "2",
				action: "query",
				meta: "tokens",
				type: "patrol"
			}).done(function()
			{
				mw.user.tokens.set("patrolToken", data.query.tokens.patroltoken);
				patrolChange(diff, true)
				.done(function(data)
				{
					deferred.resolve(data);
					console.log("NOWY TOKEN PRAWIDŁOWY");
				}).fail(function()
				{
					deferred.reject();
					console.log("NOWY TOKEN NIEPRAWIDŁOWY");
				});
			});
		}
		else // w przypadku pierwszego faila to zostanie wykonane przez funkcję wywołaną rekursywnie
		{
			console.log(error);
			
			deferred.reject();
		}
	});
	
	return deferred.promise();
}

//usage: patrolModule.patrolRangeOfChanges(to, from).done(callback).fail(callback2);
patrolModule.patrolRangeOfChanges = function(to, from, tokenAlreadyReset)
{
	if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay))
    	return $.Deferred().reject();
	QuickPatrolLastClick = Date.now();
	
	if (to === null || to === "" || typeof to == "undefined")
	{
		return $.Deferred().reject();
	}
	
	var api = new mw.Api(),
		deferred = $.Deferred();
	
	var request = 
	{
		action: 'fmod-patrol',
		format: 'json',
		maxrevid: to,
		token: mw.user.tokens.get("patrolToken")
	};
	if (from !== null && from <= to && from >= 0)
	{
		$.extend(request, {
			minrevid: from
		});
	}
	api.post(request).done(function(data)
	{
		deferred.resolve(data);
	}).fail(function(error)
	{
		if (!tokenAlreadyReset)
		{
			console.log("TOKEN PATROLOWANIA NIEPRAWIDŁOWY - DRUGA PRÓBA");
			api.post
			({
				format: "json",
				formatversion: "2",
				action: "query",
				meta: "tokens",
				type: "patrol"
			}).done(function(data)
			{
				mw.user.tokens.set("patrolToken", data.query.tokens.patroltoken);
				patrolRangeOfChanges(to, from, true)
				.done(function(data)
				{
					deferred.resolve(data);
					console.log("NOWY TOKEN PRAWIDŁOWY");
				}).fail(function()
				{
					deferred.reject();
					console.log("NOWY TOKEN NIEPRAWIDŁOWY");
				});
			});
		}
		else // w przypadku pierwszego faila to zostanie wykonane przez funkcję wywołaną rekursywnie
		{
			console.log(error);
			
			deferred.reject();
		}
	});
	
	return deferred.promise();
}

//usage: patrolModule.patrolPage(pageid).done(callback).fail(callback2);
patrolModule.patrolPage = function(id, tokenAlreadyReset)
{
	if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay))
    	return $.Deferred().reject();
	QuickPatrolLastClick = Date.now();
	
	if (id === null || id === "" || typeof id == "undefined")
	{
		return $.Deferred().reject();
	}
	
	var api = new mw.Api(),
		deferred = $.Deferred();
	
	api.post(
	{
		action: 'fmod-patrol',
		format: 'json',
		pageid: id,
		token: mw.user.tokens.get("patrolToken")
	}).done(function(data)
	{
		deferred.resolve(data);
	}).fail(function(error)
	{
		if (!tokenAlreadyReset)
		{
			console.log("TOKEN PATROLOWANIA NIEPRAWIDŁOWY - DRUGA PRÓBA");
			api.post
			({
				format: "json",
				formatversion: "2",
				action: "query",
				meta: "tokens",
				type: "patrol"
			}).done(function(data)
			{
				mw.user.tokens.set("patrolToken", data.query.tokens.patroltoken);
				patrolPage(id, true)
				.done(function(data)
				{
					deferred.resolve(data);
					console.log("NOWY TOKEN PRAWIDŁOWY");
				}).fail(function()
				{
					deferred.reject();
					console.log("NOWY TOKEN NIEPRAWIDŁOWY");
				});
			});
		}
		else // w przypadku pierwszego faila to zostanie wykonane przez funkcję wywołaną rekursywnie
		{
			console.log(error);
			
			deferred.reject();
		}
	});
	
	return deferred.promise();
}

//usage: patrolModule.resetLastClick()
//before every call to any of above functions
patrolModule.resetLastClick = function()
{
	QuickPatrolLastClick = 0;
}