Facebook 自動展開與互動增強

混合觸發模式:滑鼠游標自動展開查看更多 + 點讚 + 影片音量調整

当前为 2025-05-13 提交的版本,查看 最新版本

// ==UserScript==
// @name         Facebook 自動展開與互動增強
// @namespace    http://tampermonkey.net/
// @version      2025.05.13.1
// @description  混合觸發模式:滑鼠游標自動展開查看更多 + 點讚 + 影片音量調整
// @author       You
// @match        https://www.facebook.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    // 原有自動展開功能變數
    const CLICK_INTERVAL = 500; // 點擊間隔保護(ms)
    let lastClickTime = 0;

    // 新增功能變數
    const DEFAULT_VOLUME = 0.15; // 影片預設音量 (15%)
    let likeCoolingDown = false;

    // === 原有自動展開功能 ===
    document.addEventListener('mouseover', function(event) {
        const target = event.target;
        if (isSeeMoreButton(target) && checkClickInterval()) {
            safeClick(target);
        }
    });

    function isSeeMoreButton(element) {
        return (
            element.getAttribute('role') === 'button' &&
            element.getAttribute('aria-expanded') !== 'true' &&
            element.innerText === '查看更多'
        );
    }

    function handleOtherButtons() {
        document.querySelectorAll('div[role="button"]:not([aria-expanded="true"])').forEach(btn => {
            if (isOtherExpandButton(btn) && checkClickInterval()) {
                safeClick(btn);
            }
        });
    }

    function isOtherExpandButton(element) {
        return (
            element.innerText !== '查看更多' &&
            (/^查看全部\d+則回覆$/.test(element.innerText) ||
             /.+已回覆/.test(element.innerText) ||
             /^查看\d+則回覆$/.test(element.innerText))
        );
    }

    function checkClickInterval() {
        const now = Date.now();
        if (now - lastClickTime > CLICK_INTERVAL) {
            lastClickTime = now;
            return true;
        }
        return false;
    }

    function safeClick(element) {
        try {
            element.click();
            console.log('已展開:', element.innerText.trim());
        } catch (e) {
            console.warn('展開失敗:', e);
        }
    }

    // === 新增功能 1:影片音量調整 ===
    function adjustVideoVolume(video) {
        if (video && video.volume !== DEFAULT_VOLUME) {
            video.volume = DEFAULT_VOLUME;
            video.muted = false;
        }
    }

    function processAllVideos() {
        document.querySelectorAll('video').forEach(adjustVideoVolume);
    }

    // === 新增功能 2:滑鼠指向自動點讚 ===
    document.addEventListener('mouseover', function(event) {
        if (likeCoolingDown) return;

        const target = event.target.closest('div[aria-label="讚"]');
        if (target &&
            target.getAttribute('aria-pressed') !== 'true' &&
            isButtonVisible(target)
        ) {
            likeCoolingDown = true;
            setTimeout(() => likeCoolingDown = false, 1000); // 冷卻 1 秒防誤觸
            safeClick(target);
            console.log('已自動點讚');
        }
    });

    function isButtonVisible(button) {
        const rect = button.getBoundingClientRect();
        return rect.width > 0 && rect.height > 0;
    }

    // === 初始化與監聽 ===
    function init() {
        // 原有功能
        handleOtherButtons();
        setInterval(handleOtherButtons, 800);
        new MutationObserver(() => {
            handleOtherButtons();
            processAllVideos(); // 動態內容載入時檢查影片
        }).observe(document.body, { childList: true, subtree: true });

        // 新增功能初始化
        processAllVideos(); // 頁面載入時調整音量
    }

    window.addEventListener('load', init);
})();