Auto Facebook Reels Scroller

Automatically scrolls through Reels when a video ends!!! You should use ViolentMonkey because Tampermonkey requires enabling developer mode!!!

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Auto Facebook Reels Scroller
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Automatically scrolls through Reels when a video ends!!! You should use ViolentMonkey because Tampermonkey requires enabling developer mode!!!
// @author       hiensumi
// @match        *://www.facebook.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';
    let pollingInterval = null;
    let lastUrl = 'nothing';
    let enabled = false;

    function log(...args) {
        console.log('[AutoReels]', ...args);
    }

    function scrollToNextReel() {
        const nextButton = document.querySelector('[aria-label="Next Card"][role="button"]');
        if (nextButton) {
            nextButton.click();
            log('Scrolled to next reel');
        }
    }

    function activateReelsScript() {
        enabled = true;
        if (pollingInterval) clearInterval(pollingInterval);

        log('Activated for Reels page');
        let scrolled = false;

        pollingInterval = setInterval(() => {
            // console.log(enabled);
            if(!enabled) return;
            let vids = document.querySelectorAll('video');
            if(window.location.href.includes('reel') == true && vids.length){
                let vid = vids[vids.length - 1]
                //log(vid.duration - vid.currentTime);

                if (vid.duration > 2.0 && (vid.duration - vid.currentTime <= 1)) {
                    if (!scrolled) {
                        log('Video ends, scrolling');
                        scrolled = true;
                        setTimeout(() => {
                            scrollToNextReel();
                        }, 2000);
                    }
                } else {
                    scrolled = false;
                }
            }
            else{
                enabled = false;
                setTimeout(() => {
                    log('False activation');
                }, 1000);
                return;
            }
        }, 500);
    }

    function deactivateReelsScript() {
        enabled = false;
        if (pollingInterval) {
            clearInterval(pollingInterval);
            pollingInterval = null;
            log('Deactivated (not on Reels page)');
        }
    }

    navigation.addEventListener("navigate", e => {
        setTimeout(() => {
            log('Navigation: ' + window.location.href);
            if(window.location.href.includes('reel')){
                deactivateReelsScript();
                setTimeout(() => {
                    activateReelsScript();
                }, 1000);
            }
            else{
                deactivateReelsScript();
            }
        }, 3000);
    });
})();