Last active
April 29, 2023 03:30
-
-
Save jerblack/a26603e21810043ebd6a5659a32d0a6f to your computer and use it in GitHub Desktop.
reddit blacklist. automatically hide any posts or comments with blacklisted text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Use an extension like "User Javascript and CSS" from the Chrome Web Store to load this JS. */ | |
/* https://chrome.google.com/webstore/detail/user-javascript-and-css/nbhcbdghjpllgmfilhnhkllmkecfmpld */ | |
const blacklist = [ | |
"stock", "bitcoin", "gamestop", "gme", "wallstreetbets", "wsb", "crypto" | |
] | |
window.addEventListener("load", () => { | |
new UrlWatcher() | |
}) | |
let uu, pp, cc | |
const postPageSelector = ".rpBJOHq2PR60pnwJlUyP0" | |
const postTextSelector = "h3" | |
const commentPageSelector = "._1YCqQVO-9r-Up6QPB9H6_4" | |
const commentTextSelector = "div[data-testid='comment']" | |
class PageWatcher { | |
constructor(listSelector, textSelector) { | |
pp = this | |
this.listSelector = listSelector | |
this.textSelector = textSelector | |
this.list = undefined | |
this.observer = undefined | |
this.blacklist = blacklist | |
this.blacklistRegex = new RegExp(blacklist.join("|"), "gi") | |
this.counter = new Counter() | |
} | |
start() { | |
this.list = document.querySelector(this.listSelector) | |
if (!this.list) {return} | |
this.observer = new MutationObserver((m)=>{ | |
this.update(m) | |
}) | |
this.observer.observe(this.list, {childList: true}) | |
console.log("watcher started") | |
this.checkExisting() | |
} | |
stop() { | |
if (this.observer) { | |
this.observer.disconnect() | |
this.observer = undefined | |
console.log("watcher stopped") | |
} | |
} | |
update(mutations) { | |
console.log("update called") | |
for (const mutation of mutations) { | |
for (const node of mutation.addedNodes) { | |
const text = node.querySelector(this.textSelector) | |
if (!text) { | |
continue | |
} | |
if (text.innerText) { | |
if (this.blacklistRegex.test(text.innerText)) { | |
console.log("watcher MATCH FOUND in observer") | |
node.style.display = "none" | |
this.counter.add() | |
} | |
} | |
} | |
} | |
} | |
checkExisting() { | |
for (let node of this.list.children) { | |
const text = node.querySelector(this.textSelector) | |
if (!text) { | |
continue | |
} | |
if (text.innerText) { | |
if (this.blacklistRegex.test(text.innerText)) { | |
console.log("watcher MATCH FOUND in existing") | |
node.style.display = "none" | |
this.counter.add() | |
} | |
} | |
} | |
} | |
} | |
class UrlWatcher { | |
constructor() { | |
uu = this | |
this.currentUrl = undefined | |
this.pageWatcher = undefined | |
this.observer = new MutationObserver(()=>{ | |
this.onChange() | |
}) | |
this.observer.observe(document.body, {childList: true}) | |
this.onChange() | |
} | |
onChange() { | |
if (document.location.href === this.currentUrl) {return} | |
this.currentUrl = document.location.href | |
if (this.pageWatcher) { | |
this.pageWatcher.stop() | |
this.pageWatcher = undefined | |
} | |
if (this.currentUrl.includes("/comments/")) { | |
console.log("comment page found") | |
this.pageWatcher = new PageWatcher(commentPageSelector, commentTextSelector) | |
} else { | |
console.log("post page found") | |
this.pageWatcher = new PageWatcher(postPageSelector, postTextSelector) | |
} | |
this.pageWatcher.start() | |
} | |
} | |
class Counter { | |
constructor() { | |
cc = this | |
this.counter = undefined | |
this.i = 0 | |
this.lastI = -1 | |
this.reset() | |
} | |
setPlaceholder() { | |
if (!this.counter) { | |
this.counter = document.getElementById("header-search-bar") | |
if (!this.counter) {return} | |
} | |
if (this.i !== this.lastI) { | |
let p = `Search Reddit [${this.i}]` | |
console.log(`setting placeholder to ${p}`) | |
this.counter.placeholder = p | |
this.lastI = this.i | |
} | |
} | |
add() { | |
this.i += 1 | |
this.setPlaceholder() | |
} | |
reset() { | |
this.i = 0 | |
this.setPlaceholder() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment