Facebook 清爽化

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

当前为 2025-04-19 提交的版本,查看 最新版本

// ==UserScript==
// @name         Facebook Cleaner 
// @name:zh-TW   Facebook 清爽化
// @name:zh-CN   Facebook 清爽化
// @namespace    http://tampermonkey.net/
// @version      3.3
// @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'
    ];

    // 只執行一次的自動展開
    let expanded = false;
    function autoExpandLeftSidebarOnce() {
        if (expanded) return;
        expanded = true;
        // 點擊「顯示更多」按鈕
        document.querySelectorAll('a[aria-label], div[aria-label], span[aria-label], [role="button"][aria-label]').forEach(btn => {
            if (btn && moreKeywords.some(kw => btn.getAttribute('aria-label') && btn.getAttribute('aria-label').includes(kw))) {
                btn.click();
            }
        });
        document.querySelectorAll('a, div, span, [role="button"]').forEach(btn => {
            if (btn && moreKeywords.some(kw => btn.textContent && btn.textContent.trim() === kw)) {
                btn.click();
            }
        });
        // 移除折疊/收合按鈕
        setTimeout(removeMoreAndLessButtons, 500);
    }

    // 移除「顯示更多」「顯示較少」等按鈕
    function removeMoreAndLessButtons() {
        // 移除「顯示更多」類
        document.querySelectorAll('a[aria-label], div[aria-label], span[aria-label], [role="button"][aria-label]').forEach(btn => {
            if (btn && moreKeywords.some(kw => btn.getAttribute('aria-label') && btn.getAttribute('aria-label').includes(kw))) {
                btn.style.display = 'none';
            }
        });
        document.querySelectorAll('a, div, span, [role="button"]').forEach(btn => {
            if (btn && moreKeywords.some(kw => btn.textContent && btn.textContent.trim() === kw)) {
                btn.style.display = 'none';
            }
        });
        // 移除「顯示較少」類
        document.querySelectorAll('div[role="button"], a[role="button"], span[role="button"]').forEach(btn => {
            if (btn && lessKeywords.some(kw => btn.textContent && btn.textContent.trim() === kw)) {
                btn.style.display = 'none';
            }
        });
    }

    function hideElements() {
        selectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(el => {
                el.style.display = 'none';
            });
        });
        hideLeftSidebarByText();
        hideRightSidebarByTitle();
        removeMarketplaceButton();
        hidePolicyLinks();
        // 每次都嘗試移除「顯示較少」按鈕(因為展開後才會出現)
        removeMoreAndLessButtons();
    }

    // 只在初次載入時自動展開一次
    window.addEventListener('load', () => {
        setTimeout(autoExpandLeftSidebarOnce, 1000);
    });

    // 監控內容變動,及時隱藏新出現的「顯示較少」按鈕
    const observer = new MutationObserver(hideElements);
    observer.observe(document.body, { childList: true, subtree: true });

    // 首次執行
    hideElements();
})();