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

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

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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] 广告清理脚本已运行,评论输入框已保留 🧹');
})();