Greasy Fork

隱藏特定學校文章 - Dcard 廢版

在 Dcard 的廢版隱藏來自特定學校的文章

目前为 2024-11-12 提交的版本。查看 最新版本

// ==UserScript==
// @name         隱藏特定學校文章 - Dcard 廢版
// @name:en      Hide Specific School Posts - Dcard (Deprecated)
// @description  在 Dcard 的廢版隱藏來自特定學校的文章
// @description:en This script allows you to hide anonymous posts from schools you don't like!
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @match        https://www.dcard.tw/*
// @grant        none
// @author       Franky
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const blockedSchools = ["國立清華大學", "國立中正大學", "玄奘大學", "國立空中大學"];

    function hideBlockedSchoolPosts() {
        const articles = document.querySelectorAll("article");
        articles.forEach(article => {
            const schoolElement = article.querySelector("a div:nth-child(2)");
            if (schoolElement && blockedSchools.includes(schoolElement.textContent.trim())) {
                article.style.display = "none";
            }
        });
    }

    function initObserver() {
        // 清除先前的 MutationObserver,避免重複監聽
        if (window.articleObserver) {
            window.articleObserver.disconnect();
        }

        // 僅在 `/f/whysoserious` 路徑下啟用
        if (location.pathname.startsWith('/f/whysoserious')) {
            hideBlockedSchoolPosts();

            // 初始化並啟動 MutationObserver
            const observer = new MutationObserver(hideBlockedSchoolPosts);
            observer.observe(document.body, { childList: true, subtree: true });
            window.articleObserver = observer; // 將 observer 儲存在 window 中方便管理
        }
    }

    // 初次檢查路徑並設置 observer
    initObserver();

    // 監聽 URL 變化(單頁應用的情況)
    let lastPathname = location.pathname;
    setInterval(() => {
        if (location.pathname !== lastPathname) {
            lastPathname = location.pathname;
            initObserver();
        }
    }, 1000); // 每秒檢查一次 URL 變化

})();