Skip to content

Instantly share code, notes, and snippets.

@ovelny
Created January 25, 2022 22:09
Show Gist options
  • Save ovelny/4aa69dd7879c2bd7f44b692fe96bd5c1 to your computer and use it in GitHub Desktop.
Save ovelny/4aa69dd7879c2bd7f44b692fe96bd5c1 to your computer and use it in GitHub Desktop.
Filter SEO spam websites in google searches
// ==UserScript==
// @name Google search filter
// @version 1.0.0
// @description Filter / blacklist sites in Google search
// @author ovelny
// @match *://www.google.com/search?*
// @run-at document-start
// ==/UserScript==
blacklist = [
"programiz",
"qastack",
"qa-stack",
"kite",
"w3schools",
"tutorialspoint",
"towardsdatascience",
"geeksforgeeks",
"betterprogramming",
"linuxhint",
"makeuseof",
"codegrepper",
"solveforums"
]
const filterSites = function(blacklist) {
const citeEls = document.getElementsByTagName("cite")
let elsToDelete = []
for (const element of citeEls) {
for (const site of blacklist) {
const regex = new RegExp(`^http.\:\/\/.*${site}.*$`, "gi")
const cite = element.childNodes[0].nodeValue
const match = cite.match(regex)
if (match) {
elsToDelete.push(element.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode)
}
}
}
if (elsToDelete.length) {
elsToDelete = elsToDelete.reverse() // we revert the elements so we can first delete nested ones in results
for (const element of elsToDelete) {
element.remove()
}
}
}
const removeCommonQuestions = function() {
const spans = document.getElementsByTagName("span")
let commonQuestions = []
for (const span of spans) {
if (span) {
if (span.textContent == "People also ask") {
commonQuestions.push(span)
}
}
}
if (commonQuestions.length) {
const commonQuestionsBlock = commonQuestions[0].parentNode.parentNode.parentNode.parentNode
commonQuestionsBlock.remove()
}
}
if (document.readyState !== "loading") {
removeCommonQuestions()
filterSites(blacklist)
} else {
document.addEventListener("DOMContentLoaded", function() {
removeCommonQuestions()
filterSites(blacklist)
})
}
@deqyra
Copy link

deqyra commented May 17, 2022

Thank you SO MUCH for this.

I am so glad I'm not the only soul writhing in pain seeing how low quality websites make their way to the top of search results whenever you're trying to look up docs for anything programming-related. Crazy how your blacklist exactly matches the one I had in mind.

Works perfect too! Thanks again.

@ovelny
Copy link
Author

ovelny commented May 27, 2022

Thank you SO MUCH for this.

I am so glad I'm not the only soul writhing in pain seeing how low quality websites make their way to the top of search results whenever you're trying to look up docs for anything programming-related. Crazy how your blacklist exactly matches the one I had in mind.

Works perfect too! Thanks again.

You're welcome, glad this is useful! ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment