(2024)reddit: remove Promoted posts

Remove Promoted posts that are shown as relevant answers even if they're not.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name     (2024)reddit: remove Promoted posts
// @name:zh (2024)reddit:移除列表带Promoted标识的广告
// @namespace    acemarx
// @version      1.0.2
// @description  Remove Promoted posts that are shown as relevant answers even if they're not.
// @description:zh 自动移除列表里面的广告帖子(带Promoted标识的)
// @author       acemarx
// @grant    none
// @exclude https://*.reddit.com/robots.txt?upapi=true
// @exclude http://*.reddit.com/robots.txt?upapi=true
// @match        https://*.reddit.com/*
// @match        http://*.reddit.com/*
// @license MIT
// ==/UserScript==
const removeSponsor = () => {
    try {
        const ads = document.querySelectorAll(".promotedlink");
        ads.forEach((ad) => {
            ad?.parentNode?.removeChild(ad);
        });
    } catch (error) {
        console.error("An error occurred while removing sponsor:", error);
    }
};

if (typeof MutationObserver === 'undefined') {
    console.log('Browser does not support MutationObserver.');
} else {
    const observer = new MutationObserver((mutationsList) => {
        for (const mutation of mutationsList) {
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                removeSponsor();
            }
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });
}