Участник:Lingüista/hedgehog.js
Замечание: Возможно, после публикации вам придётся очистить кэш своего браузера, чтобы увидеть изменения.
- Firefox / Safari: Удерживая клавишу Shift, нажмите на панели инструментов Обновить либо нажмите Ctrl+F5 или Ctrl+R (⌘+R на Mac)
- Google Chrome: Нажмите Ctrl+Shift+R (⌘+Shift+R на Mac)
- Internet Explorer / Edge: Удерживая Ctrl, нажмите Обновить либо нажмите Ctrl+F5
- Opera: Нажмите Ctrl+F5.
var Hedgehog = {
interval : 10,
protectBy : [2, 3, 4, 9, 13, 14, 16, 17, 20, 22, 24, 27],
controlCount : {20 : 1},
defaultCount : 3
};
Hedgehog.specialFilters = Object.keys(Hedgehog.controlCount);
Hedgehog.scan = function () {
this.int = setInterval ( function () {
$.get('/wiki/Special:AbuseLog', function (data) {
Hedgehog.readAL(data);
});
}, this.interval * 1000);
};
Hedgehog.stop = function () {
this.int = clearInterval(this.int);
return true;
}
Hedgehog.readAL = function (html) {
this.page = document.createElement('html');
this.page.innerHTML = html;
var alist = $(this.page).find('.plainlinks li');
this.abuses = {};
for ( i = 0; i < alist.length; i ++ ) {
var name = $(alist[i]).find('a')[4].innerHTML;
var filter = $(alist[i]).find('a')[3].innerHTML;
if ( ! filter.match(/\d/) ) {
filter = $(alist[i]).find('a')[4].innerHTML;
name = $(alist[i]).find('a')[5].innerHTML;
}
if ( typeof(this.abuses[name]) == 'undefined' ) {
this.abuses[name] = {filters : [], sfilters : [], count : 0};
}
this.abuses[name].filters.push ( Number(filter.match(/\d+$/)[0]) );
this.abuses[name].sfilters.push (filter.match(/\d+$/)[0]);
this.abuses[name].count ++;
}
Hedgehog.protect(this.abuses);
};
Hedgehog.containsAnyOf = function (f_arr, s_arr) {
var contains = [];
for (var i = 0; i < s_arr.length; i++) {
if (f_arr.indexOf(s_arr[i]) > -1) {
contains.push(s_arr[i]);
break;
}
}
if (contains.length < 1) {
contains = null;
}
return contains;
};
Hedgehog.protect = function (abuses) {
for ( var page in abuses ) {
var prot = false;
if ( this.containsAnyOf(abuses[page].filters, this.protectBy) ) {
var cao = this.containsAnyOf(abuses[page].sfilters, this.specialFilters);
if ( abuses[page].count >= this.defaultCount && ! cao ) {
prot = true;
}
else if ( cao ) {
for ( i = 0; i < cao.length; i ++ ) {
if ( this.controlCount[Number(cao[i])] >= abuses[page].count ) {
prot = true;
}
}
}
}
if ( prot === true) {
$.get('/wiki/' + page + '?action=edit', function (data) {
htmle = document.createElement('html');
htmle.innerHTML = data;
if ( $(htmle).find('.mw-warning-with-logexcerpt').length < 1 ) {
pgn = $(htmle).find('#ca-nstab-main span a').attr('href').replace(/^\/wiki\//g, '');
$.get('/w/api.php', {action: 'query', prop: 'info', intoken: 'protect', titles: pgn, format: 'json'}, function (data) {
token = '';
for ( page in data["query"]["pages"] ) {
token = data["query"]["pages"][page]["protecttoken"];
pgn = data["query"]["pages"][page]["title"]
}
$.post('/w/api.php', {action: 'protect', title: pgn, protections: 'edit=autoconfirmed|move=autoconfirmed',
expiry: '1 day', token: token
}, function (data) {
console.log('Protected!');
}
);
});
}
});
}
}
};