Reddit 广告拦截器 (优化版)

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

目前為 2024-09-21 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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();
})();