Google2019-03-04
// ==UserScript==
// @name      Google
// @namespace Google
// @include   https://www.google.*/*
// @version   1.2
// @grant     none
// @run-at    document-start
// ==/UserScript==

var blockedURLs = [
  "example.com/",
];

var finished = 0;

function startup() {
  var stylesheet = window.document.styleSheets[0];
  stylesheet.insertRule("div.g { display: none; }");
  stylesheet.insertRule("a.rg_l { display: none; }");
  stylesheet.insertRule("g-section-with-header { display: none !important; }");
}

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

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

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

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

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

function filterImages() {
  var a = document.getElementsByTagName("a");
  for(var n = 0; n < a.length; n++) {
    var href = a[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);
    a[n].setAttribute("href", href);

    var removed = 0;
    for(var m = 0; m < blockedURLs.length; m++) {
      if(href.indexOf(blockedURLs[m]) != -1) {
        a[n].setAttribute("style", "display: none !important");
        a[n].parentNode.setAttribute("style", "max-width: 1px; max-height: 1px");
        removed = 1;
        break;
      }
    }

    if(removed == 0) {
      a[n].setAttribute("style", "display: inherit !important");
    }
  }
}

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