Użytkownik:Expert3222/patrolModule.js: Różnice pomiędzy wersjami
Z Nondanych
m |
|||
(Nie pokazano 8 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 = {}; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{ |
{ |
||
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 $. |
return $.Deferred().reject(); |
||
} |
} |
||
Linia 25: | Linia 27: | ||
}).done(function(data) |
}).done(function(data) |
||
{ |
{ |
||
deferred.resolve(); |
deferred.resolve(data); |
||
}).fail(function(error) |
}).fail(function(error) |
||
{ |
{ |
||
Linia 38: | 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 64: | 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)) |
||
return $. |
return $.Deferred().reject(); |
||
QuickPatrolLastClick = Date.now(); |
QuickPatrolLastClick = Date.now(); |
||
if ( |
if (to === null || to === "" || typeof to == "undefined") |
||
{ |
{ |
||
return $. |
return $.Deferred().reject(); |
||
} |
} |
||
Linia 94: | 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 111: | 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 133: | 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)) |
||
return $. |
return $.Deferred().reject(); |
||
QuickPatrolLastClick = Date.now(); |
QuickPatrolLastClick = Date.now(); |
||
if ( |
if (id === null || id === "" || typeof id == "undefined") |
||
{ |
{ |
||
return $. |
return $.Deferred().reject(); |
||
} |
} |
||
Linia 156: | Linia 158: | ||
}).done(function(data) |
}).done(function(data) |
||
{ |
{ |
||
deferred.resolve(); |
deferred.resolve(data); |
||
}).fail(function(error) |
}).fail(function(error) |
||
{ |
{ |
||
Linia 173: | 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 193: | Linia 195: | ||
return deferred.promise(); |
return deferred.promise(); |
||
} |
|||
//usage: patrolModule.resetLastClick() |
|||
//before every call to any of above functions |
|||
patrolModule.resetLastClick = function() |
|||
{ |
|||
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;
}