Reddit 广告拦截器 (优化版)

移除 Reddit 页面上的广告,包括带有 shreddit-dynamic-ad-link 类的元素

Fra 21.09.2024. Se den seneste versjonen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Reddit 广告拦截器 (优化版)
// @namespace    https://www.reddit.com/
// @version      0.3
// @description  移除 Reddit 页面上的广告,包括带有 shreddit-dynamic-ad-link 类的元素
// @author       YourName
// @match        *://*.reddit.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const adSelectors = [
        'div[data-testid="ad"]',                      // Reddit 广告容器
        '.promotedlink',                              // 推广帖子
        'div[data-adclicklocation]',                  // 带有特定点击位置的广告
        '.shreddit-dynamic-ad-link.absolute.inset-0'  // 你提供的自定义广告类
    ];

    // 移除广告的函数
    function removeAds() {
        adSelectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(ad => ad.remove());
        });
    }

    // 使用 MutationObserver 监听 DOM 变化,实时移除新出现的广告
    const observer = new MutationObserver(removeAds);
    observer.observe(document.body, { childList: true, subtree: true });

    // 初始执行一次移除现有广告
    removeAds();
})();