Greasy Fork

Greasy Fork is available in English.

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

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

当前为 2024-11-12 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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 變化

})();