Jump to content

User:Lupin/watchlistfilter.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*<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>
*/