Greasy Fork

Facebook 清爽化

删除FaceBook多余、不常使用的功能按钮和要素,使整个页面看起来更加简洁

目前为 2025-04-19 提交的版本。查看 最新版本

// ==UserScript==
// @name         Facebook Cleaner 
// @name:zh-TW   Facebook 清爽化
// @name:zh-CN   Facebook 清爽化
// @namespace    http://tampermonkey.net/
// @version      3.2
// @description  Remove unnecessary and rarely used feature buttons and elements from Facebook to make the entire page look cleaner and more streamlined.
// @description:zh-TW 刪除FaceBook多餘、不常使用的功能按鈕和要素,使整個頁面看起來更加簡潔
// @description:zh-CN 删除FaceBook多余、不常使用的功能按钮和要素,使整个页面看起来更加简洁
// @author       chatgpt
// @match        https://www.facebook.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 你要隱藏的左側功能名稱(完全比對)
    const leftKeywords = [
        '動態回顧', '我的珍藏', 'Marketplace', '兒童版 Messenger', '玩遊戲',
        '近期廣告動態', '訂單和付款', '氣候科學中心', '募款活動', '廣告管理員', 'Meta Quest 3S'
    ];

    // 只針對左側功能列的 a 標籤做精準比對
    function hideLeftSidebarByText() {
        document.querySelectorAll('nav a[role="link"], [role=navigation] a[role="link"]').forEach(a => {
            let match = false;
            a.querySelectorAll('span.x1lliihq').forEach(span => {
                if (
                    span.textContent &&
                    leftKeywords.includes(span.textContent.trim()) &&
                    span.children.length === 0
                ) {
                    match = true;
                }
            });
            if (match) {
                a.style.display = 'none';
            }
        });
    }

    // 右側欄精準隱藏
    function hideRightSidebarByTitle() {
        const rightKeywords = ['贊助', '聯絡人', '群組聊天室'];
        document.querySelectorAll('h3').forEach(h3 => {
            if (h3.textContent && rightKeywords.some(kw => h3.textContent.includes(kw))) {
                let parent = h3;
                for (let i = 0; i < 6; i++) {
                    if (parent.parentElement) parent = parent.parentElement;
                }
                if (parent && parent.offsetWidth > 200) {
                    parent.style.display = 'none';
                }
            }
        });
    }

    // Marketplace 按鈕只移除 li
    function removeMarketplaceButton() {
        document.querySelectorAll('a[aria-label="Marketplace"], a[href="/marketplace/?ref=app_tab"], a[href="/marketplace/"]').forEach(a => {
            let li = a;
            // 向上找最近的 li
            for (let i = 0; i < 5; i++) {
                if (li.parentElement && li.parentElement.tagName === 'LI') {
                    li = li.parentElement;
                    break;
                }
                if (li.parentElement) li = li.parentElement;
            }
            // 只移除 li,不動其他層
            if (li.tagName === 'LI') {
                li.remove();
            }
        });
    }

    // 政策條款
    function hidePolicyLinks() {
        const policyKeywords = [
            '隱私政策', '服務條款', '廣告', 'Ad Choices', 'Cookie', 'Meta © 2025'
        ];
        document.querySelectorAll('footer, div[role="contentinfo"]').forEach(container => {
            policyKeywords.forEach(kw => {
                if (container.textContent.includes(kw)) {
                    container.style.display = 'none';
                }
            });
        });
    }

    // 其他精準選擇器
    const selectors = [
        'footer',
        'div[role="contentinfo"]',
        'div[aria-label="Facebook"] > div:last-child'
    ];

    function hideElements() {
        selectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(el => {
                el.style.display = 'none';
            });
        });
        hideLeftSidebarByText();
        hideRightSidebarByTitle();
        removeMarketplaceButton();
        hidePolicyLinks();
    }

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