מדיה ויקי:Gadget-patrolAlarm.js: הבדלים בין גרסאות בדף

resurrect the ability to turn message off. this is useful if you have multiple tabs - we want to turn it off in all tabs
שמות פונקציות יותר ייחודיים
שורה 1: שורה 1:
function sendNonPatrolledEditsQuery(patrols) {
// Patrollers alarm clock
function pac_sendNonPatrolledEditsQuery(patrols) {
var aj = sajax_init_object();
var aj = sajax_init_object();
aj.patrols = patrols;
aj.patrols = patrols;
aj.onreadystatechange = countNonPatrolledEdits;
aj.onreadystatechange = pac_countNonPatrolledEdits;
aj.open('GET', 'http://he.wikipedia.org/w/api.php?action=query&list=recentchanges&format=xml&rcshow=!patrolled&rclimit=100');
aj.open('GET', 'http://he.wikipedia.org/w/api.php?action=query&list=recentchanges&format=xml&rcshow=!patrolled&rclimit=100');
aj.send(null);
aj.send(null);
}
}


function countNonPatrolledEdits() {
function pac_countNonPatrolledEdits() {
if (this.readyState != 4) // this is some ajax incantation - ony "4" is good.
if (this.readyState != 4) // this is some ajax incantation - ony "4" is good.
return;
return;
שורה 20: שורה 21:
});
});
}
}
analyzeResults(this.patrols, edits);
pac_analyzeResults(this.patrols, edits);
}
}


function countPatrolledEdits() {
function pac_countPatrolledEdits() {
if (this.readyState != 4) // this is some ajax incantation - ony "4" is good.
if (this.readyState != 4) // this is some ajax incantation - ony "4" is good.
return;
return;
שורה 39: שורה 40:
});
});
}
}
sendNonPatrolledEditsQuery(patrols);
pac_sendNonPatrolledEditsQuery(patrols);
}
}


function sendPatrolledEditsQuery() {
function pac_sendPatrolledEditsQuery() {
var aj = sajax_init_object();
var aj = sajax_init_object();
aj.onreadystatechange = countPatrolledEdits;
aj.onreadystatechange = pac_countPatrolledEdits;
aj.open('GET', 'http://he.wikipedia.org/w/api.php?action=query&format=xml&list=logevents&leaction=patrol/patrol&lelimit=200');
aj.open('GET', 'http://he.wikipedia.org/w/api.php?action=query&format=xml&list=logevents&leaction=patrol/patrol&lelimit=200');
aj.send(null);
aj.send(null);
}
}


function simpleSerialize(obj) {
function pac_simpleSerialize(obj) {
var res = [];
var res = [];
for (var key in obj)  
for (var key in obj)  
שורה 56: שורה 57:
}
}


function simpleDeserialize(str) {
function pac_simpleDeserialize(str) {
ar = str.split("\n");
ar = str.split("\n");
var res = {};
var res = {};
שורה 67: שורה 68:
}
}


function cookieContent() {
function pac_cookieContent() {
var str = $.cookie('patroller_alarmclock2', { path: '/' }) || '';
var str = $.cookie('patrollers_alarmclock', { path: '/' }) || '';
return simpleDeserialize(str);
return pac_simpleDeserialize(str);
}
}


function setCookieVal(key, val) {
function pac_setCookieVal(key, val) {
var attr = cookieContent();
var attr = pac_cookieContent();
attr[key] = val;
attr[key] = val;
$.cookie('patroller_alarmclock2', simpleSerialize(attr), { path: '/', expires: 1 });
$.cookie('patrollers_alarmclock', pac_simpleSerialize(attr), { path: '/', expires: 1 });
}
}


function getCookieVal(key) {
function pac_getCookieVal(key) {
var attr = cookieContent();
var attr = pac_cookieContent();
return attr[key];
return attr[key];
}
}


function clearCookie() {
function pac_clearCookie() {
$.cookie('patroller_alarmclock2', null, {path: '/'});
$.cookie('patrollers_alarmclock', null, {path: '/'});
}
}


function distressMessage(distress) {
function pac_distressMessage(distress) {
var li = document.getElementById("n-recentchanges");
var li = document.getElementById("n-recentchanges");
if (!li)
if (!li)
שורה 101: שורה 102:
}
}


function analyzeResults(patrols, edits) {
function pac_analyzeResults(patrols, edits) {
setCookieVal('lastTestedPatrols', new Date().valueOf());
pac_setCookieVal('lastTestedPatrols', new Date().valueOf());
if (edits && edits.length > 1) {
if (edits && edits.length > 1) {
var interval = stringToDate(edits[0].ts) - stringToDate(edits[edits.length - 1].ts);
var interval = pac_stringToDate(edits[0].ts) - pac_stringToDate(edits[edits.length - 1].ts);
var limit = typeof wgPatrolDistressLimit == "undefined" ? 150 : wgPatrolDistressLimit;
var limit = typeof wgPatrolDistressLimit == "undefined" ? 150 : wgPatrolDistressLimit;
setDistress(interval < limit * 60 * 1000);
pac_setDistress(interval < limit * 60 * 1000);
}
}
}
}


function setDistress(distress) {
function pac_setDistress(distress) {
setCookieVal("inDistress", distress ? "1" : "0");
pac_setCookieVal("inDistress", distress ? "1" : "0");
distressMessage(distress);
pac_distressMessage(distress);
}
}


function getDistress() {
function pac_getDistress() {
return getCookieVal("inDistress") == "1";
return pac_getCookieVal("inDistress") == "1";
}
}


function restPeriod() {
function pac_stringToDate(dstr) {
return 1000 * numSecs;
}
 
function stringToDate(dstr) {
dar = dstr.split(/[^\d]/)
dar = dstr.split(/[^\d]/)
return new Date(dar[0],dar[1],dar[2],dar[3],dar[4],dar[5]);
return new Date(dar[0],dar[1],dar[2],dar[3],dar[4],dar[5]);
}
}


function patrollersWakeUpPeriodic() {
function pac_patrollersWakeUpPeriodic() {
//if we've been in recent changes in last 5 minutes, or we tested patrolls in last 5 minutes, do nothing.
//if we've been in recent changes in last 5 minutes, or we tested patrolls in last 5 minutes, do nothing.
var numSecs = typeof wgPatrolDistressRestSeconds == "undefined" ? 1 * 60 : wgPatrolDistressRestSeconds;
var numSecs = typeof wgPatrolDistressRestSeconds == "undefined" ? 1 * 120 : wgPatrolDistressRestSeconds;
try {
try {
var ts = parseInt(getCookieVal('lastVisitRC')  || "0");
var ts = parseInt(pac_getCookieVal('lastVisitRC')  || "0");
if (ts && ((new Date() - new Date(ts)) < numSecs * 1000)) {
if (ts && ((new Date() - new Date(ts)) < numSecs * 1000)) {
setDistress(false);
pac_setDistress(false);
return;
return;
}
}
} catch(e) {
} catch(e) {
clearCookie();
pac_clearCookie();
}
}
if (getDistress()) {
if (pac_getDistress()) {
distressMessage(true);
pac_distressMessage(true);
return;
return;
}
}
try {
try {
ts = parseInt(getCookieVal('lastTestedPatrols')  || "0");
ts = parseInt(pac_getCookieVal('lastTestedPatrols')  || "0");
if (ts && ((new Date() - new Date(ts)) < numSecs * 1000))
if (ts && ((new Date() - new Date(ts)) < numSecs * 1000))
return;
return;
} catch(e) {
} catch(e) {
clearCookie();
pac_clearCookie();
}
}


sendNonPatrolledEditsQuery([]);
pac_sendNonPatrolledEditsQuery([]);
}
}


function patrollersWakeUp() {
function pac_patrollersWakeUp() {
if (wgCanonicalSpecialPageName == 'Recentchanges') {  // mark cookie with last visit and exit.
if (wgCanonicalSpecialPageName == 'Recentchanges') {  // mark cookie with last visit and exit.
setCookieVal('lastVisitRC', new Date().valueOf());
pac_setCookieVal('lastVisitRC', new Date().valueOf());
setDistress(false);
pac_setDistress(false);
}
}
else  
else  
setInterval(patrollersWakeUpPeriodic, 1000); // it's ok to call every second - the function will quit if needed.
setInterval(pac_patrollersWakeUpPeriodic, 1000); // it's ok to call every second - the function will quit if needed.
}
}


hookEvent("load", patrollersWakeUp);
hookEvent("load", pac_patrollersWakeUp);