Użytkownik:Expert3222/patrolModule.js: Różnice pomiędzy wersjami
Z Nondanych
m |
(ładniej) |
||
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 = {}; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{ |
{ |
||
console.log("start"); |
|||
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
||
return $.when(); |
return $.when(); |
||
Linia 18: | Linia 19: | ||
deferred = $.Deferred(); |
deferred = $.Deferred(); |
||
console.log("dupa"); |
|||
api.post( |
api.post( |
||
{ |
{ |
||
Linia 66: | Linia 66: | ||
} |
} |
||
// |
//usage: patrolModule.patrolRangeOfChanges(to, from).done(callback).fail(callback2); |
||
patrolModule.patrolRangeOfChanges = function(to, from, tokenAlreadyReset) |
|||
{ |
{ |
||
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
||
Linia 73: | Linia 73: | ||
QuickPatrolLastClick = Date.now(); |
QuickPatrolLastClick = Date.now(); |
||
console.log(to); |
|||
if (to === null || to === "" || typeof to == "undefined") |
if (to === null || to === "" || typeof to == "undefined") |
||
{ |
{ |
||
Linia 136: | Linia 135: | ||
} |
} |
||
// |
//usage: patrolModule.patrolPage(pageid).done(callback).fail(callback2); |
||
patrolModule.patrolPage = function(id, tokenAlreadyReset) |
|||
{ |
{ |
||
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
if (QuickPatrolLastClick >= (Date.now() - QuickPatrolDelay)) |
||
Linia 198: | Linia 197: | ||
} |
} |
||
//usage: patrolModule.resetLastClick() |
|||
//before every call to any of above functions |
|||
patrolModule.resetLastClick = function() |
|||
{ |
{ |
||
QuickPatrolLastClick = 0; |
QuickPatrolLastClick = 0; |
Wersja z 19:05, 15 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 $.when();
QuickPatrolLastClick = Date.now();
if (diff === null || diff === "" || typeof diff == "undefined")
{
return $.when(); //resolved promise
}
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();
}).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);
patrolChange(diff, true)
.done(function()
{
deferred.resolve();
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 $.when();
QuickPatrolLastClick = Date.now();
if (to === null || to === "" || typeof to == "undefined")
{
return $.when(); //resolved promise
}
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();
}).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()
{
deferred.resolve();
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 $.when();
QuickPatrolLastClick = Date.now();
if (id === null || id === "" || typeof id == "undefined")
{
return $.when(); //resolved promise
}
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();
}).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()
{
deferred.resolve();
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;
}