Skip to content

Instantly share code, notes, and snippets.

@attitude
Last active May 29, 2016 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save attitude/6bcb1d110f414481a0f86f1acedbdbc5 to your computer and use it in GitHub Desktop.
Save attitude/6bcb1d110f414481a0f86f1acedbdbc5 to your computer and use it in GitHub Desktop.
Tiny script to dim some Google SERPs in Chrome; requires Control Freak extension.
(function (w, domains) {
'use strict';
var to,
// Google SERP div#search
search,
// Array of links
links,
// Passed arg iterator
domainsI,
// Passed arg length
domainsL = domains.length,
// Function to look for matching links
lookForAnchorElements = function () {
// Lookup links
links = search.getElementsByTagName('a');
if (links.length > 0) {
var i, l = links.length, parent;
// Loop through links
for (i = 0; i < l; i = i + 1) {
// Loop through passed domains
for (domainsI = 0; domainsI < domainsL; domainsI = domainsI + 1) {
// Link has matching href attr
if (!links[i].$$hiddenSerp && links[i].href && links[i].href.match('/' + domains[domainsI] + '/')) {
// Get parent...
parent = links[i].parentElement;
// ... with proper class
while (parent.className !== 'g' || parent.nodeName.toLowerCase() === 'body') {
parent = parent.parentElement;
}
// (Something went wrong)
if (parent.className === 'g' && !parent.$$hiddenSerp) {
// Mark elements for easier skipping
parent.$$hiddenSerp = true;
links[i].$$hiddenSerp = true;
// Set opacity
parent.style.opacity = 0.25;
}
}
}
}
}
},
// Function to look for the div#search element
lookForSearchElement = function () {
search = w.document.getElementById('search');
// If already found...
if (search) {
// ...clear timeout hack from below
w.clearInterval(to);
// Start links lookup
to = w.setTimeout(lookForAnchorElements, 300);
// Set-up observer
new w.MutationObserver(function () {
// Clear previous link lookup
w.clearTimeout(to);
// Set new lookup
to = w.setTimeout(lookForAnchorElements, 300);
}).observe(
// Observed element
search,
// Observiing options
{childList: true, subtree: true, attributes: true}
);
}
};
// Loop until div#search element appears in DOM
to = w.setInterval(lookForSearchElement, 300);
}(window, ['www.anyannoyingdomain.com'])); // Array of regexes of annoying domains you want to dim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment