Make any Acellus Video Unpaused

Unpauses the video when changing tabs on Acellus ;)

目前為 2024-04-26 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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');
    });

})();