jscher2000 / oujs Forum - My Scripts Filter

// ==UserScript==
// @name        oujs Forum - My Scripts Filter 
// @namespace   JeffersonScher
// @description Hide discussions that are not on one of my scripts (23 Feb 2015f)
// @include     https://openuserjs.org/all*
// @include     https://openuserjs.org/issues*
// @version     0.4.1
// @author      Jefferson "jscher2000" Scher
// @copyright   2015, Jefferson Scher
// @license     BSD 3-clause
// @grant       GM_getValue
// @grant       GM_setValue
// @updateURL https://openuserjs.org/meta/jscher2000/oujs_Forum_-_My_Scripts_Filter.meta.js
// ==/UserScript==

// Get user pref (set/update if needed)
var showMineOnly = GM_getValue("showMineOnly");
if (!showMineOnly || showMineOnly.length == 0){
  showMineOnly = "Y-N";
  GM_setValue("showMineOnly", showMineOnly);
}
// update for 0.2.0
if (showMineOnly.length == 1){
  showMineOnly += "-N";
  GM_setValue("showMineOnly", showMineOnly);
}
// Extract username
var unamelink = document.querySelector("ul.nav a[href^='/users/']"); // Edited 2/23/2015 to check only in nav bar
if (unamelink) var uname = unamelink.textContent;
else { // offline, check for preference
  var uname = GM_getValue("showMineUname");
  if (!uname) uname = "";
}
// Hide rows if desired
doShowHide(showMineOnly);
// Add style rules
var sty = document.createElement("style");
sty.setAttribute("type", "text/css");
sty.innerHTML = '#spanShowMineOnly label{font-size:1em; font-weight:normal; padding:1px 6px 2px; border:3px outset #2C3E50; border-radius:5px;} ' +
  '#spanShowMineOnly input:checked+label{background-color:#fff; border-style:inset;} ' +
  '#spanShowMineOnly input{display:none;} #showMineCog{font-size:0.75em; font-weight:normal; padding:2px; margin:4px; cursor:pointer;}' +
  '#frmOfflineSetup label, #frmOfflineSetup button{font-size:1em; font-weight:normal;';
document.body.appendChild(sty);
// Add toggle controls
var spNew = document.createElement("span");
spNew.innerHTML = '<input type="checkbox" id="chkMineOnly"><label for="chkMineOnly" title="Filter out issues on scripts of other authors\' scripts">My Scripts</label>' +
  ' <input type="checkbox" id="chkShowGen"><label for="chkShowGen" title="Show posts on general discussion boards">General</label>' +
  '<span class="fa fa-cog" style="display:none; font-size:1.5em;" id="showMineCog" title="Offline setup"></span>';
spNew.setAttribute("style", "float:right; margin-right:1em;");
spNew.id = "spanShowMineOnly";
document.querySelector(".list-controls.row.container-fluid").appendChild(spNew);
if (showMineOnly.split("-")[0] == "Y"){
  document.getElementById("chkMineOnly").setAttribute("checked", "checked");
  document.getElementById("chkMineOnly").checked = true;
}
if (showMineOnly.split("-")[1] == "N"){
  document.getElementById("chkShowGen").setAttribute("checked", "checked");
  document.getElementById("chkShowGen").checked = true;
}
document.getElementById("chkMineOnly").addEventListener("change", toggleRows, true);
document.getElementById("chkShowGen").addEventListener("change", toggleRows, true);
// Check for offline mode
if (!unamelink){
  // If not logged in, display the cog, add event listener
  document.getElementById("showMineCog").style.display = "";
  document.getElementById("showMineCog").addEventListener("click", offlineSetup, true);
  // If not logged in AND no username assigned, do not use the white background on "My Scripts"
  if (uname == "") document.getElementById("chkMineOnly").nextElementSibling.style.backgroundColor = "transparent";
}

function toggleRows(e){
  // Capture and update preference
  var chk = e.target;
  if (chk.id == "chkMineOnly"){
    if (chk.checked){
      showMineOnly = "Y" + showMineOnly.substr(1,2);
    } else {
      showMineOnly = "N" + showMineOnly.substr(1,2);
    }
  } else {
    if (chk.checked){
      showMineOnly = showMineOnly.substr(0,2) + "N";
    } else {
      showMineOnly = showMineOnly.substr(0,2) + "Y";
    }
  }
  GM_setValue("showMineOnly", showMineOnly);
  e.stopPropagation();
  chk.blur();
  // Hide or unhide rows
  doShowHide(showMineOnly);
}

function doShowHide(wts){
  var hideOthers = wts.split("-")[0];
  var hideGeneral = wts.split("-")[1];
  // Hide or unhide rows
  var rowEls = document.getElementsByTagName("tbody")[0].rows;
  for (var i=0; i<rowEls.length; i++){
    if (rowEls[i].cells[1].textContent.indexOf(uname+"/") == -1){ // not one of my scripts
      if (rowEls[i].cells[1].textContent.indexOf("/") > -1){ // Scripts of others
        if (hideOthers == "Y") rowEls[i].style.display = "none";
        else rowEls[i].style.display = "";
      } else { // Posts on general boards
        if (hideGeneral == "Y") rowEls[i].style.display = "none";
        else rowEls[i].style.display = "";
      }
    } else { // make sure mine is shown
      rowEls[i].style.display = "";
    }
  }
}

function offlineSetup(e){
  var offForm = document.getElementById("frmOfflineSetup");
  // If offline setup form is displayed, close the form, otherwise show the form
  if (offForm && offForm.style.display != "none"){
    offForm.style.display = "none";
    e.stopPropagation();
    e.target.blur();
    return;
  }
  if (offForm){   // Display form
    offForm.style.display = "";
  } else {        // Create form
    offForm = document.createElement("div");
    offForm.id = "frmOfflineSetup";
    offForm.innerHTML = '<label>Username:&nbsp;<input id="showMineUnameInput" type="text" style="width:12em" placeholder="Offline, filter using..."></label>&nbsp;' +
      '<button id="btnShowMineUnameSave">Save</button>&nbsp;<button id="btnShowMineUnameClose">Close</Close>';
    offForm.setAttribute("style", "z-index:100; position:absolute; right:12px; top:36px; padding:4px 10px; border:1px solid #2C3E50; border-radius:5px; background-color:#eee;");
    document.querySelector(".list-controls.row.container-fluid").appendChild(offForm);
    document.getElementById("showMineUnameInput").value = uname;
    document.getElementById("btnShowMineUnameSave").addEventListener("click", saveOfflineUname, true);
    document.getElementById("btnShowMineUnameClose").addEventListener("click", offlineSetup, true);
  }
}

function saveOfflineUname(e){
  var unameNew = document.getElementById("showMineUnameInput").value;
  var unameOld = GM_getValue("showMineUname");
  if (!unameOld || unameNew != unameOld){
    uname = unameNew;
    GM_setValue("showMineUname", uname);
  }
  e.stopPropagation();
  e.target.blur();
  // Update My Scripts button background color
  if (uname == "") document.getElementById("chkMineOnly").nextElementSibling.style.backgroundColor = "transparent";
  else document.getElementById("chkMineOnly").nextElementSibling.style.backgroundColor = ""; 
  // Hide or unhide rows
  doShowHide(showMineOnly);
}