Jump to content

User:Splarka/wbfilter.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
/* Watchlist line filter script, version [0.0.2] 
Originally from http://en.wikipedia.org/wiki/User:Splarka/wbfilter.js

Triggers on watchlists, lets you filter by byte change.
*/

if(wgCanonicalSpecialPageName == 'Watchlist') addOnloadHook(watchlistByteFilter)
function watchlistByteFilter() {
  var obj = document.getElementById('contentSub') || document.getElementById('topbar')
  if(!obj) return;
  var wbf = document.createElement('div');
   wbf.setAttribute('id','wbffrm');
   wbf.style.color = 'black';
   var fs = document.createElement('fieldset');
    var lg = document.createElement('legend');
     lg.appendChild(document.createTextNode('Byte change threshold filter'));
    fs.appendChild(lg);
    var lab1 = document.createElement('label');
     lab1.appendChild(document.createTextNode('Byte threshold '));
     lab1.setAttribute('for','wbfinput1');
    fs.appendChild(lab1);
    var inp1 = document.createElement('input');
     inp1.setAttribute('id','wbfinput1');
     inp1.setAttribute('name','wbfinput1');
     inp1.setAttribute('type','text');
    fs.appendChild(inp1);
    var inp2 = document.createElement('input');
     inp2.setAttribute('id','wbfinput2');
     inp2.setAttribute('name','wbfinput2');
     inp2.setAttribute('type','checkbox');
    fs.appendChild(inp2);
    var lab2 = document.createElement('label');
     lab2.style.fontSize = '200%'
     lab2.appendChild(document.createTextNode(String.fromCharCode(177) + ' '));
     lab2.setAttribute('for','wbfinput2');
    fs.appendChild(lab2);
    var sub1 = document.createElement('input');
     sub1.setAttribute('type','button');
     sub1.setAttribute('value','greater than');
     addClickHandler(sub1,function() { filterWbf(true) });
     sub1.setAttribute('onclick','');
    fs.appendChild(sub1)
    fs.appendChild(document.createTextNode(' '));
    var sub2 = document.createElement('input');
     sub2.setAttribute('type','button');
     sub2.setAttribute('value','less than');
     addClickHandler(sub2,function() { filterWbf(false) });
    fs.appendChild(sub2)
   wbf.appendChild(fs);
  obj.appendChild(wbf);  
}

function filterWbf(greater) {
  var plusminus = document.getElementById('wbfinput2').checked;
  var bt = parseInt(document.getElementById('wbfinput1').value);
  var docobj = document.getElementById('bodyContent') || document.getElementById('content') || document.body
  var pos = getElementsByClassName(docobj,'*','mw-plusminus-pos');
  var neg = getElementsByClassName(docobj,'*','mw-plusminus-neg'); 
  var nul = getElementsByClassName(docobj,'*','mw-plusminus-null'); 
  var posneg = pos.concat(neg).concat(nul);

  for(var i=0;i<posneg.length;i++) posneg[i].parentNode.style.backgroundColor = ''

  if(plusminus) {
    spans = posneg;
  } else if(bt > 0) {
    spans = pos.concat(nul);
  } else if(bt < 0) {
    spans = neg.concat(nul);
  } else {
    alert('bad input');
    return;
  }
  for(var i=0;i<spans.length;i++) {
    spans[i].parentNode.style.backgroundColor = '';
    var val = gettext(spans[i]);
    val = parseInt(val.replace(/[^\d+-]/ig,''));
    if((Math.abs(val) > Math.abs(bt) && greater) || (Math.abs(val) < Math.abs(bt) && !greater)) { 
      spans[i].parentNode.style.backgroundColor = '#ffff99';
    }
  }
}

function gettext(object) {
  if (object.nodeType == 3) return object.nodeValue;
  var txt = [];
  var i=0;
  while(object.childNodes[i]) {
    txt[txt.length] = gettext(object.childNodes[i]);
    i++;
  }
  return txt.join('');
}