מדיה ויקי: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 | // 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 = | 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 | 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: | ||
}); | }); | ||
} | } | ||
pac_analyzeResults(this.patrols, edits); | |||
} | } | ||
function | 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: | ||
}); | }); | ||
} | } | ||
pac_sendNonPatrolledEditsQuery(patrols); | |||
} | } | ||
function | function pac_sendPatrolledEditsQuery() { | ||
var aj = sajax_init_object(); | var aj = sajax_init_object(); | ||
aj.onreadystatechange = | 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 | function pac_simpleSerialize(obj) { | ||
var res = []; | var res = []; | ||
for (var key in obj) | for (var key in obj) | ||
| שורה 56: | שורה 57: | ||
} | } | ||
function | function pac_simpleDeserialize(str) { | ||
ar = str.split("\n"); | ar = str.split("\n"); | ||
var res = {}; | var res = {}; | ||
| שורה 67: | שורה 68: | ||
} | } | ||
function | function pac_cookieContent() { | ||
var str = $.cookie(' | var str = $.cookie('patrollers_alarmclock', { path: '/' }) || ''; | ||
return | return pac_simpleDeserialize(str); | ||
} | } | ||
function | function pac_setCookieVal(key, val) { | ||
var attr = | var attr = pac_cookieContent(); | ||
attr[key] = val; | attr[key] = val; | ||
$.cookie(' | $.cookie('patrollers_alarmclock', pac_simpleSerialize(attr), { path: '/', expires: 1 }); | ||
} | } | ||
function | function pac_getCookieVal(key) { | ||
var attr = | var attr = pac_cookieContent(); | ||
return attr[key]; | return attr[key]; | ||
} | } | ||
function | function pac_clearCookie() { | ||
$.cookie(' | $.cookie('patrollers_alarmclock', null, {path: '/'}); | ||
} | } | ||
function | function pac_distressMessage(distress) { | ||
var li = document.getElementById("n-recentchanges"); | var li = document.getElementById("n-recentchanges"); | ||
if (!li) | if (!li) | ||
| שורה 101: | שורה 102: | ||
} | } | ||
function | function pac_analyzeResults(patrols, edits) { | ||
pac_setCookieVal('lastTestedPatrols', new Date().valueOf()); | |||
if (edits && edits.length > 1) { | if (edits && edits.length > 1) { | ||
var interval = | 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; | ||
pac_setDistress(interval < limit * 60 * 1000); | |||
} | } | ||
} | } | ||
function | function pac_setDistress(distress) { | ||
pac_setCookieVal("inDistress", distress ? "1" : "0"); | |||
pac_distressMessage(distress); | |||
} | } | ||
function | function pac_getDistress() { | ||
return | return pac_getCookieVal("inDistress") == "1"; | ||
} | } | ||
function | function pac_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 | 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 * | var numSecs = typeof wgPatrolDistressRestSeconds == "undefined" ? 1 * 120 : wgPatrolDistressRestSeconds; | ||
try { | try { | ||
var ts = parseInt( | 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)) { | ||
pac_setDistress(false); | |||
return; | return; | ||
} | } | ||
} catch(e) { | } catch(e) { | ||
pac_clearCookie(); | |||
} | } | ||
if ( | if (pac_getDistress()) { | ||
pac_distressMessage(true); | |||
return; | return; | ||
} | } | ||
try { | try { | ||
ts = parseInt( | 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) { | ||
pac_clearCookie(); | |||
} | } | ||
pac_sendNonPatrolledEditsQuery([]); | |||
} | } | ||
function | function pac_patrollersWakeUp() { | ||
if (wgCanonicalSpecialPageName == 'Recentchanges') { // mark cookie with last visit and exit. | if (wgCanonicalSpecialPageName == 'Recentchanges') { // mark cookie with last visit and exit. | ||
pac_setCookieVal('lastVisitRC', new Date().valueOf()); | |||
pac_setDistress(false); | |||
} | } | ||
else | else | ||
setInterval( | setInterval(pac_patrollersWakeUpPeriodic, 1000); // it's ok to call every second - the function will quit if needed. | ||
} | } | ||
hookEvent("load", | hookEvent("load", pac_patrollersWakeUp); | ||