byuu.orgbyuu.netGitHubGitLabTwitterMastodonYouTubeTwitchWikipediaPatreonRSS Feed
  1. byuu.org
  2. code
  3. google

Google

2019-09-22

// ==UserScript==
// @name      Google
// @namespace Google
// @include   https://www.google.*/*
// @author    byuu
// @version   1.6
// @grant     none
// @run-at    document-start
// ==/UserScript==

var blocked = [
  "example.com",
];

var started  = 0;
var awaiting = 0;
var finished = 0;

function startup() {
  if(started == 1) return;
  var stylesheet = window.document.styleSheets[0];
  if(!stylesheet) return;
  started = 1;
  stylesheet.insertRule("div.g { display: none; }");
  stylesheet.insertRule("a.rg_l { display: none; }");
  //remove inline elements (Twitter, YouTube, etc.)
  stylesheet.insertRule("g-section-with-header { display: none !important; }");
  //don't show image search result link names until after they've been filtered
  stylesheet.insertRule("a.iKjWAf * { color: #ffffff; }");
  //these two rules prevent the image search result link text from overlapping images below them:
  stylesheet.insertRule("a.iKjWAf { display: inherit !important; padding: 0em !important; }");
  stylesheet.insertRule("div.rg_bx { margin-bottom: 50px !important; }");
}

function finish() {
  if(finished == 1) return;
  finished = 1;
  var stylesheet = window.document.styleSheets[0];
  stylesheet.insertRule("div.g { display: inherit !important; }");
}

function main() {
  startup();
  if(window.location.href.indexOf("tbm=isch") != -1) {
    filterImages();
  } else if(window.location.href.indexOf("search?") != -1) {
    filterSearch();
  }
}

function filterSearch() {
  //filter the actual search results
  var node = document.getElementsByTagName("a");
  for(var n = 0; n < node.length; n++) {
    node[n].removeAttribute("onmousedown");
    var href = node[n].getAttribute("href");
    if(href === null) continue;

    for(var m = 0; m < blocked.length; m++) {
      if(href.indexOf(blocked[m]) != -1) {
        node[n].parentNode.parentNode.setAttribute("style", "display: none !important");
        break;
      }
    }
  }

  //filter the knowledge panel image results
  node = document.getElementsByTagName("img");
  for(var n = 0; n < node.length; n++) {
    var title = node[n].getAttribute("title");
    if(title === null) continue;

    var href = node[n].parentNode.parentNode.getAttribute("href");
    if(href === null) continue;
  //todo: for reasons unknown, setting the actual link causes the images to disappear
  //node[n].parentNode.parentNode.setAttribute("href", title);

    for(var m = 0; m < blocked.length; m++) {
      if(title.indexOf(blocked[m]) != -1) {
        node[n].parentNode.setAttribute("style", "display: none !important");
        break;
      }
    }
  }

  if(awaiting == 0) {
    awaiting = 1;
    setTimeout(finish, 500);
  }

  if(finished == 0) {
    var node = document.getElementsByClassName("navend");
    if(node.length > 0) finish();
  }
}

function filterImages() {
  //filter the image links themselves
  var node = document.getElementsByTagName("a");
  for(var n = 0; n < node.length; n++) {
    node[n].removeAttribute("onmousedown");
    var href = node[n].getAttribute("href");
    if(href === null) continue;
    if(href.indexOf("/imgres?imgurl=") == -1) continue;

    href = href.substring(15);
    href = href.substring(0, href.indexOf("&imgrefurl="));
    href = decodeURIComponent(href);
    node[n].setAttribute("href", href);

    var removed = 0;
    for(var m = 0; m < blocked.length; m++) {
      if(href.indexOf(blocked[m]) != -1) {
        node[n].setAttribute("style", "display: none !important");
        node[n].parentNode.parentNode.removeChild(node[n].parentNode);
        removed = 1;
        break;
      }
    }

    if(removed == 0) {
      node[n].setAttribute("style", "display: inherit !important");
      //now that the results are filtered, restore the text colors to make the link text visible
      node[n].nextSibling.firstChild.setAttribute("style", "color: #5f6368");
      node[n].nextSibling.firstChild.nextSibling.firstChild.setAttribute("style", "color: #5f6368");
    }
  }

  //filter the links to the pages containing the images
  for(var n = 0; n < node.length; n++) {
    var link = node[n].getAttribute("link");
    if(link !== null) {
      node[n].setAttribute("href", link);
      node[n].setAttribute("data-cthref", link);
      continue;
    }

    var href = node[n].getAttribute("href");
    if(href === null) continue;
    if(href.indexOf("http") == -1) continue;

    node[n].setAttribute("link", href);
    node[n].setAttribute("data-cthref", href);
    node[n].setAttribute("jsaction", "");
  }
}

startup();
var timer = setInterval(main, 50);