Użytkownik:Expert3222/patrolModule.js: Różnice pomiędzy wersjami
Z Nondanych
(ładniej) |
|||
Linia 8: | Linia 8: | ||
{ |
{ |
||
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
||
return $. |
return $.Deferred().reject(); //rejected promise |
||
QuickPatrolLastClick = Date.now(); |
QuickPatrolLastClick = Date.now(); |
||
if (diff === null || diff === "" || typeof diff == "undefined") |
if (diff === null || diff === "" || typeof diff == "undefined") |
||
{ |
{ |
||
return $. |
return $.Deferred().reject(); |
||
} |
} |
||
Linia 27: | Linia 27: | ||
}).done(function(data) |
}).done(function(data) |
||
{ |
{ |
||
deferred.resolve(); |
deferred.resolve(data); |
||
}).fail(function(error) |
}).fail(function(error) |
||
{ |
{ |
||
Linia 40: | Linia 40: | ||
meta: "tokens", |
meta: "tokens", |
||
type: "patrol" |
type: "patrol" |
||
}).done(function( |
}).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 70: | Linia 70: | ||
{ |
{ |
||
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
||
return $. |
return $.Deferred().reject(); |
||
QuickPatrolLastClick = Date.now(); |
QuickPatrolLastClick = Date.now(); |
||
if (to === null || to === "" || typeof to == "undefined") |
if (to === null || to === "" || typeof to == "undefined") |
||
{ |
{ |
||
return $. |
return $.Deferred().reject(); |
||
} |
} |
||
Linia 96: | 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 113: | 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 139: | Linia 139: | ||
{ |
{ |
||
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
||
return $. |
return $.Deferred().reject(); |
||
QuickPatrolLastClick = Date.now(); |
QuickPatrolLastClick = Date.now(); |
||
if (id === null || id === "" || typeof id == "undefined") |
if (id === null || id === "" || typeof id == "undefined") |
||
{ |
{ |
||
return $. |
return $.Deferred().reject(); |
||
} |
} |
||
Linia 158: | Linia 158: | ||
}).done(function(data) |
}).done(function(data) |
||
{ |
{ |
||
deferred.resolve(); |
deferred.resolve(data); |
||
}).fail(function(error) |
}).fail(function(error) |
||
{ |
{ |
||
Linia 175: | 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() |
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;
}