thoughtsunificator / Google Search Results Filter

// ==UserScript==
// @license				      MIT
// @copyright           Copyright (c) 2019, Romain Lebesle <oss@thoughtsunificator.me> (https://thoughtsunificator.me)
// @namespace           https://thoughtsunificator.me
// @name                Google Search Results Filter
// @author				      Romain Lebesle <oss@thoughtsunificator.me> (https://thoughtsunificator.me)
// @homepageURL		    https://thoughtsunificator.me/
// @supportURL          https://thoughtsunificator.me/
// @version             1.0
// @description         Google Blacklist Results
// @run-at 				      document-start
// @include             https://google.*/*
// ==/UserScript==

(function() {

	if(location.pathname !== "/search") {
		return
	}

	console.log("Running blacklist..")

	const BLACKLIST = [
		"chat",
		"similar",
		"related",
		"alternative",
		"comment",
		"reply",
		"/thread",
		"/topic",
		"community",
		"forum",
		"viewtopic.php",
		"showthread.php",
		"rating",
		"best",
		"worst",
		"lyrics",
		"answers.yahoo.com"
	];
	const WHITELIST = [
		"support.microsoft.com",
		"www.microsoft.com"
	];

	const displayedDomains = [];
	const displayedURLs = [];
	const hiddenDomains = [];
	const hiddenURLs = [];

	const observer = new MutationObserver(async mutationsList => {
			for(const mutation of mutationsList) {
				for(const addedNode of mutation.addedNodes) {
					if(addedNode.id === "search") {
						let results = document.querySelectorAll("#search .g");
						if(results.length > 0) {
							results.forEach(function(result) {
								let anchor = result.querySelector("a");
								if(anchor === null) {
									return;
								}
								let domain = anchor.href.split("/")[2];
								let url = anchor.href.toLowerCase();
								let title = anchor.innerText.toLowerCase();
								let description = result.querySelector(".rc > div:nth-child(2) span:last-of-type").innerText.toLowerCase();
								let isInWhiteList = typeof WHITELIST.find(function(keyword) {
									return url.includes(keyword) === true;
								}) !== "undefined";
								let isInBlackList = typeof BLACKLIST.find(function(keyword) {
									return url.includes(keyword) === true || title.includes(keyword) === true || description.includes(keyword) === true;
								}) !== "undefined";
								if(isInWhiteList === false && isInBlackList === true) {
									result.style.display = "none";
								}
								let domainArray = isInBlackList === true ? hiddenDomains : displayedDomains;
								let urlArray  = isInBlackList === true ? hiddenURLs : displayedURLs;
								if(domainArray.includes(domain) === false) {
									domainArray.push(domain);
								}
								urlArray.push(url);
							});
							if(displayedDomains.length === 0) {
								let navLinkBtn = document.querySelector(".nav-link .btn[value=\"next\" i]");
								if(navLinkBtn !== null) {
									navLinkBtn.click();
								}
							}
							return
						}
					}
				}
			}
		})

	observer.observe(document.documentElement, { childList: true, subtree: true })

})()