Blocks all of the promoted advertisements on Reddit.
// ==UserScript==
// @name Reddit Promotion Blocker
// @namespace http://tampermonkey.net/
// @version 0.6.0
// @description Blocks all of the promoted advertisements on Reddit.
// @author Aiden Charles
// @match http://reddit.com/*
// @match https://reddit.com/*
// @match http://www.reddit.com/*
// @match https://www.reddit.com/*
// @match http://old.reddit.com/*
// @match https://old.reddit.com/*
// @grant none
// ==/UserScript==
(function () {
console.log("Reddit promotion blocker script is running!");
function hide(selector, parentLevels = 0, text = "") {
document.querySelectorAll(selector).forEach(element => {
if (text === "" || element.textContent.includes(text)) {
let target = element;
for (let i = 0; i < parentLevels; i++) {
if (target.parentElement) {
target = target.parentElement;
} else {
return;
}
}
target.style.display = 'none';
}
});
}
const elementsToHide = {
oldReddit: [
["span", 5, "promoted"],
["div .ad-container", 1],
["span .promoted-tag", 2]
],
newReddit: [
["shreddit-ad-post"],
["shreddit-comments-page-ad"],
["shreddit-comment-tree-ad"],
["shreddit-async-loader[bundlename='sidebar_ad']"],
["shreddit-async-loader[bundlename='feed_announcement']"],
["div[data-before-content='advertisement']", 3],
["div .adsense-ad", 2],
["span .promoted-tag", 2],
["div .promotedlink"]
]
};
const observer = new MutationObserver(function () {
const isOldReddit = window.location.hostname === "old.reddit.com";
const targets = isOldReddit ? elementsToHide.oldReddit : elementsToHide.newReddit;
targets.forEach(args => hide(...args));
});
observer.observe(document, {childList: true, subtree: true});
})();