User:Lupin/watchlistfilter.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Lupin/watchlistfilter. |
/*<nowiki>
== Installation instructions ==
Add {{subst:js|User:Lupin/watchlistfilter.js}} to your monobook.js.
*/
var wlfilter={};
wlfilter.readList=function() {
var bc=document.getElementById('bodyContent') || document;
var uls=bc.getElementsByTagName('ul');
var ulCount=0;
var itemCount=0;
var list=[];
for (var i=0; i<uls.length; ++i) {
try { var j=wlfilter.readListUl(uls[i]); list=list.concat(j); }
catch (someError) { continue; }
++ulCount;
itemCount += j.length;
}
wlfilter.list=list;
return [ulCount,itemCount, list];
};
wlfilter.readListUl=function(ul) {
var ret=[];
var lis=ul.getElementsByTagName('li');
for (var i=0; i<lis.length; ++i) {
ret.push( wlfilter.readListLi(lis[i]) );
}
return ret;
};
wlfilter.readListLi=function(li) {
var lks=li.getElementsByTagName('a');
var art=lks[2].title || lks[2].originalTitle;
var ed=lks[3].title || lks[3].originalTitle;
var redEd=false;
if (lks[3].className=='new' || lks[4].className=='new') { redEd=true; }
if (ed == 'Special:Contributions') { ed = lks[3].innerHTML; }
var mine=(ed=='User:'+mw.config.get('wgUserName'));
var talk=/^Talk:|^[^:]+ talk:/.test(art);
var ip= RegExp('((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}' +
'(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])').test(ed);
var sp=li.getElementsByTagName('span');
var minor=false;
var comment='';
for (var i=0; i<sp.length; ++i) {
switch(sp[i].className){
case 'minor': minor=true; break;
case 'comment': comment=sp[i].innerHTML; break;
}
}
var rv=RegExp('^[(](Reverted edits by | *[Rr][Vv] ?[Vv]|Revert to |BOT - rv)').test(comment);
return { li: li, article: art, editor: ed, ip: ip, talk: talk, visibility: 1,
mine: mine,minor: minor, comment: comment, revert: rv, redlinks: redEd };
};
wlfilter.newCheckbox=function(label, state, onclick, internalName, labelRight) {
if (!wlfilter.controls) {
wlfilter.makeControlsDiv();
if (!wlfilter.controls) { return null; }
}
// checkbox
var box=document.createElement('input');
box.type='checkbox';
box.checked = state;
box.onclick = onclick;
if (internalName) { wlfilter.controls[internalName]=box; }
// label
var l=document.createElement('label');
l.innerHTML=label;
if (labelRight) {
wlfilter.controls.appendChild(box);
wlfilter.controls.appendChild(l);
} else {
wlfilter.controls.appendChild(l);
wlfilter.controls.appendChild(box);
}
l.onclick=function(){box.click()};
return box;
};
wlfilter.makeControlsDiv=function() {
var anchs=document.getElementsByTagName('A');
var found=wlfilter.findH4();
if (!found) { return null; }
var div=document.createElement('div');
div.id='wlfilter_controls';
div.style.cssFloat='right';
div.style.background='#EEF';
div.style.border='2px solid #AAF';
div.style.opacity=0.7;
div.style.textAlign='right';
div.style.padding='4px';
found.parentNode.insertBefore(div,found);
wlfilter.controls=div;
};
wlfilter.findH4=function() {
var h4s=document.getElementsByTagName('h4');
if (h4s.length) { return h4s[0].previousSibling; }
return null;
};
wlfilter.metafilter=function( filter , state ) {
if (!wlfilter.list) { wlfilter.readList(); }
var l=wlfilter.list;
for (var i=0; i<l.length; ++i) {
if ( filter( l[i] ) ) {
wlfilter.show( l[i], state );
}
}
};
wlfilter.filterMine=function( o ) { return o.mine; };
wlfilter.filterTalk=function( o ) { return o.talk; };
wlfilter.filterIp=function( o ) { return o.ip };
wlfilter.filterNonIp=function( o ) { return !o.ip };
wlfilter.filterMinor=function( o ) { return o.minor };
wlfilter.filterRv=function( o ) { return o.revert };
wlfilter.filterNoRedlinks=function( o ) { return !(o.ip || o.redlinks) };
wlfilter.show=function( o, show ) {
if (show) {
if (o.visibility === 0) { o.li.style.display=''; }
++o.visibility;
} else {
if (o.visibility == 1) { o.li.style.display='none'; }
--o.visibility;
}
};
wlfilter.makeui = function () {
wlfilter.makeControlsDiv();
if (!wlfilter.controls) { return; }
wlfilter.controls.newline = function(){
wlfilter.controls.appendChild(document.createElement('br'));
};
wlfilter.newCheckbox(
'My edits', true,
function() { wlfilter.metafilter( wlfilter.filterMine, this.checked ); },
'filtermine');
wlfilter.controls.newline();
wlfilter.newCheckbox(
'Talk pages', true,
function() { wlfilter.metafilter( wlfilter.filterTalk, this.checked ); },
'filtertalk');
wlfilter.controls.newline();
wlfilter.newCheckbox(
'IP edits', true,
function() { wlfilter.metafilter( wlfilter.filterIp, this.checked ); },
'filterip');
wlfilter.controls.newline();
wlfilter.newCheckbox(
'Non-IP edits', true,
function() { wlfilter.metafilter( wlfilter.filterNonIp, this.checked ); },
'filternonip');
wlfilter.controls.newline();
wlfilter.newCheckbox(
'Minor edits', true,
function() { wlfilter.metafilter( wlfilter.filterMinor, this.checked ); },
'filterminor');
wlfilter.controls.newline();
wlfilter.newCheckbox(
'Reversions', true,
function() { wlfilter.metafilter( wlfilter.filterRv, this.checked ); },
'filterrv');
wlfilter.controls.newline();
wlfilter.newCheckbox(
'"Active" editors', true,
function() { wlfilter.metafilter( wlfilter.filterNoRedlinks, this.checked ); },
'filterred');
};
if (/Special:(Watchlist|Recentchangeslinked)/.test(mw.config.get('wgPageName'))) {
addOnloadHook(wlfilter.makeui);
}
/*
</nowiki>
*/