Greasy Fork

Facebook 清爽化

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

// ==UserScript==
// @name         Facebook Cleaner 
// @name:zh-TW   Facebook 清爽化
// @name:zh-CN   Facebook 清爽化
// @namespace    http://tampermonkey.net/
// @version      3.4
// @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'
    ];
    const moreKeywords = ['顯示更多', '更多', 'See more', 'More'];
    const lessKeywords = ['顯示較少', 'Show less'];

    // 只針對左側功能列的 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;
            for (let i = 0; i < 5; i++) {
                if (li.parentElement && li.parentElement.tagName === 'LI') {
                    li = li.parentElement;
                    break;
                }
                if (li.parentElement) li = li.parentElement;
            }
            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 removeMoreAndLessButtons() {
        // 移除「顯示更多」類
        document.querySelectorAll('[role="button"]').forEach(btn => {
            if (btn && btn.innerText && moreKeywords.some(kw => btn.innerText.trim() === kw || btn.innerText.includes(kw))) {
                btn.style.display = 'none';
            }
        });
        // 移除「顯示較少」類
        document.querySelectorAll('[role="button"]').forEach(btn => {
            if (btn && btn.innerText && lessKeywords.some(kw => btn.innerText.trim() === kw || btn.innerText.includes(kw))) {
                btn.style.display = 'none';
            }
        });
    }

    // 只展開一次
    let expanded = false;
    let expandTries = 0;
    const maxExpandTries = 15; // 最多嘗試 15 次(約 7.5 秒)

    function tryAutoExpandLeftSidebarLoop() {
        if (expanded || expandTries >= maxExpandTries) return;
        expandTries++;
        // 搜尋所有 role="button" 的元素,遞迴找裡面有「顯示更多」的 span
        const btns = Array.from(document.querySelectorAll('[role="button"]'));
        for (const btn of btns) {
            if (btn.offsetParent === null) continue; // 跳過隱藏的
            if (btn.innerText && moreKeywords.some(kw => btn.innerText.trim() === kw || btn.innerText.includes(kw))) {
                expanded = true;
                btn.click();
                // 展開後延遲移除按鈕
                setTimeout(removeMoreAndLessButtons, 800);
                return;
            }
        }
        // 若沒找到,0.5 秒後再試
        setTimeout(tryAutoExpandLeftSidebarLoop, 500);
    }

    // 監控左側欄內容變動
    const leftSidebarObserver = new MutationObserver(() => {
        if (!expanded) tryAutoExpandLeftSidebarLoop();
    });
    function observeLeftSidebar() {
        // 嘗試找到左側欄的容器
        const nav = document.querySelector('nav[role="navigation"], [role=navigation]');
        if (nav) {
            leftSidebarObserver.observe(nav, { childList: true, subtree: true });
            tryAutoExpandLeftSidebarLoop();
        } else {
            // 若還沒出現,稍後再試
            setTimeout(observeLeftSidebar, 500);
        }
    }

    // 其他元素隱藏
    function hideOtherElements() {
        selectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(el => {
                el.style.display = 'none';
            });
        });
        hideLeftSidebarByText();
        hideRightSidebarByTitle();
        removeMarketplaceButton();
        hidePolicyLinks();
    }

    // 頁面載入後開始監控左側欄
    window.addEventListener('load', () => {
        observeLeftSidebar();
    });

    // 監控內容變動,及時隱藏新出現的「顯示較少」按鈕
    const observer = new MutationObserver(() => {
        hideOtherElements();
        // 不在這裡移除「顯示更多」按鈕
    });
    observer.observe(document.body, { childList: true, subtree: true });

    // 首次執行(不移除「顯示更多」按鈕)
    hideOtherElements();
})();