Reddit 去广告增强版(修正版)

自动移除 Reddit 广告,保留评论区输入框

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Reddit 去广告增强版(修正版)
// @namespace    https://reddit.com/
// @version      1.2
// @description  自动移除 Reddit 广告,保留评论区输入框
// @author       Glyn
// @match        https://www.reddit.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
  'use strict';

  const adSelectors = [
    'shreddit-comments-page-ad',
    'shreddit-ad-post',
    'div[data-testid="adpost"]',
    'div[data-testid*="promoted"]',
    'div[data-adclicklocation]',
    'div[id^="ad_"]',
    'faceplate-tracker[thing-type="ad_post"]',
  ];

  const removeAds = () => {
    // 删除明确的广告元素
    adSelectors.forEach(selector => {
      document.querySelectorAll(selector).forEach(el => el.remove());
    });

    // 有些广告被封装在 async-loader 里,我们要精准判断
    document.querySelectorAll('shreddit-async-loader').forEach(el => {
      const bundle = el.getAttribute('bundlename') || '';
      if (
        bundle.includes('ad') &&                     // 包含广告关键字
        !bundle.includes('comment') &&               // 但不是评论区相关
        !bundle.includes('reply') &&
        !bundle.includes('body_header')
      ) {
        el.remove();
      }
    });

    // 删除包含 "Promoted" 标签的帖子
    document.querySelectorAll('span, a').forEach(el => {
      if (el.textContent.trim().toLowerCase() === 'promoted') {
        const post = el.closest('shreddit-post, shreddit-ad-post, div[data-testid="post-container"]');
        if (post) post.remove();
      }
    });
  };

  removeAds();

  const observer = new MutationObserver(() => removeAds());
  observer.observe(document.body, { childList: true, subtree: true });

  console.log('[RedditCleaner] 广告清理脚本已运行,评论输入框已保留 🧹');
})();