User:Stevage/filterwatchlist.user.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.
// ==UserScript==

// @name           Filter watchlist
// @namespace      stevage
// @description    Removes various namespaces from watchlist display
// @include        *.wikipedia.org/*Special:Watchlist*
// ==/UserScript==

(
function() {
  function hideNamespace() {

    if (!document.getElementById('bodyContent')) {
        return;
    }
    
    this.add_buttons();
    
  }

  hideNamespace.prototype.add_buttons = function() {
    GM_log('in add_buttons');

    // Create the hide Wikipedia namespace buttion
    var 
    button1 = document.createElement('input');
    button1.setAttribute('id', 'hideNamespace_button1');
    button1.className = 'searchbutton';
    button1.style.marginLeft = '5px';
    button1.setAttribute('type', 'button');
    button1.value = 'Hide Wikipedia:';
    button1.onclick = function() { hideNamespace.start('Wikipedia:',0); }

    var 
    button2 = document.createElement('input');
    button2.setAttribute('id', 'hideNamespace_button2');
    button2.className = 'searchbutton';
    button2.style.marginLeft = '5px';
    button2.setAttribute('type', 'button');
    button2.value = 'Hide Wikipedia talk:';
    button2.onclick = function() { hideNamespace.start('Wikipedia_talk:',0); }

    var 
    button3 = document.createElement('input');
    button3.setAttribute('id', 'hideNamespace_button3');
    button3.className = 'searchbutton';
    button3.style.marginLeft = '5px';
    button3.setAttribute('type', 'button');
    button3.value = 'Hide article talk:';
    button3.onclick = function() { hideNamespace.start('Talk:',0); }

    var 
    button4 = document.createElement('input');
    button4.setAttribute('id', 'hideNamespace_button4');
    button4.className = 'searchbutton';
    button4.style.marginLeft = '5px';
    button4.setAttribute('type', 'button');
    button4.value = 'Hide userspace/talk:';
    button4.onclick = function() { 
      hideNamespace.start('User:',0); 
      hideNamespace.start('User_talk:',0);
      }

    var 
    button5 = document.createElement('input');
    button5.setAttribute('id', 'hideNamespace_button5');
    button5.className = 'searchbutton';
    button5.style.marginLeft = '5px';
    button5.setAttribute('type', 'button');
    button5.value = 'Hide all talk:';
    button5.onclick = function() { 
      hideNamespace.start('Talk:',0); 
      hideNamespace.start('User_talk:',0);
      hideNamespace.start('Wikipedia_talk:',0);
      hideNamespace.start('Template_talk:',0);
      }

    var 
    button6 = document.createElement('input');
    button6.setAttribute('id', 'hideNamespace_button6');
    button6.className = 'searchbutton';
    button6.style.marginLeft = '5px';
    button6.setAttribute('type', 'button');
    button6.value = 'Hide everything:';
    button6.onclick = function() { 
      hideNamespace.start('Talk:',0); 
      hideNamespace.start('User:',0); 
      hideNamespace.start('User_talk:',0);
      hideNamespace.start('Wikipedia:',0);
      hideNamespace.start('Wikipedia_talk:',0);
      hideNamespace.start('Template:',0);
      hideNamespace.start('Template_talk:',0);
      }

    var 
    button7 = document.createElement('input');
    button7.setAttribute('id', 'hideNamespace_button7');
    button7.className = 'searchbutton';
    button7.style.marginLeft = '5px';
    button7.setAttribute('type', 'button');
    button7.value = 'Hide logged in users:';
    button7.onclick = function() { 
      hideNamespace.start('',1); 
      }

   
    // Add the buttons to the page
    var whereto = document.getElementById('bodyContent');
    whereto.parentNode.insertBefore(button1, whereto);
    whereto.parentNode.insertBefore(button2, whereto);
    whereto.parentNode.insertBefore(button3, whereto);
    whereto.parentNode.insertBefore(button4, whereto);
    whereto.parentNode.insertBefore(button5, whereto);
    whereto.parentNode.insertBefore(button6, whereto);
    whereto.parentNode.insertBefore(button7, whereto);
  }

  hideNamespace.prototype.start = function(namespacetohide, hideloggedin) {
    var body;
    body = document.getElementById('bodyContent');

    if (body) {
      var changes;
      if (!hideloggedin) {
        changes = document.evaluate(
          'id("bodyContent")/DIV/A[starts-with(@HREF,"/wiki/' + namespacetohide + '") and @title=text()]',
          document,
          null,
          XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
          null
        );
        GM_log(changes.snapshotLength + " A's starting with " + namespacetohide);
      } else {
        changes= document.evaluate(
//          'id("bodyContent")/DIV/A[@title="Special:Contributions" and text() != "contribs"]',
          'id("bodyContent")/DIV/A[text() = "contribs"]',
          document,
          null,
          XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
          null
        );
        GM_log(changes.snapshotLength + " A's not by logged-in users.");
      }  

      for (var i = 0; i < changes.snapshotLength; i++) {
        var change = changes.snapshotItem(i);
        
        
        var x = change.previousSibling;
        while (x != null && x.nodeName != "BR") {
          GM_log("Deleting node: " + change.href + " --> " + x.nodeName);
          var y = x.previousSibling;
          //GM_log("  next: " + y.innerHTML +  " (" + y.nodeName + ")");
          x.parentNode.removeChild(x);
          //GM_log("Deleted node.");
          x = y;
        }
        
        x = change.nextSibling;
        while (x != null && x.nodeName != "BR") {
          GM_log("Deleting node2: " + change.href + " --> " + x.nodeName);
          var y = x.nextSibling;
          x.parentNode.removeChild(x);
          x = y;
        }

        GM_log("Deleting node: " + change.href);
        change.parentNode.removeChild(x); //rm BR
        change.parentNode.removeChild(change);
      }//for
    } //if hist
  } // function 'start'



  var hideNamespace = new hideNamespace();
  document.hideNamespace = hideNamespace;

} // unnamed function

) ();