לדלג לתוכן

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

מתוך צפונות ויקי
מ Bot: Security fix: protocol-relative URLs per https://meta.wikimedia.org/?oldid=3197161 (questions?).
מ 38 גרסאות של הדף wikipedia:he:מדיה_ויקי:Gadget-patrolAlarm.js יובאו
 
(9 גרסאות ביניים של 2 משתמשים אינן מוצגות)
שורה 1: שורה 1:
// Patrollers alarm clock
// Patrollers alarm clock
$(document).ready(function(){
function pac_sendNonPatrolledEditsQuery(patrols) {
var aj = sajax_init_object();
aj.patrols = patrols;
aj.onreadystatechange = pac_countNonPatrolledEdits;
aj.open('GET', '//he.wikipedia.org/w/api.php?action=query&list=recentchanges&format=xml&rcshow=!patrolled&rclimit=100');
aj.send(null);
}


function pac_countNonPatrolledEdits() {
(function() {
if (this.readyState != 4) // this is some ajax incantation - ony "4" is good.
return;
xml = this.responseXML;
var responses = xml.getElementsByTagName('rc');
var edits = [];
for (var i = 0; i < responses.length; i++) {
var response = responses[i];
edits.push({
ts:response.getAttribute('timestamp'),
cur:response.getAttribute('revid')
});
}
pac_analyzeResults(this.patrols, edits);
}


function pac_countPatrolledEdits() {
var cookiename = 'patrollers_alarmclock',
if (this.readyState != 4) // this is some ajax incantation - ony "4" is good.
    distressLimit = window.wgPatrolDistressLimit || 75,
return;
frequency = window.wgPatrolDistressRestSeconds || 120,
xml = this.responseXML;
keys = {distress: 'inDistress', lastTest: 'lastTestedPatrols', lastVisitRC: 'lastVisitRC'};
var responses = xml.getElementsByTagName('item');
var patrols = [];
function tsToDate(rc) {
for (var i = 0; i < responses.length; i++) {
// timestamp looks like so: "2011-05-05T18:56:27Z"
var response = responses[i];
var dar = rc.timestamp.split(/[^\d]/)  
if (response.getAttribute('auto') != 0)
.map(function(s) {
continue;
return parseInt(s, 10);
patrols.push({
ts:response.getAttribute('timestamp'),
patroller:response.getAttribute('user'),
cur:response.getAttribute('cur')
});
});
}
var month = dar[1] - 1; // "Date" expexts months in the range of 0..11, timestamp is more conventional.
pac_sendNonPatrolledEditsQuery(patrols);
return new Date(dar[0],month,dar[2],dar[3],dar[4],dar[5]);
}
}


function pac_sendPatrolledEditsQuery() {
function sendNonPatrolledEditsQuery() {
var aj = sajax_init_object();
var params = {action: 'query', list: 'recentchanges', rcshow: '!patrolled', rclimit: 100, rcnamespace: 0, format: 'json'}
aj.onreadystatechange = pac_countPatrolledEdits;
$.getJSON(mw.util.wikiScript('api'), params, function(data) {
aj.open('GET', '//he.wikipedia.org/w/api.php?action=query&format=xml&list=logevents&leaction=patrol/patrol&lelimit=200');
cookieVal(keys.lastTest, new Date().valueOf());
aj.send(null);
if (data && data.query && data.query.recentchanges) {
var ar = data.query.recentchanges;
var interval = tsToDate(ar[0]) - tsToDate(ar[99]);
if (interval < distressLimit * 60 * 1000)
distress(true);
}
});
}
}


function pac_simpleSerialize(obj) {
function cookieVal(key, val) {
var res = [];
var ar = ($.cookie(cookiename) || '').split("\n"),
for (var key in obj)
cookieContent = {};
res.push(key + "\t" + obj[key]);
return res.join("\n");
}
 
function pac_simpleDeserialize(str) {
ar = str.split("\n");
var res = {};
for (var i in ar) {
for (var i in ar) {
var keyval = ar[i].split("\t");
var keyval = ar[i].split("\t");
if (keyval.length == 2)
if (keyval.length == 2)
res[keyval[0]] = keyval[1];
cookieContent[keyval[0]] = keyval[1];
}
if (typeof val == "undefined") // getting
return cookieContent[key]; // might be undefined
else { // setting
cookieContent[key] = val;
var res = [];
for (var key in cookieContent)
res.push(key + "\t" + cookieContent[key]);
$.cookie(cookiename, res.join("\n"), {path: '/', expires: 1});
}
}
return res;
}
function pac_cookieContent() {
var str = $.cookie('patrollers_alarmclock', { path: '/' }) || '';
return pac_simpleDeserialize(str);
}
function pac_setCookieVal(key, val) {
var attr = pac_cookieContent();
attr[key] = val;
$.cookie('patrollers_alarmclock', pac_simpleSerialize(attr), { path: '/', expires: 1 });
}
}


function pac_getCookieVal(key) {
function clearCookie() {$.cookie(cookiename, null, {path: '/'});}
var attr = pac_cookieContent();
return attr[key];
}


function pac_clearCookie() {
function distressMessage() {
$.cookie('patrollers_alarmclock', null, {path: '/'});
var li = $('#n-recentchanges'),
}
inDistress = distress();
 
if (li.attr('distress') == inDistress)
function pac_distressMessage(distress) {
var li = document.getElementById("n-recentchanges");
if (!li)
return;
return;
if (distress) {
li.attr('distress', inDistress)
$.extend(li.style, {backgroundColor:'yellow',cursor:'pointer'});
if (inDistress) {
li.title = "מצוקת ניטור חמורה.\n.כל המצילים מתבקשים לחוף גורדון";
li
li.onclick = function(e){if ((e||window.event).button == 0) window.location='//he.wikipedia.org/w/index.php?title=מיוחד:שינויים_אחרונים&hidepatrolled=1';}
.css({backgroundColor:'#F9C557', cursor:'pointer'})
} else {
.attr({title: "מצוקת ניטור חמורה.\n.כל המצילים מתבקשים לחוף גורדון"})
li.style.background = li.style.cursor = li.title = '';
.click(function(e){
delete li.onclick;
if (e.button == 0)
}
window.location = mw.util.wikiScript() + '?title=מיוחד:שינויים_אחרונים&hidepatrolled=1&namespace=0';
}
});
 
function pac_analyzeResults(patrols, edits) {
pac_setCookieVal('lastTestedPatrols', new Date().valueOf());
if (edits && edits.length > 1) {
var interval = pac_stringToDate(edits[0].ts) - pac_stringToDate(edits[edits.length - 1].ts);
var limit = typeof wgPatrolDistressLimit == "undefined" ? 150 : wgPatrolDistressLimit;
pac_setDistress(interval < limit * 60 * 1000);
}
}
else
li
.css({background: '', cursor: ''})
.attr({title: ''})
.undelegate('click');
}
}


function pac_setDistress(distress) {
function distress(val) {
pac_setCookieVal("inDistress", distress ? "1" : "0");
if (typeof val === "boolean") {
pac_distressMessage(distress);
cookieVal(keys.distress, val.toString());
}
distressMessage();
 
} else
function pac_getDistress() {
return cookieVal(keys.distress) === "true"
return pac_getCookieVal("inDistress") == "1";
}
}


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


function pac_patrollersWakeUpPeriodic() {
function 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 * 120 : wgPatrolDistressRestSeconds;
try {
try {
var ts = parseInt(pac_getCookieVal('lastVisitRC')  || "0");
var ts = parseInt(cookieVal(keys.lastVisitRC)  || "0");
if (ts && ((new Date() - new Date(ts)) < numSecs * 1000)) {
if (ts && ((new Date() - new Date(ts)) < frequency * 1000)) {
pac_setDistress(false);
distress(false);
return;
return;
}
}
} catch(e) {
} catch(e) {
pac_clearCookie();
clearCookie();
}
}
 
if (pac_getDistress()) {
if (distress()) {
pac_distressMessage(true);
distressMessage(true);
return;
return;
}
}
 
try {
try {
ts = parseInt(pac_getCookieVal('lastTestedPatrols')  || "0");
ts = parseInt(cookieVal(keys.lastTest)  || "0");
if (ts && ((new Date() - new Date(ts)) < numSecs * 1000))
if (ts && ((new Date() - new Date(ts)) < frequency * 1000))
return;
return;
} catch(e) {
} catch(e) {
pac_clearCookie();
clearCookie();
}
}


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


if ( mw.config.get( 'wgCanonicalSpecialPageName' ) == 'Recentchanges') {  // mark cookie with last visit and exit.
cookieVal(keys.lastVisitRC, new Date().valueOf());
distress(false);
}
else
setInterval(patrollersWakeUpPeriodic, 1000); // it's ok to call every second - the function will quit if needed.


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

גרסה אחרונה מ־13:05, 5 בדצמבר 2022

// Patrollers alarm clock

(function() {

var cookiename = 'patrollers_alarmclock',
    distressLimit = window.wgPatrolDistressLimit || 75,
 	frequency = window.wgPatrolDistressRestSeconds || 120,
	keys = {distress: 'inDistress', lastTest: 'lastTestedPatrols', lastVisitRC: 'lastVisitRC'};
 
function tsToDate(rc) {
// timestamp looks like so: "2011-05-05T18:56:27Z"
	var dar = rc.timestamp.split(/[^\d]/) 
		.map(function(s) { 
			return parseInt(s, 10);
		});
	var month = dar[1] - 1; // "Date" expexts months in the range of 0..11, timestamp is more conventional.
	return new Date(dar[0],month,dar[2],dar[3],dar[4],dar[5]);
}

function sendNonPatrolledEditsQuery() {
	var params = {action: 'query', list: 'recentchanges', rcshow: '!patrolled', rclimit: 100, rcnamespace: 0, format: 'json'}
	$.getJSON(mw.util.wikiScript('api'), params, function(data) {
		cookieVal(keys.lastTest, new Date().valueOf());
		if (data && data.query && data.query.recentchanges) {
			var ar = data.query.recentchanges;
			var interval = tsToDate(ar[0]) - tsToDate(ar[99]);
			if (interval < distressLimit * 60 * 1000)
				distress(true);
		}
	});
}

function cookieVal(key, val) {
	var ar = ($.cookie(cookiename) || '').split("\n"),
		cookieContent = {};
	for (var i in ar) {
		var keyval = ar[i].split("\t");
		if (keyval.length == 2)
			cookieContent[keyval[0]] = keyval[1];
	}
	
	if (typeof val == "undefined") // getting
		return cookieContent[key]; // might be undefined
	else { // setting
		cookieContent[key] = val;
		var res = [];
		for (var key in cookieContent)
			res.push(key + "\t" + cookieContent[key]);
		$.cookie(cookiename, res.join("\n"), {path: '/', expires: 1});
	}
}

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

function distressMessage() {
	var li = $('#n-recentchanges'),
		inDistress = distress();
	if (li.attr('distress') == inDistress)
		return;
	li.attr('distress', inDistress)
	if (inDistress) {
		li
		.css({backgroundColor:'#F9C557', cursor:'pointer'})
		.attr({title: "מצוקת ניטור חמורה.\n.כל המצילים מתבקשים לחוף גורדון"})
		.click(function(e){
			if (e.button == 0)
				window.location = mw.util.wikiScript() + '?title=מיוחד:שינויים_אחרונים&hidepatrolled=1&namespace=0';
		});
	}
	else
		li
		.css({background: '', cursor: ''})
		.attr({title: ''})
		.undelegate('click');
}

function distress(val) {
	if (typeof val === "boolean") {
		cookieVal(keys.distress, val.toString());
		distressMessage();
	} else 
		return cookieVal(keys.distress) === "true"
}


function patrollersWakeUpPeriodic() {
	//if we've been in recent changes in last 5 minutes, or we tested patrolls in last 5 minutes, do nothing.
	try {
		var ts = parseInt(cookieVal(keys.lastVisitRC)  || "0");
		if (ts && ((new Date() - new Date(ts)) < frequency * 1000)) {
			distress(false);
			return;
		}
	} catch(e) {
		clearCookie();
	}

	if (distress()) {
		distressMessage(true);
		return;
	}

	try {
		ts = parseInt(cookieVal(keys.lastTest)  || "0");
		if (ts && ((new Date() - new Date(ts)) < frequency * 1000))
			return;
	} catch(e) {
		clearCookie();
	}

	sendNonPatrolledEditsQuery();
}

if ( mw.config.get( 'wgCanonicalSpecialPageName' ) == 'Recentchanges') {  // mark cookie with last visit and exit.
	cookieVal(keys.lastVisitRC, new Date().valueOf());
	distress(false);
}
else
	setInterval(patrollersWakeUpPeriodic, 1000); // it's ok to call every second - the function will quit if needed.

})();