Instagram Video Autoplay Controller

Auto-plays next reel when your on the reels page on someone's profile!

当前为 2025-01-26 提交的版本,查看 最新版本

// ==UserScript==
// @name         Instagram Video Autoplay Controller
// @namespace    http://tampermonkey.net/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=instagram.com
// @version      1.0
// @description  Auto-plays next reel when your on the reels page on someone's profile!
// @author       YourName
// @match        *://*.instagram.com/*
// @grant        none
 // @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let currentVideo = null;
    let observer = null;
    let isTabActive = document.visibilityState === 'visible';

    function findVideoElement() {
        return document.querySelector('video.x1lliihq.x5yr21d.xh8yej3');
    }

    function simulateArrowKey() {
        if (!isTabActive) return; // Only act if tab is active
        const rightArrowEvent = new KeyboardEvent('keydown', {
            key: 'ArrowRight',
            code: 'ArrowRight',
            keyCode: 39,
            which: 39,
            bubbles: true,
            cancelable: true
        });
        document.dispatchEvent(rightArrowEvent);
    }

    function setupVideoListener() {
        const video = findVideoElement();
        if (video && video !== currentVideo) {
            currentVideo = video;
            video.addEventListener('ended', simulateArrowKey);
            video.addEventListener('pause', () => {
                if (!isTabActive) return; // Only act if tab is active
                if (!video.ended) video.play();
            });
        }
    }

    function initObserver() {
        observer = new MutationObserver(() => {
            if (!findVideoElement()) return;
            setupVideoListener();
        });
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    // Handle tab visibility changes
    document.addEventListener('visibilitychange', () => {
        isTabActive = document.visibilityState === 'visible';
        if (isTabActive) {
            // Re-initialize when tab becomes active
            initObserver();
            setupVideoListener();
        } else {
            // Cleanup when tab becomes inactive
            if (observer) {
                observer.disconnect();
                observer = null;
            }
        }
    });

    // Initial setup if tab is active
    if (isTabActive) {
        initObserver();
        setupVideoListener();
    }

    // Handle navigation changes (back/forward)
    window.addEventListener('popstate', () => {
        if (isTabActive) {
            if (observer) observer.disconnect();
            initObserver();
        }
    });

})();