Make any Acellus Video Unpaused

Unpauses the video when changing tabs on Acellus ;)

当前为 2024-04-26 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Make any Acellus Video Unpaused
// @namespace    https://greasyfork.org/en/users/1291009
// @version      1.8
// @description  Unpauses the video when changing tabs on Acellus ;)
// @author       BadOrBest
// @license      MIT
// @icon         https://www.google.com/s2/favicons?sz=64&domain=acellus.com
// @match        https://admin192c.acellus.com/student/*
// @grant        none
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @run-at       document-end
// ==/UserScript==
(function() {
    'use strict';

    let isInPiPMode = false;
    let isTabFocused = true;
    let shouldTogglePlayer = true;

    function unpauseMedia(mediaElements) {
        mediaElements.forEach(function(mediaElement) {
            if (document.hidden && !isTabFocused && mediaElement.paused && !mediaElement.getAttribute('data-user-paused')) {
                mediaElement.play();
            }
        });
    }

    function controlMedia(mediaElements) {
        unpauseMedia(mediaElements);
    }

    function handleVisibilityChange(mediaElements) {
        if (document.hidden && !isTabFocused) {
            controlMedia(mediaElements);
            if (shouldTogglePlayer && !isInPiPMode) {
                enterPIPMode(mediaElements);
            }
        } else {
            if (!isTabFocused && isInPiPMode) {
                exitPIPMode();
            }
        }
    }

    function handleMutations(mutationsList, observer) {
        const mediaElements = document.querySelectorAll('video, audio, .plyr');
        controlMedia(mediaElements);
    }

    const observer = new MutationObserver(handleMutations);
    observer.observe(document.body, { subtree: true, childList: true });

    // Initial check for existing media elements
    controlMedia(document.querySelectorAll('video, audio, .plyr'));

    // Function to enter Picture-in-Picture mode
    function enterPIPMode(mediaElements) {
        console.log('Attempting to enter PiP mode...');
        if (document.pictureInPictureEnabled) {
            mediaElements.forEach(function(mediaElement) {
                if (mediaElement instanceof HTMLVideoElement && mediaElement.readyState >= 2) {
                    mediaElement.requestPictureInPicture()
                        .then(() => {
                            isInPiPMode = true;
                            console.log('Entered PiP mode successfully.');
                        })
                        .catch(error => {
                            console.error('Error entering PiP mode:', error);
                        });
                }
            });
        } else {
            console.error('Picture-in-Picture is not supported in this browser.');
        }
    }

    // Function to exit Picture-in-Picture mode
    function exitPIPMode() {
        console.log('Exiting PiP mode...');
        if (document.pictureInPictureElement) {
            document.exitPictureInPicture()
                .then(() => {
                    isInPiPMode = false;
                    console.log('Exited PiP mode successfully.');
                })
                .catch(error => {
                    console.error('Error exiting PiP mode:', error);
                });
        }
    }

    // Function to toggle the PiP mode
    function togglePlayer(enabled) {
        shouldTogglePlayer = enabled;
    }

    // Example usage: togglePlayer(true) to enable PiP mode, togglePlayer(false) to disable
    // togglePlayer(false);
    togglePlayer(false); // Default is disabled

    // Event listener for visibility change
    document.addEventListener('visibilitychange', function() {
        const mediaElements = document.querySelectorAll('video, audio, .plyr');
        handleVisibilityChange(mediaElements);
    });

    // Event listeners for focus change
    window.addEventListener('focus', function() {
        isTabFocused = true;
    });

    window.addEventListener('blur', function() {
        isTabFocused = false;
    });

    // Event listeners to set and remove data-user-paused attribute when the user manually pauses or plays the video
    document.addEventListener('play', function(event) {
        event.target.removeAttribute('data-user-paused');
    });

    document.addEventListener('pause', function(event) {
        event.target.setAttribute('data-user-paused', 'true');
    });

})();