dzonc / Remove Reddit Ads

// ==UserScript==
// @name         Remove Reddit Ads
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Remove ads on Reddit
// @author       dzonc
// @match        https://www.reddit.com/*
// @license MIT
// @grant        none
// ==/UserScript==
(function() {
    'use strict';
    // Function to remove elements with the class 'promoted' or 'promotedlink' or data tags containing 'adserver'
    function removeRedditAds() {
        const siteTable = document.getElementById('siteTable');
        if (siteTable) {
            const adElements = siteTable.querySelectorAll('.promoted, .promotedlink');
            adElements.forEach((element) => {
                element.remove();
            });
            const allElements = siteTable.querySelectorAll('[data-tag*="adserver"]');
            allElements.forEach((element) => {
                element.remove();
            });
        }
    }
    // Run the function when the page loads and when new content is loaded (e.g., when scrolling)
    window.addEventListener('load', removeRedditAds);
    document.addEventListener('DOMNodeInserted', removeRedditAds);
})();