Skip to content

Instantly share code, notes, and snippets.

@thalesmg
Last active October 23, 2020 18:26
Show Gist options
  • Save thalesmg/e20763fc3f45c568b7a4a8e990c81ea9 to your computer and use it in GitHub Desktop.
Save thalesmg/e20763fc3f45c568b7a4a8e990c81ea9 to your computer and use it in GitHub Desktop.
remove annoying google ads and people also search for
// ==UserScript==
// -*- mode: js2; -*-
// @name Remove ads from google search
// @namespace https://userscripts-mirror.org/users/529924
// @include *://*google.com*/search?*
// @include *://*google.com*/#fp=*
// @include *://*google.com*/webhp?tab=*
// @include *://*google.com*/?gws_rd*
// @include *://*google.com*/#newwindow*
// @grant none
// @author Thales M. G.
// @version 1.3.5
// @downloadURL https://gist.github.com/thalesmg/e20763fc3f45c568b7a4a8e990c81ea9
// @updateURL https://gist.github.com/thalesmg/e20763fc3f45c568b7a4a8e990c81ea9
// ==/UserScript==
// Uses the new MutationObserver listener, instead of the deprecated addEventListener
//Runs once, so you don't even see the ads flashing.
var vitimas = document.getElementsByClassName("ads-container");
for (var ii = 0; ii < vitimas.length; ii++) {
// Hide each one of them. Could be replaced with
// vitimas[ii].parentNode.removeChild(vitimas[ii]);
vitimas[ii].style.display="none";
};
var vitimas4 = document.getElementsByClassName("ads-container-list");
for (var ii = 0; ii < vitimas4.length; ii++) {
vitimas4[ii].style.display="none";
};
// Sometimes, Google changes the class name to something too generic
// So, try to destroy directly the ads
// Uses try/catch because sometimes the pages has not created those elements yet
// Which would generate an error and terminate the script
var vitimas2 = document.getElementById("tads");
var vitimas3 = document.getElementById("tadsb");
var vitimas5 = document.getElementById("mbEnd");
try { vitimas2.style.display = "none"; } catch(e) {};
try { vitimas3.style.display = "none"; } catch(e) {};
try { vitimas5.style.display = "none"; } catch(e) {};
var vitimas4 = document.getElementsByClassName("ads-container-list");
for (var ii = 0; ii < vitimas4.length; ii++) {
vitimas4[ii].style.display="none";
};
cleanScreen();
var observador = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var vitimas2 = document.getElementById("tads");
var vitimas3 = document.getElementById("tadsb");
var vitimas5 = document.getElementById("mbEnd");
try { vitimas2.style.display = "none"; } catch(e) {};
try { vitimas3.style.display = "none"; } catch(e) {};
try { vitimas5.style.display = "none"; } catch(e) {};
var vitimas4 = document.getElementsByClassName("ads-container-list");
for (var ii = 0; ii < vitimas4.length; ii++) {
vitimas4[ii].style.display="none";
};
});
});
var config = { childList: true, subtree: true };
observador.observe(document, config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment