byuu.org
  1. byuu.org
  2. projects
  3. google

Google

Important Notice: I have moved to a new domain. This site is no longer being updated. Read more here.

Google2019-12-17 13:42:17

// ==UserScript==
// @name      Google
// @namespace Google
// @include   https://www.google.*/*
// @author    byuu
// @version   1.8
// @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("span.dtviD { display: none; }");
  stylesheet.insertRule("a.rg_l { display: none; }");
  //don't show image search result link names until after they've been filtered
  stylesheet.insertRule("div.isv-r { visibility: hidden; }");
  //this rule prevents the "Cached" dropdown arrow from overlapping ling text when URLs word-wrap:
  stylesheet.insertRule("div.yWc32e { float: right; }");

  //optional: remove inline elements (Twitter, YouTube, image packs, etc.)
  //stylesheet.insertRule("g-section-with-header { display: none !important; }");
}

function finish() {
  if(finished == 1) return;
  finished = 1;
  var stylesheet = window.document.styleSheets[0];
  stylesheet.insertRule("div.g { display: inherit !important; }");
  stylesheet.insertRule("span.dtviD { 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;
      }
    }

    //restore full URLs in search results
    var div = node[n].getElementsByTagName("div");
    if(div.length > 0) {
      var cite = div[0].getElementsByTagName("cite");
      if(cite.length > 0) {
        cite[0].innerHTML = href.replace("http://", "").replace("https://", "");
        //the dropdown arrow position is computed based on a second cite element
        var sibling = node[n].nextSibling;
        if(sibling) {
          div = sibling.getElementsByTagName("div");
          if(div.length > 0) {
            var cite = div[0].getElementsByTagName("cite");
            if(cite.length > 0) {
              cite[0].innerHTML = href.replace("http://", "").replace("https://", "");
            }
          }
        }
      }
    }
  }

  //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;
      }
    }
  }

  //filter the image pack results
  //todo: this only works on the first visible results, subsequent images are loaded dynamically and missed
  node = document.getElementsByTagName("img");
  for(var n = 0; n < node.length; n++) {
    var id = node[n].getAttribute("id");
    if(id === null) continue;
    if(id.indexOf("dimg_") == -1) continue;
    var title = node[n].getAttribute("title");
    if(title === null) continue;

    for(var m = 0; m < blocked.length; m++) {
      if(title.indexOf(blocked[m]) != -1) {
        node[n].parentNode.parentNode.parentNode.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.getElementsByClassName("VFACy");
  for(var n = 0; n < node.length; n++) {
    node[n].removeAttribute("onmousedown");
    var href = node[n].getAttribute("href");
    if(href === null) continue;

    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) {
      var style = node[n].parentNode.getAttribute("style");
      if(style !== null && style.indexOf("visibility") == -1) {
        node[n].parentNode.setAttribute("style", "visibility: inherit; " + style);
      }
    }
  }
}

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

Permalink